【转】在linux下使用gcc/g++编译多个.h文件

转自:https://www.jianshu.com/p/e5c6a255076b

博主写得很好

多个文件编译在linux下编译,下面有三个文件,分别是1.cpp 和 2.cpp 和myhead.h 文件。

1.cpp

#include <iostream>
#include "myhead.h"
using namespace std;

int main(){
    print();
    cout<<"yes !"<<endl;
    return 0;
}

2.cpp

#include <iostream>
#include "myhead.h"
using namespace std;

void print(){
    std::cout<<" print "<<std::endl;
    cout<<
}

myhead.h

#ifndef __myhead_h
#define __myhead_h
void  print();
#endif

假如他们都在一个目录下面,那么编译流程:

g++ -c 2.cpp             #将2.cpp 编译成2.o 文件
g++ 1.cpp -o a.out 2.o   #多个文件一起链接

or

g++ -c 2.cpp 
g++ -c 1.cpp 
g++ 1.o 2.o -o test

当然,没有头文件,两个.c文件也是可以编译的。如下:

1.cpp文件

#include <iostream>
using namespace std;
void fn();
int main(){
    cout<<"123"<<endl;
    fn();
    return 0;
}

2.cpp文件

#include <iostream>
void fn(){
    std::cout<<"fn"<<std::endl;
}

编译:

g++ -c 1.cpp 
g++ -c 2.cpp 
g++ -o test 1.o 2.o

在稍微大一点的项目里面,一般都会包含多个文件。尤其是包含多个头文件,各自头文件的实现,和包含main函数的文件。这样的好处就是更容易开发和维护。

举一个简单的例子,main.cpp 文件是包含main函数的文件,在myinclude的文件下,包含了myhead.h 和 myhead.cpp 文件。分别是头文件的定义和实现。


 
文件树

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;
}

下面开始编译:
假如在当前目录直接编译的话:

zhaozheng@ubuntu:~/code/c++/test_compile/src$ g++ main.cpp -o main
main.cpp:2:20: fatal error: myhead.h: No such file or directory
compilation terminated.

直接编译的结果就是报错了,错误的原因是 默认的include目录下面没有 myhead.h 头文件。

如果不知道include的默认的头文件,请看:
http://www.jianshu.com/p/3eb25114576e

那么通过 -I 选项 链接上去。重新编译

zhaozheng@ubuntu:~/code/c++/test_compile/src$ g++ main.cpp -o main -I ../myinclude/
/tmp/ccH3BlLo.o: In function `main':
main.cpp:(.text+0x3e): undefined reference to `sum(int, int)'
collect2: error: ld returned 1 exit status

有报错,错误的原因是头文件虽然找到了,但是没有提示错误,没有定义sum函数。sum函数是在myhead.cpp文件上面定义的。也就是需要把myhead.cpp文件编译一下。

zhaozheng@ubuntu:~/code/c++/test_compile/myinclude$ g++ -c myhead.cpp -o myhead.o

编译通过:


 
image.png

最后,再一次的编译:

zhaozheng@ubuntu:~/code/c++/test_compile/src$ g++ main.cpp -o main -I ../myinclude/    ../myinclude/myhead.o
zhaozheng@ubuntu:~/code/c++/test_compile/src$ 
 
image.png

运行结果:


 
image.png

PS : 上面写得是,如果编译,链接多个文件,如果好奇编译和链接的具体过程,请看 :
http://www.jianshu.com/writer#/notebooks/10152412/notes/9498296

PS:
gcc/g++ 里面的-c选项指的是,比如

gcc -c myhead.cpp -o myhead.o

将一个文件编译,但是不链接。将代码变成机器码。

比如,

gcc -c main.cpp -o main.o

将main.cpp文件编译为main.o 文件

那么链接器的作用是什么?

gcc -o main  main.o  ../myinclude/myhead.o 

这样将两个.o文件和依赖库的文件链接起来,编程可执行的文件。

<br />
PPS:

  1. 以后再来写,什么是编译,什么是链接。
  2. 动态库和静态库的区别

多个文件编译的时候,可以顺便理解一下static,extern两个关键字的意思。

  • extern
    如下所示的代码:

     
     

如果在2.cpp文件里面定义了一个变量,在1.cpp这个文件里面去访问这个变量。在链接的时候,2.cpp里面的变量对1.cpp是可以的。1.cpp里面是可以访问这个变量的,但是,需要使用extern声明这个变量是赖在其他文件的。

编译

g++ -c 1.cpp 
g++ -c 2.cpp
g++ 1.o 2.o -o test

但是,如果去掉extern的话,那么链接的时候,就会报一个错误


 
 

错误提示,连接的时候 两个文件里面的value都是彼此可以的,所以,提示变量被定义了多次。

2.o:(.data+0x0): multiple definition of `value'
1.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status

改正这个错误,除了可以加上extern 之外,还可以加上static,改变变量的可见范围。(加上static之后,变量只能在当前文件里面可见。)

 
 
  • static

static 声明一个变量的时候,除了可以将变量的空间开辟在全局区。第二个就是改变变量的可见范围,这个变量只能在文件内部可以。



作者:zhaozhengcoder
链接:https://www.jianshu.com/p/e5c6a255076b
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

诸位正值青春年少,一定恣情放纵,贪恋香艳梅施之情,喜欢风流雅韵之事,洒脱木拘。然而诸位可知,草上露一碰即落,竹上霜一触即溶,此种风情难于长久。
原文地址:https://www.cnblogs.com/shilipojianshen/p/13207669.html