[CMake] dll not found in test app for library within visual studio solution (Windows)

Petr Kmoch petr.kmoch at gmail.com
Mon Jun 25 12:15:15 EDT 2012


Hi Benjamin.

What you're dealing with is the runtime not finding the DLL, and cmake
has little control over that. One solution would be to set both
library and the executable to build in the same directory (using
set_property(TARGET trgt PROPERTY RUNTIME_OUTPUT_DIRECTORY whatever).

Two other solutions exist, both VS-specific Open the executable's
Property dialog and go to the Debugging page. There, you can either:
1) set working directory to that where the library is built
or
2) modify the PATH for the executable, like this:
PATH=path_to_library;$(PATH)
(entered into the Environment field). Also make sure Merge Environment is Yes.

Petr

On Mon, Jun 25, 2012 at 5:48 PM, Benjamin Kurz
<benjamin.kurz at bioskill.com> wrote:
> Hi there,
>
> I have a small project setup, it contains one library and one small test
> application, that should use the library. Basically very similar to the
> basic example on the cmake website.
>
> the structure is still pretty easy:
> Project (called ProjectBridge)
>  |
>  |-- Library (called Bridge)
>  |
>  |-- testApp
>
>
> I have 3 CMakeLists.txt one in the root directory, one in the Library
> directory, and one in the TestApp. I add them at the end of this mail.
> Now I create a Visual Studio solution with cmake and build the project. It
> builds fine. But if I want to run the TestApp it cannot find the dll of the
> Library. Of couse it works if I copy the dll by hand into the
> TestApp/Release folder, but I want it to work out of the box, without the
> "install" project necessary to run first too (where I could easily copy the
> dll to the appropriate place).
>
> Although the project settings of visual studio of the TestApp project,
> "additional libary paths" contains "...<build_directory_of_project>/Bridge"
> and "...<build_directory_of_project>/Bridge/$(Configuration)", it does not
> find the Bridge.dll.
>
> The responsible line for this is in TestApp/CMakeLists.txt. At least I
> assume it is.
> "link_directories (${ProjectBridge_BINARY_DIR}/Bridge)"
>
> If wanted or necessary, I can also easily make a project package and attach
> it.
>
> Thanks for any help.
>
> Best regards
> Benjamin
>
> System: Win7 64-bit, CMake 2.8.8, Visual Studio 2010 Express
>
> ######################
> CMakeLists.txt (Project)
> ######################
> cmake_minimum_required (VERSION 2.8)
> project (ProjectBridge)
>
> add_subdirectory (Bridge)
> add_subdirectory (TestApp)
>
> #####################
> CMakeLists.txt(testApp)
> #####################
> include_directories (${ProjectBridge_SOURCE_DIR}/Bridge)
> include_directories (${CMAKE_INSTALL_PREFIX}/include)
>
> link_directories (${ProjectBridge_BINARY_DIR}/Bridge)
> link_directories (${CMAKE_INSTALL_PREFIX}/bin)
>
> add_executable (testApp main.cpp)
>
> target_link_libraries (testApp Bridge)
>
> #####################
> CMakeLists.txt(Bridge)
> #####################
> cmake_minimum_required (VERSION 2.8)
> project (Bridge)
>
> IF(MSVC_VERSION EQUAL 1400 OR MSVC_VERSION GREATER 1400)
>  ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE
> -D_CRT_SECURE_NO_WARNINGS)
>  ADD_DEFINITIONS(-D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS)
> ENDIF(MSVC_VERSION EQUAL 1400 OR MSVC_VERSION GREATER 1400)
>
> SET( Bridge_MAJOR_VERSION 0 )
> SET( Bridge_MINOR_VERSION 1 )
> SET( Bridge_BUILD_VERSION 1 )
>
> SET( Bridge_FULL_VERSION
> ${Bridge_MAJOR_VERSION}.${Bridge_MINOR_VERSION}.${Bridge_BUILD_VERSION} )
>
>
> FILE( GLOB Bridge_HDRS RELATIVE ${Bridge_SOURCE_DIR} *.h )
> set(Bridge_HDRS "${Bridge_HDRS}")
>
> FILE( GLOB Bridge_SRCS RELATIVE ${Bridge_SOURCE_DIR} *.cpp )
> set(Bridge_SRCS "${Bridge_SRCS}")
>
> include (GenerateExportHeader)
>
> FOREACH( src ${Bridge_SRCS} )
>  LIST( APPEND Bridge_SRCS_global ${CMAKE_CURRENT_SOURCE_DIR}/${src} )
> ENDFOREACH( src )
>
> FOREACH( incl ${Bridge_HDRS} )
>  LIST( APPEND Bridge_HDRS_global ${CMAKE_CURRENT_SOURCE_DIR}/${incl} )
> ENDFOREACH( incl )
>
> SET(CMAKE_INSTALL_PREFIX "install" CACHE PATH "Bridge install prefix")
>
> # make sure symbols are exported.
> IF (WIN32)
> SET( Bridge_COMPILE_FLAGS "-DBRIDGE_EXPORTS" )
> ENDIF (WIN32)
>
> INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR} )
> INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} )
>
> add_compiler_export_flags()
>
> # Create build files.
> ADD_LIBRARY(Bridge SHARED
>                ${Bridge_SRCS_global}
>                ${Bridge_HDRS_global} ${PROJECT_BINARY_DIR}/Bridge_export.h)
>
> TARGET_LINK_LIBRARIES( Bridge ${requiredLibs} ${optionalLibs} )
>
> GENERATE_EXPORT_HEADER(Bridge
>            BASE_NAME Bridge
>            EXPORT_MACRO_NAME BRIDGE_EXPORT
>            EXPORT_FILE_NAME Bridge_export.h
>            STATIC_DEFINE Bridge_BUILT_AS_STATIC
> )
>
> install(FILES Bridge.h ${PROJECT_BINARY_DIR}/Bridge_export.h DESTINATION
> ${CMAKE_INSTALL_PREFIX}/include  )
>
> INSTALL( TARGETS Bridge
>         LIBRARY DESTINATION lib
>         RUNTIME DESTINATION bin
>         ARCHIVE DESTINATION lib )
>
>
>
>
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake


More information about the CMake mailing list