CMake, Exe cant find DLL

2019-03-05 07:03发布

so im trying to setup a project, on windows, with cmake

this is what my project structure looks like:

  • GameEngine
    • .git
    • build
    • include
    • source
    • testing
    • 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. So it should depend on the GameEngine.dll and GameEngine.lib (on windows).

my top level CMakesLists.txt contain:

    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:

    project(GameEngineLib)

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

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


and the CMakeLists.txt in the "testing" directory contain:

    project(Testing)

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

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


This all works fine on 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.
and the executable already know where to find the .so file, so i can just run without any errors.


The problem comes when i build on windows with visual studio.
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.


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.


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.


isn't there a way to tell cmake to automatically copy the GameEngineLib.dll to the Tester.exe's folder?

1条回答
一纸荒年 Trace。
2楼-- · 2019-03-05 07:41

I had a similar problem whem trying to use cmocka lib to create tests.

Even thought CMake found your library with a find_library command like

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. 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

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

That's it.

查看更多
登录 后发表回答