即使链接了库,CMake可执行链接程序错误 [英] CMake executable linker error even though the libraries are linked
问题描述
我对CMake还是很陌生,但是我已经遇到了一个我不明白的错误.
I'm pretty new to CMake, but I already encountered an error which I do not understand.
由于给定的原因,我具有以下C ++设置:我有两个自己的静态库,多个外部静态库和一个可执行文件.我自己的库a
定义了通用内容,所有外部库都链接到其中.我的库b
定义了更多特殊内容,并将库a
链接到其中.最后,b
被链接到我的可执行文件c
中. b
和c
都使用来自不同外部库的函数,这些函数应该通过a
可见.
For given reasons, I have the following C++ setup: I have two own static libraries, multiple external static libraries and one executable. My own libraries a
defines common stuff and all external libraries are linked into it. My library b
defines more special stuff and has library a
linked into it. Finally, b
is linked into my executable c
. Both b
and c
use functions from different external libraries, which should be visible through a
.
我有以下CMakeLists.txt,在这里我仅使用一个外部库:ZeroC Ice减少了问题.
I have the following CMakeLists.txt, where I reduced the problem by using only one external library: ZeroC Ice.
根CMakeLists.txt
cmake_minimum_required (VERSION 3.3.0)
project (myproject)
# some settings
if (CMAKE_COMPILER_IS_GNUCC)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fmessage-length=0")
endif (CMAKE_COMPILER_IS_GNUCC)
if (CMAKE_COMPILER_IS_GNUCXX)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmessage-length=0")
endif (CMAKE_COMPILER_IS_GNUCXX)
set (CMAKE_VERBOSE_MAKEFILE ON)
set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--export-all-symbols")
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
include_directories (${CMAKE_CURRENT_SOURCE_DIR})
# ZeroC Ice
find_package (Ice 3.6.0 REQUIRED COMPONENTS Ice IceUtil)
include_directories (${Ice_INCLUDE_DIRS})
# libraries and executable
add_subdirectory (a)
add_subdirectory (b)
add_subdirectory (c)
用于a
CMakeLists.txt for a
set (a_FILES
stuff.cpp
)
add_library (a STATIC ${a_FILES})
# link to third-party libraries
target_link_libraries (a PUBLIC
${Ice_LIBRARIES}
)
用于b
CMakeLists.txt for b
set (b_FILES
more_stuff.cpp
)
add_library (b STATIC ${b_FILES})
# link to a
target_link_libraries (b PUBLIC
a
)
用于c
CMakeLists.txt for c
set (c_FILES
main.cpp
)
add_executable (c ${c_FILES})
# link to b
target_link_libraries (c PUBLIC
b
)
在此示例中,stuff.cpp
,more_stuff.cpp
和main.cpp
使用Ice库中的类. a
和奇怪的是,甚至b
都可以毫无问题地进行编译和链接.只有c
会引发很多类似的链接器错误:
In this example, stuff.cpp
, more_stuff.cpp
and main.cpp
use classes from the Ice library. a
and strangely even b
compile and link without any problem. Only c
throws a lot of similar linker errors:
C:/PROGRA~2/ZeroC/ICE-36~1.0/include/Ice/FactoryTableInit.h:27:对'_imp___ZN11IceInternal16FactoryTableInitD1Ev'的未定义引用
C:/PROGRA~2/ZeroC/ICE-36~1.0/include/Ice/FactoryTableInit.h:27: undefined reference to `_imp___ZN11IceInternal16FactoryTableInitD1Ev'
对我来说,CMakeLists.txt设置看起来还不错,而且谷歌搜索几天也没有什么改变.我将不胜感激!
For me, the CMakeLists.txt setup looks just fine and googleing for days did not change anything. I would appreciate any help!
我正在使用CMake 3.3.1和CMake Generator"Eclipse CDT4-MinGW Makefiles"(g ++版本4.8.1)在Windows 10上工作.
I'm working on Windows 10 with CMake 3.3.1 and CMake Generator "Eclipse CDT4 - MinGW Makefiles" (g++ version 4.8.1).
提前谢谢! :)
c
的g ++链接器输出显示它尝试链接到liba.a libb.a C:/.../ice.lib C:/.../iceutil.lib
,这对我来说似乎还不错.无论如何,undefined reference
链接器错误发生在其类在ice.lib
内部实现的头文件中,所以我不知道这里出了什么问题...
The g++ linker output for c
shows that it tries to link against liba.a libb.a C:/.../ice.lib C:/.../iceutil.lib
, which seems just fine to me. Anyways, the undefined reference
linker errors occur in header files whose classes are implemented inside of ice.lib
, so I don't know what is going wrong here...
推荐答案
我自己发现了一个解决方法":使用另一个CMake生成器.通过将Visual Studio用作基础编译器和链接器,不会出现此问题.看来问题在于混淆了.lib
和.a
库.由于Windows上的默认Ice安装仅提供.lib
文件,因此我最终切换到VS,一切正常.
I found a "workaround" by myself: Using another CMake generator. By using Visual Studio as underlying compiler and linker, the issue does not appear. It seems like the problem was mixing up .lib
and .a
libraries. Since the default Ice installation on windows only provides .lib
files, I ended up switching to VS and everything worked.
这篇关于即使链接了库,CMake可执行链接程序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!