在使用gcc编译的时候有时候会碰到这样的问题,编译为.o(obj) 或者动态库.so文件时没有问题,但是编译链接为可执行文件的时候会出现找不到’xxx’的定义的情况。

例如:

sl:02$ gcc -fPIC -shared -I. hard_disk_factory.c seagate_hard_disk.c toshiba_hard_disk.c -o libhard_disk.so
sl:02$ gcc -I. -L ./ -lhard_disk main.c -o test
/tmp/ccODhv1I.o:在函数‘main’中:
main.c:(.text+0x27):对‘HardDiskFactory’未定义的引用
main.c:(.text+0xa7):对‘_HardDiskFactory’未定义的引用
collect2: error: ld returned 1 exit status

原因

出现这种情况的原因,主要是C/C++编译为obj文件的时候并不需要函数的具体实现,只要有函数的原型即可。但是在链接为可执行文件的时候就必须要具体的实现了。如果错误是未声明的引用,那就是找不到函数的原型,解决办法通常就是相关的头文件未包含,包含即可

解决办法

gcc 依赖顺序问题

这个主要的原因是gcc编译的时候,各个文件依赖顺序的问题。

gcc编译的时候,如果文件a依赖于文件b,那么编译的时候必须把a放前面,b放后面。

在gcc编译时,链接库的指定顺序会影响编译行为

gcc手册说明:

-l library
       Search the library named library when linking.  (The second alternative with the library as a separate argument is only for POSIX compliance and is not
       recommended.)

       It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are
       specified.  Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o.  If bar.o refers to functions in z, those functions may not be
       loaded.

       The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a.  The linker then uses this file as if
       it had been specified precisely by name.

       The directories searched include several standard system directories plus any that you specify with -L.

       Normally the files found this way are library files---archive files whose members are object files.  The linker handles an archive file by scanning
       through it for members which define symbols that have so far been referenced but not defined.  But if the file that is found is an ordinary object file,
       it is linked in the usual fashion.  The only difference between using an -l option and specifying a file name is that -l surrounds library with lib and
       .a and searches several directories.

即链接时,符号寻找过程是根据库路径列表依次找到需要链接的库,若一个库在该路径列表中存在多个,则使用第一个找到的。从左向右,依次找寻符号定义gcc -o c -lb -la,符号搜寻过程是c=>b, b=>a
所以在main.c中使用了libhard_disk.so库中相关的函数,那么编译的时候必须是main.c在前,-lhard_disk在后。

上面出现问题的原因就是依赖库的顺序在前面,将其放置在后面即可。

sl:02$ gcc main.c -I. -L ./ -lhard_disk -o test
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