Building of executable and shared library with cmake, runtimelinker does not find dll

前端 未结 4 1340
梦毁少年i
梦毁少年i 2021-01-04 05:14

I am working with gcc(cygwin), gnu make, windows 7 and cmake.

my cmake testprojekt has the following structure

rootdir
|-- App
|   |-- app.cpp
|   +-         


        
4条回答
  •  天命终不由人
    2021-01-04 05:28

    I discovered (what I believe to be) quite a nice way of handling this. It follows the approach of adding the .dll to the same directory as the .exe. You can do it in CMake like so:

    if (WIN32)
    # copy the .dll file to the same folder as the executable
    add_custom_command(
        TARGET  POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        $>
        $)
    endif()
    

    where app-target is the name of the application or library you're building (created through add_executable or add_library) and lib-target is the imported library brought in with find_package.

    # full example
    cmake_minimum_required(VERSION 3.14)
    
    project(my-app-project VERSION 0.0.1 LANGUAGES CXX)
    
    find_package(useful-library REQUIRED)
    
    add_executable(my-application main.cpp)
    
    target_link_libraries(my-application PUBLIC useful-library::useful-library)
    
    if (WIN32)
    # copy the .dll file to the same folder as the executable
    add_custom_command(
        TARGET my-application POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        $
        $)
    endif()
    

提交回复
热议问题