首页 > 电脑

gcc编译时出现很多undefinedreferenceto怎么解决啊

更新时间2018-03-06 22:02:34

omo@ubuntu:~/Software/RNAplex/Progs$ make

gcc -DHAVE_CONFIG_H -I. -I.. -I./../H    -g -O2 -MT RNAplex.o -MD -MP -MF .deps/RNAplex.Tpo -c -o RNAplex.o RNAplex.c

RNAplex.c: In function ‘read_plfold_i_bin’:

RNAplex.c:1388:3: warning: ignoring return value of ‘fread’, declared with attribute warn_unused_result [-Wunused-result]

   fread(first_line,sizeof(int),(end-beg)+1,fp);                  //get the value of the u option

   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

RNAplex.c:1411:5: warning: ignoring return value of ‘fread’, declared with attribute warn_unused_result [-Wunused-result]

     fread(access[count],sizeof(int),(end-beg)+1,fp); //read the needed number of accessibility values

     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

mv -f .deps/RNAplex.Tpo .deps/RNAplex.Po

gcc  -g -O2   -o RNAplex RNAplex.o ../lib/libRNAplex.a -lm 

RNAplex.o: In function `read_plfold_i_bin':

/home/omo/Software/RNAplex/Progs/RNAplex.c:1387: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1399: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1403: undefined reference to `space'

RNAplex.o: In function `read_plfold_i':

/home/omo/Software/RNAplex/Progs/RNAplex.c:1231: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1233: undefined reference to `space'

RNAplex.o:/home/omo/Software/RNAplex/Progs/RNAplex.c:1457: more undefined references to `space' follow

RNAplex.o: In function `main':

/home/omo/Software/RNAplex/Progs/RNAplex.c:259: undefined reference to `update_fold_params'

/home/omo/Software/RNAplex/Progs/RNAplex.c:261: undefined reference to `read_parameter_file'

/home/omo/Software/RNAplex/Progs/RNAplex.c:263: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:263: undefined reference to `nonstandards'

/home/omo/Software/RNAplex/Progs/RNAplex.c:271: undefined reference to `nonstandards'

/home/omo/Software/RNAplex/Progs/RNAplex.c:272: undefined reference to `nonstandards'

/home/omo/Software/RNAplex/Progs/RNAplex.c:274: undefined reference to `nonstandards'

/home/omo/Software/RNAplex/Progs/RNAplex.c:275: undefined reference to `nonstandards'

RNAplex.o: In function `main':

/home/omo/Software/RNAplex/Progs/RNAplex.c:121: undefined reference to `subopt_sorted'

RNAplex.o: In function `usage':

/home/omo/Software/RNAplex/Progs/RNAplex.c:1178: undefined reference to `nrerror'

RNAplex.o: In function `main':

/home/omo/Software/RNAplex/Progs/RNAplex.c:148: undefined reference to `do_backtrack'

RNAplex.o: In function `usage':

/home/omo/Software/RNAplex/Progs/RNAplex.c:1178: undefined reference to `nrerror'

RNAplex.o: In function `main':

/home/omo/Software/RNAplex/Progs/RNAplex.c:151: undefined reference to `noGU'

/home/omo/Software/RNAplex/Progs/RNAplex.c:152: undefined reference to `no_closingGU'

/home/omo/Software/RNAplex/Progs/RNAplex.c:153: undefined reference to `noLonelyPairs'

/home/omo/Software/RNAplex/Progs/RNAplex.c:175: undefined reference to `dangles'

/home/omo/Software/RNAplex/Progs/RNAplex.c:177: undefined reference to `dangles'

RNAplex.o: In function `usage':

/home/omo/Software/RNAplex/Progs/RNAplex.c:1178: undefined reference to `nrerror'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1178: undefined reference to `nrerror'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1178: undefined reference to `nrerror'

RNAplex.o: In function `aliprint_struct':

/home/omo/Software/RNAplex/Progs/RNAplex.c:1672: undefined reference to `aliPS_color_aln'

