Linux多文件编译.h或c文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LoveJiaYu/article/details/85337127

1.在home中建立test文件,在test中建立myinclude,src文件。myinclude下建立myhead.cpp,myhead.h文件。src中建立main.cpp文件。如下图所示:

2.源代码如下:

main.cpp:

#include <iostream>
#include <myhead.h>
using namespace std;
int main()
{
    //fun_head();
    cout<<"in main"<<endl;
    int x=100;
    int y=200;
    cout<<"sum : "<<sum(x,y);
    return 0;
}

myhead.h:

#ifndef __myhead_h
#define __muhead_h
void  print();
int sum(int a,int b);
#endif

myhead.cpp:

#include "myhead.h"
#include <iostream>
using namespace std;
void  print(){
    cout<<"in fun head"<<endl;
}
int sum(int a,int b){
    return a+b;
}

3.编译命令如下:

提示:

(1).必须先将myhead.cpp文件编译一下,生成myhead.o

(2).如果我们在给要编译的代码加上自己的头文件,那么gcc或g++ 编译的时候 ,加上 -I 选项。

4.运行结果如下:

猜你喜欢

转载自blog.csdn.net/LoveJiaYu/article/details/85337127