CMake:使用gcc正确地链接系统库。

时间:2022-09-18 12:26:43

I have a static libary mylib that depends on the math library.

我有一个依赖于数学库的静态libary mylib。

If I first link mylib with math and then to my executable it works:

如果我先把mylib和数学联系起来,然后再把它链接到我的可执行文件上,它就会起作用:

add_executable(myapp main.c)
target_link_libraries(mylib m)
target_link_libraries(myapp mylib)

But if I do the linking directly with the executable it fails when using gcc (with clang it works!)

但是,如果我直接与可执行文件连接,那么当使用gcc时,它就失败了。

add_executable(myapp main.c)
target_link_libraries(myapp m mylib)

Why does this make any difference?
I thought that it is anyway not possible to link libraries together?

为什么会有不同呢?我认为把图书馆链接在一起是不可能的?

2 个解决方案

#1


6  

When using cmake's target_link_libraries it does not mean you will link anything. It rather will create a dependency between a target and a library of type/action link.

当使用cmake的target_link_libraries时,它并不意味着您将链接任何东西。它将创建目标和类型/操作链接库之间的依赖关系。

I guess that the actually build line of the first example will result in something like that:

我想第一个例子的构建线会产生这样的结果:

gcc -o myapp myapp.o -lmylib -lm

and the second one

和第二个

gcc -o myapp myapp.o -lm -lmylib

. If mylib has references to m the second example (might) not link.

。如果mylib引用了第二个示例(可能),则没有链接。

Try to run make VERBOSE=1 and study the command-line of the link-process to really understand what's happening. The linker of clang is maybe intelligent and waits for all calls to be linked before actually dropping a library during the link-process.

尝试运行make VERBOSE=1并研究链接过程的命令行,以真正了解正在发生的事情。clang的链接器可能是智能的,在链接过程中删除一个库之前,等待所有调用链接。

#2


0  

When using target_link_libraries it matters in which order you specify linked libraries.

当使用target_link_libraries时,您指定链接库的顺序非常重要。

This does not work when using gcc (at least in v4.6.3):

这在使用gcc时不起作用(至少在v4.6.3中):

target_link_libraries(myapp m mylib)

while this works:

虽然这工作原理:

target_link_libraries(myapp mylib m)

So all libraries mylib depends on have to come after mylib.

所以所有库mylib都依赖于mylib之后。

If you track down the actual linker invocation with make VERBOSE=1 you will find this for the broken example:

如果您用make VERBOSE=1跟踪实际的链接器调用,就会发现这个例子的例子:

gcc main.c.o  -o luatest -rdynamic -lm mylib.a

and this for the working one:

这是关于工作的:

gcc main.c.o  -o luatest -rdynamic mylib.a -lm

Invoking clang with the exact same parameters works in both cases!

在这两种情况下调用具有完全相同参数的clang !

So @PatrickB seems to be right:

所以@PatrickB似乎是对的:

The linker of clang is maybe intelligent and waits for all calls to be linked before actually dropping a library during the link-process.

clang的链接器可能是智能的,在链接过程中删除一个库之前,等待所有调用链接。

#1


6  

When using cmake's target_link_libraries it does not mean you will link anything. It rather will create a dependency between a target and a library of type/action link.

当使用cmake的target_link_libraries时,它并不意味着您将链接任何东西。它将创建目标和类型/操作链接库之间的依赖关系。

I guess that the actually build line of the first example will result in something like that:

我想第一个例子的构建线会产生这样的结果:

gcc -o myapp myapp.o -lmylib -lm

and the second one

和第二个

gcc -o myapp myapp.o -lm -lmylib

. If mylib has references to m the second example (might) not link.

。如果mylib引用了第二个示例(可能),则没有链接。

Try to run make VERBOSE=1 and study the command-line of the link-process to really understand what's happening. The linker of clang is maybe intelligent and waits for all calls to be linked before actually dropping a library during the link-process.

尝试运行make VERBOSE=1并研究链接过程的命令行,以真正了解正在发生的事情。clang的链接器可能是智能的,在链接过程中删除一个库之前,等待所有调用链接。

#2


0  

When using target_link_libraries it matters in which order you specify linked libraries.

当使用target_link_libraries时,您指定链接库的顺序非常重要。

This does not work when using gcc (at least in v4.6.3):

这在使用gcc时不起作用(至少在v4.6.3中):

target_link_libraries(myapp m mylib)

while this works:

虽然这工作原理:

target_link_libraries(myapp mylib m)

So all libraries mylib depends on have to come after mylib.

所以所有库mylib都依赖于mylib之后。

If you track down the actual linker invocation with make VERBOSE=1 you will find this for the broken example:

如果您用make VERBOSE=1跟踪实际的链接器调用,就会发现这个例子的例子:

gcc main.c.o  -o luatest -rdynamic -lm mylib.a

and this for the working one:

这是关于工作的:

gcc main.c.o  -o luatest -rdynamic mylib.a -lm

Invoking clang with the exact same parameters works in both cases!

在这两种情况下调用具有完全相同参数的clang !

So @PatrickB seems to be right:

所以@PatrickB似乎是对的:

The linker of clang is maybe intelligent and waits for all calls to be linked before actually dropping a library during the link-process.

clang的链接器可能是智能的,在链接过程中删除一个库之前,等待所有调用链接。