RNAplex.o: In function `main':

/home/omo/Software/RNAplex/Progs/RNAplex.c:926: undefined reference to `get_line'

/home/omo/Software/RNAplex/Progs/RNAplex.c:943: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:948: undefined reference to `get_line'

/home/omo/Software/RNAplex/Progs/RNAplex.c:965: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:975: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:936: undefined reference to `get_line'

/home/omo/Software/RNAplex/Progs/RNAplex.c:959: undefined reference to `get_line'

/home/omo/Software/RNAplex/Progs/RNAplex.c:931: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:954: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1029: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1030: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1076: undefined reference to `nrerror'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1078: undefined reference to `Lduplexfold_CXS'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1020: undefined reference to `Lduplexfold_C'

/home/omo/Software/RNAplex/Progs/RNAplex.c:977: undefined reference to `get_line'

RNAplex.o: In function `make_pair_matrix':

/home/omo/Software/RNAplex/Progs/./../H/pair_mat.h:111: undefined reference to `nrerror'

RNAplex.o: In function `main':

/home/omo/Software/RNAplex/Progs/RNAplex.c:597: undefined reference to `get_line'

/home/omo/Software/RNAplex/Progs/RNAplex.c:628: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1110: undefined reference to `read_clustal'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1111: undefined reference to `read_clustal'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1121: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1122: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1152: undefined reference to `aliLduplexfold_XS'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1118: undefined reference to `nrerror'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1012: undefined reference to `Lduplexfold'

/home/omo/Software/RNAplex/Progs/RNAplex.c:480: undefined reference to `get_line'

/home/omo/Software/RNAplex/Progs/RNAplex.c:511: undefined reference to `space'

/home/omo/Software/RNAplex/Progs/RNAplex.c:525: undefined reference to `get_line'

/home/omo/Software/RNAplex/Progs/RNAplex.c:555: undefined reference to `space'

RNAplex.o: In function `usage':

/home/omo/Software/RNAplex/Progs/RNAplex.c:1178: undefined reference to `nrerror'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1178: undefined reference to `nrerror'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1178: undefined reference to `nrerror'

/home/omo/Software/RNAplex/Progs/RNAplex.c:1178: undefined reference to `nrerror'

