博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Ubuntu动态链接库问题

Posted on 2012-04-21 15:53  algorithmer  阅读(1578)  评论(0)    收藏  举报
在CentOS上,Makefile中动态链接库的位置放在源码文件名的前面和后面都可以,而在ubuntu中LIBS必须放到源文件名的后面,否则会碰到下面的问题:

I am currently using gcc to compile and I need to use <math.h>. Problem is that it won't recognize the library. I have also tried -lm and nothing. The function I tried to use was ceil() and I get the following error:

: undefined reference to `ceil'
collect2: ld returned 1 exit status

I am using the latest Ubuntu and math.h is there. I tried to use -lm in a different computer and it work perfectly.

Does anyone know how to solve this problem?


I did include <math.h>. Also, the command I used was:

gcc -lm -o fb file.c

Take this code and put it in a file ceil.c:

#include <math.h>
#include <stdio.h>
int main(void)
{
    printf("%f\n", ceil(1.2));
    return 0;
}

Compile it with:

$ gcc -o ceil ceil.c
$ gcc -o ceil ceil.c -lm

One of those two should work. If neither works, show the complete error message for each compilation. Note that -lm appears after the name of the source file (or the object file if you compile the source to object before linking).

Try changing the relevant lines to this:

LDFLAGS=`pkg-config --libs-only-L --libs-only-other $(PACKAGES)`
LIBS=`pkg-config --libs-only-l $(PACKAGES)`

# ...

./main: ./main.o
    $(LD) $(LDFLAGS) ./main.o -o ./main $(LIBS)
The reason is that the linker may search libraries in the order they are given on the command line, so they should always be placed last to be safe.