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

来源:互联网 发布:gta5娇羞萌妹捏脸数据 编辑:程序博客网 时间:2024/05/18 21:38

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.omyrun:$(Objects)        $(CXX) -o myrun $(Objects)b.o:b.cpp myhead.h        $(CXX) -c b.cppa.o:a.cpp myhead.h        $(CXX) -c a.cpp

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

原创粉丝点击