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

编译---链接过程

   要理解C文件与头文件有什么不同之处,首先需要弄明白编译器的工作过程,一般说来编译器会做以下几个过程: 
1.预处理阶段 
2.词法与语法分析阶段 
3.编译阶段,首先编译成纯汇编语句,再将之汇编成跟CPU相关的二进制码,生成各个目标文件 
4.连接阶段,将各个目标文件中的各段代码进行绝对地址定位,生成跟特定平台相关的可执行文件,编译器在编译时是以C文件为单位进行的,也就是说如果你的项目中一个C文件都没有,那么你的项目将无法编译,连接器是以目标文件为单位,它将一个或多个目标文件进行函数与变量的重定位,生成最终的可执行文件,在PC上的程序开发,一般都有一个main函数,这是各个编译器的约定。为了生成一个最终的可执行文件,就需要一些目标文件,也就是需要C文件,而这些C文件中又需要一个main函数作为可执行程序的入口。 
    简单些说就是C语言的编译分为预处理、编译、汇编、链接(test.c test.h => test.i => test.s => test.o => test)四个大的阶段。c文件中的#include宏处理,会在预处理的阶段将c中引用的h文件的内容全部写到c文件中,最后生成.i中间文件,这时h 文件中的内容就相当于被写道c文件中。这也为代码的复用提供了渠道,很多的c文件可以去引用同一个h文件,这样这个h文件就会被放到多个c文件中被编译多 次,这也是h文件中不能放定义只能放声明的原因,放定义时被编译多次,在程序链接的时候(系统中定义了多个int a;强符号定义)会出现错误, 声明就不一样,声明表示对定义的扩展,最终都会终结到一个定义上,所以不会出现link时重复定义的错误。

 

多个文件编译在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
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 9
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
make for plat=atlas310 cross=aarch64-ascend310-linux-gnu- LDFLAGS=-Wl,--gc-sections -static -L/home/257916/server/test/v1.1/Trunk/build/../src/thirdpart/libs/atlas310 -L/home/257916/server/test/v1.1/Trunk/build/../lib/atlas310 -ldw -lbs -lpthread -lm EXTRA_CFLAGS=-DSVN_VERSION="\"64670"\" -I /net -DOSA_MODULE_NAME=Spectrum-Convert@64670 -DBUILD_DATE="\"Mon, 24 Jul 2023 19:48:54 +0800"\" CFLAGS=-D_GNU_SOURCE -I/home/257916/server/test/v1.1/Trunk/build/../include -I/home/257916/server/test/v1.1/Trunk/build/../src/base/include -I/home/257916/server/test/v1.1/Trunk/build/../src/base/test/include -I/home/257916/server/test/v1.1/Trunk/build/../src/decode/include -I/home/257916/server/test/v1.1/Trunk/build/../src/decode/test/include -I/home/257916/server/test/v1.1/Trunk/build/../src/fourier/include -I/home/257916/server/test/v1.1/Trunk/build/../src/fourier/test/include -I/home/257916/server/test/v1.1/Trunk/build/../src/include -I/home/257916/server/test/v1.1/Trunk/build/../src/service/include -I/home/257916/server/test/v1.1/Trunk/build/../src/service/test/include -I/home/257916/server/test/v1.1/Trunk/build/../src/draw/include -I/home/257916/server/test/v1.1/Trunk/build/../src/draw/test/include -I/home/257916/server/test/v1.1/Trunk/build/../tools/include -fPIC -ffunction-sections -fdata-sections -g -Wall -O1 -c -o ############################################################ mkdir -p /home/257916/server/test/v1.1/Trunk/build/../bin/atlas310 aarch64-ascend310-linux-gnu-g++ -o draw test/src/draw_pic_test.o -Wl,--gc-sections -static -L/home/257916/server/test/v1.1/Trunk/build/../src/thirdpart/libs/atlas310 -L/home/257916/server/test/v1.1/Trunk/build/../lib/atlas310 -ldw -lbs -lpthread -lm /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: error adding symbols: file in wrong format collect2: error: ld returned 1 exit status make[1]: *** [draw] Error 1 make[1]: Leaving directory `/home/257916/server/test/v1.1/Trunk/src/draw' make: *** [draw] Error 2
07-25
根你提供的信息,编译过程中出现了一个错误。错误信息显示 ld (链接器) 试图处理一个格式错误的文件。 具体来说,ld 报告了以下错误: ``` /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: error adding symbols: file in wrong format collect2: error: ld returned 1 exit status ``` 这个错误通常发生在尝试链接不兼容的目标文件时。可能有以下几个原因导致这个错误: 1. 目标文件 `test/src/draw_pic_test.o` 的格式不正确,不符合链接器的预期。你可能需要检查这个目标文件是否由正确的编译器生成,并且是否与你的交叉编译目标匹配。 2. 链接器正在尝试将不同架构的目标文件链接在一起。请确保你的交叉编译环境配置正确,并且编译器和链接器的目标架构与你的目标平台一致。 3. 缺少必要的库文件。请确保你的链接命令中指定的库文件存在,并且位于正确的位置。 针对这个错误,你可以尝试以下几个步骤来解决问题: 1. 检查目标文件的生成方式和来源。确保使用正确的编译器和编译选项生成目标文件,并确认目标文件的格式正确。 2. 检查你的交叉编译环境配置,确保交叉编译器和链接器的目标架构与你的目标平台一致。 3. 检查链接命令中指定的库文件路径是否正确,并确保库文件存在。 如果问题仍然存在,可能需要更多的上下文信息来进行进一步的诊断和解决。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值