将静态库与 C 数学库正确链接
Properly Linking a static library with the C math library
我有一个使用 math.h 中的日志函数的库。我在编译打包这个库的时候没有出现编译错误,这是正常的(我觉得)。
现在,当我尝试在应用程序中使用库时,gcc 给我链接器错误:
Compiling mytestlist using "mytestlist.o":
gcc mytestlist.o -I/student/cmpt332/pthreads -I. -std=c99 -Wall -pedantic -L. -L/student/cmpt332/pthreads/lib/linuxx86_64/ -llist -o mytestlist
./liblist.a(list_adders.o): In function `NodeCreate':
list_adders.c:(.text+0x343): undefined reference to `log'
./liblist.a(list_adders.o): In function `ListCreate':
list_adders.c:(.text+0x62f): undefined reference to `log'
./liblist.a(list_adders.o): In function `ListFree':
list_adders.c:(.text+0xdcc): undefined reference to `log'
list_adders.c:(.text+0xe55): undefined reference to `log'
list_adders.c:(.text+0xefb): undefined reference to `log'
./liblist.a(list_adders.o):list_adders.c:(.text+0xf96): more undefined references to `log' follow
collect2: error: ld returned 1 exit status
Makefile:47: recipe for target 'mytestlist' failed
make: *** [mytestlist] Error 1
为什么会这样?唯一可行的解决方案是,当我编译使用该库的程序时,我必须向 gcc 提供 -lm
选项(即使程序本身不使用 math.h),但我发现这个做起来很麻烦。
我也试过在编译库时提供 -lm
选项,但是当使用库编译应用程序时,我遇到了同样的链接器错误。
有没有一种方法可以使用 math.h 编译库,而不必向使用该库的其他程序提供 -lm
?
如果您想知道,我使用以下方法编译构成库的每个对象:
gcc -std=c99 -Wall -pedantic -static -I. -c list_adders.c -o list_something.o -lm
库使用以下方式打包:
ar cvfr liblist.a list_something.o ...
在您的 gcc -c
命令中,-lm
没有执行任何操作。这是一个链接器选项,-c
表示 "don't link".
放置 -lm
的正确位置实际上是在 -llist
之后,无论何时使用它。静态库依赖就是这样搞定的。把它放在 liblist 的文档中。
如果您想要更高级的,pkg-config
。使用适当的配置文件,pkg-config --static --libs liblist
将输出 -llist -lm
.
我有一个使用 math.h 中的日志函数的库。我在编译打包这个库的时候没有出现编译错误,这是正常的(我觉得)。
现在,当我尝试在应用程序中使用库时,gcc 给我链接器错误:
Compiling mytestlist using "mytestlist.o":
gcc mytestlist.o -I/student/cmpt332/pthreads -I. -std=c99 -Wall -pedantic -L. -L/student/cmpt332/pthreads/lib/linuxx86_64/ -llist -o mytestlist
./liblist.a(list_adders.o): In function `NodeCreate':
list_adders.c:(.text+0x343): undefined reference to `log'
./liblist.a(list_adders.o): In function `ListCreate':
list_adders.c:(.text+0x62f): undefined reference to `log'
./liblist.a(list_adders.o): In function `ListFree':
list_adders.c:(.text+0xdcc): undefined reference to `log'
list_adders.c:(.text+0xe55): undefined reference to `log'
list_adders.c:(.text+0xefb): undefined reference to `log'
./liblist.a(list_adders.o):list_adders.c:(.text+0xf96): more undefined references to `log' follow
collect2: error: ld returned 1 exit status
Makefile:47: recipe for target 'mytestlist' failed
make: *** [mytestlist] Error 1
为什么会这样?唯一可行的解决方案是,当我编译使用该库的程序时,我必须向 gcc 提供 -lm
选项(即使程序本身不使用 math.h),但我发现这个做起来很麻烦。
我也试过在编译库时提供 -lm
选项,但是当使用库编译应用程序时,我遇到了同样的链接器错误。
有没有一种方法可以使用 math.h 编译库,而不必向使用该库的其他程序提供 -lm
?
如果您想知道,我使用以下方法编译构成库的每个对象:
gcc -std=c99 -Wall -pedantic -static -I. -c list_adders.c -o list_something.o -lm
库使用以下方式打包:
ar cvfr liblist.a list_something.o ...
在您的 gcc -c
命令中,-lm
没有执行任何操作。这是一个链接器选项,-c
表示 "don't link".
放置 -lm
的正确位置实际上是在 -llist
之后,无论何时使用它。静态库依赖就是这样搞定的。把它放在 liblist 的文档中。
如果您想要更高级的,pkg-config
。使用适当的配置文件,pkg-config --static --libs liblist
将输出 -llist -lm
.