问题描述
我的cmake的输出是一个静态库.我正在这样创建它:
The output of my cmake is a static library. I'm creating it as such:
add_library(myMainLib STATIC ${BACKEND_SOURCES})
当我尝试让myMainLib链接到第三方lib/dll时出现问题.将在运行时找到该dll文件,但是,我尝试针对lib文件导入/链接,但未成功.我的第三方库是SDL2和SDL2 NET.
Problems arise when I try to get myMainLib to link against a third party lib/dll. The dll file will be found at run time, however, I'm trying to import/link against the lib file, with no success. My third party library is SDL2 and SDL2 NET.
我认为这很简单,已经用尽了我在网上找到的所有方法.都失败了.下面是我尝试过的列表.请告诉我我做错了.
I would think this is straight forward and have exhausted all methods I've found online. All fail. A list of what I've tried is below. Please inform me what I'm doing wrong.
-
使用target_link_libraries的简单方法
Simple method, using target_link_libraries
add_library(myMainLib STATIC ${BACKEND_SOURCES})
target_link_libraries(myMainLib path_to_thirdPartyLib/thirdParty.lib)
根据 cmake docs
add_library(myMainLib STATIC ${BACKEND_SOURCES})
add_library(Third_Party SHARED IMPORTED)
set_property(TARGET Third_Party PROPERTY IMPORTED_LOCATION path_to_thirdPartyLib/thirdParty.dll)
set_property(TARGET Third_Party PROPERTY IMPORTED_IMPLIB path_to_thirdPartyLib/thirdParty.lib)
target_link_libraries(myMainLib Third_Party)
使用链接目录设置库的路径
Set path to library using link directories
add_library(myMainLib STATIC ${BACKEND_SOURCES})
set(LIB_DIR path_to_thirdPartyLib)
LINK_DIRECTORIES(${LIB_DIR})
target_link_libraries(myMainLib ${LIB_DIR}/thirdParty.lib)
尝试查找库
Try finding the library
add_library(myMainLib STATIC ${BACKEND_SOURCES})
find_library(Third_Party thirdParty.lib)
if(Third_Party)
#never gets in here
target_link_libraries(myMainLib ${Third_Party})
endif()
推荐答案
在CMake和一些构建系统中,将静态库直接链接到另一个静态库是没有意义的.您可以构建一个静态库,然后构建一个静态库,并使您的可执行项目链接到这两个库,但是无法将第一个静态库与第二个库链接,然后将它们链接到最终的可执行文件中.尽管VS允许这样做,但对于其他构建系统却没有意义,因此CMake拒绝了它.
In CMake and several build systems directly linking a static library into another static library is meaningless. You can build a static library and a second one and have your executable project linked against both, but it's not possible to link the first static library with the second library and then link them into the final executable. Although VS allows that, it doesn't make sense for other build systems and thus CMake refrains from it.
某些解决方案涉及使您的静态库成为共享库,或者将库源文件放入可执行文件中.
Some solutions involve making your static library a shared one or pull the library sources into the executable.
其他详细信息此处
这篇关于cmake针对dll/lib的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!