0

I have a simple project with the following directory structure.

HelloCMake (directory)
|--CMakeLists.txt
|--main.cpp
|
|--HelloDll (directory)
   |--CMakeLists.txt
   |--hw1.hpp
   |--hw1.cpp

The intent is to create a DLL out of hw1.cpp and call one of its functions from main.cpp, both functions printing their hello world (main or dll) respectively. HelloCMake\CMakeLists.txt has:

cmake_minimum_required(VERSION 3.6)
project (HelloDll)
add_subdirectory (HelloDll)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
# the above facilitates running / debugging the program from VS without install

add_executable (hw1 main.cpp)
target_link_libraries (hw1 hw1Dll)

Now HelloDll\CMakeLists.txt:

add_library (hw1Dll SHARED hw1.cpp hw1.hpp)
set_target_properties (hw1Dll PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE)

Let's say I modify hw1.cpp to print from 2 dll hello world and build, and do Ctrl+F5 to run the program, it prints from 1 dll hello world, the previous version. I can see that the latest dll was not copied. If I perform another build, it copies the latest dll and I see the right output.

I have tried solutions from other posts to no avail. The current one is from how do I make cmake output into a 'bin' dir?. What am I missing? I am sure it's something embarrassingly simple. Thanks.

Community
  • 1
  • 1
NameRakes
  • 463
  • 6
  • 12
  • 1
    I solve this by using a common bin folder where all executables / dlls for a CMake generated project are located. This is /bin relative to the root CMakeLists.txt. And yes this is under Visual Studio. So there is no need to copy the dlls since they share the same output folder as the executables that are generated for the whole project. – drescherjm Sep 29 '16 at 22:25
  • ***What am I missing?*** Do that only in the root CMakeLists.txt. and make sure that the other CMakeLists.txt files do not have a `project()` in them. – drescherjm Sep 29 '16 at 22:35
  • 3
    You set *CMAKE_LIBRARY_OUTPUT_DIRECTORY* **after** processing directory with library, so the variable doesn't affect the library. – Tsyvarev Sep 29 '16 at 22:56
  • Good point. I set `CMAKE_LIBRARY_OUTPUT_DIRECTORY` in the root CMakeLists.txt before any add_subdirectory() commands. – drescherjm Sep 29 '16 at 23:09
  • Thanks for the quick diagnosis of the problem `@Tsyvarev`, now it works. – NameRakes Sep 29 '16 at 23:25

0 Answers0