RNAplex.o:/home/omo/Software/RNAplex/Progs/RNAplex.c:1178: more undefined references to `nrerror' follow

collect2: error: ld returned 1 exit status

Makefile:240: recipe for target 'RNAplex' failed

make: *** [RNAplex] Error 1


  (.text+0x13): undefined reference to `func'  关于undefined reference这样的问题,大家其实经常会遇到,在此,我以详细地示例给出常见错误的各种原因以及解决方法,希望对初学者有所帮助。  1.  链接时缺失了相关目标文件(.o)  测试代码如下:    然后编译。  gcc -c test.c  gcc –c main.c  得到两个 .o 文件,一个是 main.o,一个是 test.o ,然后我们链接 .o 得到可执行程序:  gcc -o main main.o  这时,你会发现,报错了:  main.o: In function `main':  main.c:(.text+0x7): undefined reference to `test'  collect2: ld returned 1 exit status  这就是最典型的undefined reference错误,因为在链接时发现找不到某个函数的实现文件,本例中test.o文件中包含了test()函数的实现,所以如果按下面这种方式链接就没事了。  gcc -o main main.o test.o  【扩展】:其实上面为了让大家更加清楚底层原因,我把编译链接分开了,下面这样编译也会报undefined reference错,其实底层原因与上面是一样的。  gcc -o main main.c //缺少test()的实现文件  需要改成如下形式才能成功,将test()函数的实现文件一起编译。  gcc -o main main.c test.c //ok,没问题了  2.    链接时缺少相关的库文件(.a/.so)  在此,只举个静态库的例子,假设源码如下。  先把test.c编译成静态库(.a)文件  gcc -c test.c  ar -rc test.a test.o  至此,我们得到了test.a文件。我们开始编译main.c  gcc -c main.c  这时,则生成了main.o文件,然后我们再通过如下命令进行链接希望得到可执行程序。  gcc -o main main.o  你会发现,编译器报错了:  /tmp/ccCPA13l.o: In function `main':  main.c:(.text+0x7): undefined reference to `test'  collect2: ld returned 1 exit status  其根本原因也是找不到test()函数的实现文件,由于该test()函数的实现在test.a这个静态库中的,故在链接的时候需要在其后加入test.a这个库,链接命令修改为如下形式即可。  gcc -o main main.o ./test.a  //注:./ 是给出了test.a的路径  【扩展】:同样,为了把问题说清楚,上面我们把代码的编译链接分开了,如果希望一次性生成可执行程序,则可以对main.c和test.a执行如下命令。  gcc -o main main.c ./test.a  //同样,如果不加test.a也会报错  3.    链接的库文件中又使用了另一个库文件  这种问题比较隐蔽,也是我最近遇到的与网上大家讨论的不同的问题,举例说明如下,首先,还是看看测试代码。  从上图可以看出,main.c调用了test.c的函数,test.c中又调用了fun.c的函数。  首先,我们先对fun.c,test.c,main.c进行编译,生成 .o文件。  gcc -c func.c  gcc -c test.c  gcc -c main.c  然后,将test.c和func.c各自打包成为静态库文件。  ar –rc func.a func.o  ar –rc test.a test.o  这时,我们准备将main.o链接为可执行程序,由于我们的main.c中包含了对test()的调用,因此,应该在链接时将test.a作为我们的库文件,链接命令如下。  gcc -o main main.o test.a  这时,编译器仍然会报错,如下:  test.a(test.o): In function `test':  test.c:(.text+0x13): undefined reference to `func'  collect2: ld returned 1 exit status  就是说,链接的时候,发现我们的test.a调用了func()函数,找不到对应的实现。由此我们发现,原来我们还需要将test.a所引用到的库文件也加进来才能成功链接,因此命令如下。  gcc -o main main.o test.a func.a  ok,这样就可以成功得到最终的程序了。同样,如果我们的库或者程序中引用了第三方库(如pthread.a)则同样在链接的时候需要给出第三方库的路径和库文件,否则就会得到undefined reference的错误。  4 多个库文件链接顺序问题  这种问题也非常的隐蔽,不仔细研究你可能会感到非常地莫名其妙。我们依然回到第3小节所讨论的问题中,在最后,如果我们把链接的库的顺序换一下,看看会发生什么结果?  gcc -o main main.o func.a test.a  我们会得到如下报错.  test.a(test.o): In function `test':  test.c:(.text+0x13): undefined reference to `func'  collect2: ld returned 1 exit status  因此,我们需要注意,在链接命令中给出所依赖的库时,需要注意库之间的依赖顺序,依赖其他库的库一定要放到被依赖库的前面,这样才能真正避免undefined reference的错误,完成编译链接。  5. 在c++代码中链接c语言的库  如果你的库文件由c代码生成的,则在c++代码中链接库中的函数时,也会碰到undefined reference的问题。下面举例说明。  首先,编写c语言版库文件:    编译,打包为静态库:test.a  gcc -c test.c  ar -rc test.a test.o  至此,我们得到了test.a文件。下面我们开始编写c++文件main.cpp    然后编译main.cpp生成可执行程序:  g++ -o main main.cpp test.a  会发现报错:  /tmp/ccJjiCoS.o: In function `main':  main.cpp:(.text+0x7): undefined reference to `test()'  collect2: ld returned 1 exit status  原因就是main.cpp为c++代码,调用了c语言库的函数,因此链接的时候找不到,解决方法:即在main.cpp中,把与c语言库test.a相关的头文件包含添加一个extern "C"的声明即可。例如,修改后的main.cpp如下:    g++ -o main main.cpp test.a  再编译会发现,问题已经成功解决。

你的make工程要么少了一个或以上的源码,要么少了库了


上一篇:c语言中用double型函数做另一个函数的参数?

下一篇:请各位大神帮忙p一下图,