简体   繁体   English

CMake,Exe 找不到 DLL

[英]CMake, Exe cant find DLL

so im trying to setup a project, on windows, with cmake所以我试图在 Windows 上用 cmake 设置一个项目

this is what my project structure looks like:这是我的项目结构的样子:

  • GameEngine游戏引擎
    • .git .git
    • build建造
    • include包括
    • source来源
    • testing测试
    • CMakeLists.txt CMakeLists.txt

the idea is that the "source" directory contain all the .cpp files for the GameEngine library, the "include" directory contain all the header files and the "testing" directory is for testing the library.这个想法是“source”目录包含GameEngine库的所有.cpp文件,“include”目录包含所有头文件,“testing”目录用于测试库。 So it should depend on the GameEngine.dll and GameEngine.lib (on windows).所以它应该依赖于 GameEngine.dll 和 GameEngine.lib(在 Windows 上)。

my top level CMakesLists.txt contain:我的顶级CMakesLists.txt 包含:

    cmake_minimum_required(VERSION 2.7)
    PROJECT( GameEngine )

    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include )

    ADD_SUBDIRECTORY( source )
    add_subdirectory( testing )


the CMakeLists.txt in the "source" directory contain: “源”目录中的 CMakeLists.txt 包含:

    project(GameEngineLib)

    file(GLOB src "*.cpp" "../include/*.h")

    add_library(${PROJECT_NAME} SHARED ${src})


and the CMakeLists.txt in the "testing" directory contain:并且“testing”目录中的 CMakeLists.txt 包含:

    project(Testing)

    file(GLOB src "*.h" "*.cpp")

    ADD_EXECUTABLE( ${PROJECT_NAME} ${src})
    target_link_libraries(${PROJECT_NAME} GameEngineLib)


This all works fine on linux.这一切在 linux 上都可以正常工作。
when i build it, i get two folders (+ some others), "testing" and "source" and a Makefile that outputs an executable and a .so file.当我构建它时,我得到两个文件夹(+ 一些其他文件夹),“testing”和“source”以及一个输出可执行文件和 .so 文件的 Makefile。
and the executable already know where to find the .so file, so i can just run without any errors.并且可执行文件已经知道在哪里可以找到 .so 文件,所以我可以毫无错误地运行。


The problem comes when i build on windows with visual studio.当我使用 Visual Studio 在 Windows 上构建时,问题就出现了。
Everything works except that the Testing executable, that i get after building the "ALL_BUILD" project, gives me an error saying that it can't find GameEngineLib.dll.一切正常,除了我在构建“ALL_BUILD”项目后得到的测试可执行文件给我一个错误,说它找不到 GameEngineLib.dll。


I tried searching around on google on how to fix the problem, but most of the solutions i found just said that you had to manually change your settings in visual studio, to look for the dll, where its located.我试着在谷歌上搜索如何解决这个问题,但我发现的大多数解决方案只是说你必须在 Visual Studio 中手动更改设置,以查找它所在的 dll。


but if i was working in a team with multiple developers, i wouldn't really want to have every developer that uses visual studio to have to go in and change settings in the project themselves.但是如果我在一个有多个开发人员的团队中工作,我真的不希望每个使用 Visual Studio 的开发人员都必须自己进入并更改项目中的设置。


isn't there a way to tell cmake to automatically copy the GameEngineLib.dll to the Tester.exe's folder?没有办法告诉 cmake 自动将 GameEngineLib.dll 复制到 Tester.exe 的文件夹中吗?

I'm trying to equip OpenCV 1.0.0 with CMake support and meet the very similar situation, ie in my own solution, there is a shared library(.dll) and a executable(.exe) built from my source and header files, and when executing that .exe file, how could we ensure .exe can find .dll?我正在尝试为 OpenCV 1.0.0 配备 CMake 支持并满足非常相似的情况,即在我自己的解决方案中,有一个共享库(.dll)和一个从我的源文件和头文件构建的可执行文件(.exe),当执行那个.exe文件时,我们如何确保.exe可以找到.dll?

As @drescherjm commented, the solution is: in root CMakeLists.txt, before add_subdirectory() , add these two lines:正如@drescherjm 所评论的,解决方案是:在根 CMakeLists.txt 中,在add_subdirectory()之前,添加这两行:

set(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}")
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_BINARY_DIR}")

I had a similar problem whem trying to use cmocka lib to create tests.我在尝试使用 cmocka lib 创建测试时遇到了类似的问题。

Even thought CMake found your library with a find_library command like甚至认为 CMake 使用 find_library 命令找到了您的库,例如

find_library(<SOME_VAR> NAMES lib_name PATHS "where/to/search")

you'll still run into this problem.你仍然会遇到这个问题。

Windows execution will not be able to find the .dll. Windows 执行将无法找到 .dll。 You can solve this problem by adding this library stored in right next to your executable.您可以通过添加存储在可执行文件旁边的这个库来解决这个问题。

So if you have something like所以如果你有类似的东西

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

in your CMakeLists.txt file, you'd only have to add在你的 CMakeLists.txt 文件中,你只需要添加

file(COPY ${SOME_VAR}
    DESTINATION ${EXECUTABLE_OUTPUT_PATH})

That's it.就是这样。

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

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