[英]cmake building shared lib from object library results in undefined symbol
I'm building a shared library using cmake.我正在使用 cmake 构建一个共享库。 Here are the steps that I take, starting with building a shared library
A_shared
using an object library A_obj
.以下是我采取的步骤,首先使用 object 库
A_obj
A_shared
add_library(A_obj OBJECT ${A_SRCS})
add_library(A_shared SHARED)
target_link_libraries(A_shared PUBLIC A_obj)
This process works.这个过程有效。 Now I wish to build another shared library that uses
A_shared
and its own sources.现在我希望构建另一个使用
A_shared
和它自己的源的共享库。 So I have:所以我有:
add_library(B_obj OBJECT ${B_SRCS})
target_link_libraries(B_obj PUBLIC A_shared)
add_library(B_shared SHARED)
target_link_libraries(B_shared PUBLIC B_obj)
It seems to me that this should be valid, because I'm building objects B_obj
which have dependence on a shared libraries A_shared
, and then using these objects to construct shared library B_shared
and at the same time transitively passing the dependence on A_shared
using the call to target_link_libraries
.在我看来,这应该是有效的,因为我正在构建依赖于共享库
A_shared
的对象B_obj
,然后使用这些对象构造共享库B_shared
,同时使用调用传递对A_shared
的依赖到target_link_libraries
。
However, this results in undefined symbols when building in MSVC.但是,这会导致在 MSVC 中构建时出现未定义的符号。 When linking
B_shared.dll
, I get unresolved external dependencies on global variables that were defined in ${A_SRCS}
and used in ${B_srcs}
, and not anything else (like functions).链接
B_shared.dll
时,我得到了对在 ${A_SRCS} 中定义并在${B_srcs}
${A_SRCS}
中使用的全局变量的未解决的外部依赖关系,而不是其他任何东西(如函数)。 Strangely, the object files B_obj
compile fine.奇怪的是,object 文件
B_obj
编译得很好。
B_shared
to A_obj
, it works fine.B_shared
链接到A_obj
,它工作正常。 But this gives me the impression that B_shared
will actually contain the object files from A_obj
, but all I want it only to link to A_shared
.B_shared
实际上将包含来自A_obj
的 object 文件,但我只希望它链接到A_shared
。B_obj
to A_obj
, nothing changes and I still get unresolved dependencies.B_obj
链接到A_obj
,没有任何变化,我仍然得到未解决的依赖关系。 With gcc, B_shared
is successfully linked.与gcc,
B_shared
链接成功。
Therefore, my question is: am I doing the correct thing in cmake to achieve what I want?因此,我的问题是:我在 cmake 中做正确的事情来实现我想要的吗? I'm wondering what I'm misunderstanding, because I've researched this extensively and I can't find the fault in my process, so I would greatly appreciate any clarification.
我想知道我在误解什么,因为我已经对此进行了广泛的研究,但我找不到我的过程中的错误,所以我非常感谢任何澄清。
This comes from the behaviour of shared libraries on Windows in general: See this question .这通常来自 Windows 上共享库的行为:请参阅此问题。 I have to manually export the symbols.
我必须手动导出符号。 The interaction with the object library wasn't creating issues.
与 object 库的交互没有产生问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.