本文主要是介绍gcc 编译时对’xxxx’未定义的引用问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在使用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 librarySearch the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is notrecommended.)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 arespecified. 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 beloaded.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 ifit 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 scanningthrough 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
这篇关于gcc 编译时对’xxxx’未定义的引用问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!