linux g++ 目录,在linux下使用gcc/g++编译多个.h .c 文件

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

1.cpp

#include

#include "myhead.h"

using namespace std;

int main(){

print();

cout<

return 0;

}

2.cpp

#include

#include "myhead.h"

using namespace std;

void print(){

std::cout<

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

using namespace std;

void fn();

int main(){

cout<

fn();

return 0;

}

2.cpp文件

#include

void fn(){

std::cout<

}

编译:

g++ -c 1.cpp

g++ -c 2.cpp

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

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

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

e5c6a255076b?t=123

文件树

main.cpp :

#include

#include

using namespace std;

int main(){

//fun_head();

cout<

int x=100;

int y=200;

cout<

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

using namespace std;

void print(){

cout<

}

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 头文件。

那么通过 -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

编译通过:

e5c6a255076b?t=123

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$

e5c6a255076b?t=123

image.png

运行结果:

e5c6a255076b?t=123

image.png

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文件和依赖库的文件链接起来,编程可执行的文件。

PPS:

以后再来写,什么是编译,什么是链接。

动态库和静态库的区别

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

extern

如下所示的代码:

e5c6a255076b?t=123

如果在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的话,那么链接的时候,就会报一个错误

e5c6a255076b?t=123

错误提示,连接的时候 两个文件里面的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之后,变量只能在当前文件里面可见。)

e5c6a255076b?t=123

static

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值