简体   繁体   English

cmake针对dll / lib的链接

[英]cmake link against dll/lib

The output of my cmake is a static library. 我的cmake的输出是一个静态库。 I'm creating it as such: 我正在这样创建它:

add_library(myMainLib STATIC ${BACKEND_SOURCES})

Problems arise when I try to get myMainLib to link against a third party lib/dll. 当我尝试使myMainLib链接到第三方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. 将在运行时找到该dll文件,但是,我试图针对lib文件导入/链接,但未成功。 My third party library is SDL2 and SDL2 NET. 我的第三方库是SDL2和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. 请告诉我我在做什么错。

  1. Simple method, using target_link_libraries 简单的方法,使用target_link_libraries

     add_library(myMainLib STATIC ${BACKEND_SOURCES}) target_link_libraries(myMainLib path_to_thirdPartyLib/thirdParty.lib) 
  2. According to cmake docs 根据cmake文档

     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) 
  3. 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) 
  4. 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() 

In CMake and several build systems directly linking a static library into another static library is meaningless. 在CMake和几个构建系统中,将静态库直接链接到另一个静态库是没有意义的。 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. 尽管VS允许这样做,但对于其他构建系统没有意义,因此CMake拒绝了它。

Some solutions involve making your static library a shared one or pull the library sources into the executable. 一些解决方案涉及使您的静态库成为共享库,或者将库源放入可执行文件中。

Other details here 其他细节在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM