c++中如何在主函数中调用其他文件内的函数?

linux下c++中如何在主函数中调用其他文件内的函数?

这个作用不巴拉巴拉了,直接上干货!

需要四个文件: a.cpp包含一个被调用的函数,myhead.h预定义这个函数,3.b.cpp主函数,4.Makefile文件。
1.a.cpp
#include "myhead.h"
#include <iostream>
using namespace std;
int myfun(int a,int b){
         std::cout<<a+b<<std::endl;
        return 0;
}
2.myhead.h
#ifndef A_H_
#define A_H_
int myfun(int a,int b);
#endif
3.b.cpp
#include "myhead.h"
#include <iostream>
int main(int argc, char** argv){
  int a=2,b=3;
  myfun(a,b);
  return 0;
}
4.Makefile
CXX=g++
Objects=b.o a.o
myrun:$(Objects)
        $(CXX) -o myrun $(Objects)
b.o:b.cpp myhead.h
        $(CXX) -c b.cpp
a.o:a.cpp myhead.h
        $(CXX) -c a.cpp

5.linux 命令行下:
make
./myrun

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值