0

I am creating a visual studio project in C++ using CMake to show how to use a DLL (This is our SDK so the codes are samples for the dll)

What I have at the moment, is to document to users that they need to manually copy the DLL to the debug/release directory of sample application so they can be found by sample applications.

I am wondering how I can add a file to a Visual Studio project in a way that the file is copied to output directory during build process.

the directory structure is as follow

MySDK\
    CMakeLists.txt
    src\
        main.cpp
    sdkDLL\
        MYSDK.DLL

and I am building the project in a build directory, so after I build a project with CMake, I should have a new directory called build in the root directory as follow: MySDK\ build\ MyProject.sln

at this stage there no debug or release folder, so I can not copy the dlls to them using CMake, it should be done using VS as part of the build.

mans
  • 17,104
  • 45
  • 172
  • 321
  • 1
    I do basically this: [https://cmake.org/pipermail/cmake/2012-January/048484.html](https://cmake.org/pipermail/cmake/2012-January/048484.html) – drescherjm Mar 10 '20 at 13:44
  • I suspect the clean option is missing. The MSBuild project generator in CMake would need to generate a [`CopyTask`](https://learn.microsoft.com/en-us/visualstudio/msbuild/copy-task?view=vs-2019). There appears to be no CMake command which does that. – MSalters Mar 10 '20 at 14:05
  • Have you seen the [configure_file()](https://cmake.org/cmake/help/latest/command/configure_file.html) command with the COPYONLY option? – Pesho_T Mar 10 '20 at 14:09
  • @Pesho_T That function doesn't work as it copies files during project creation by CMake, but we need to do this during build process of VS. – mans Mar 10 '20 at 14:14
  • Maybe a custom command with `PRE_BUILD` keyword does this. `add_custom_command(TARGET yourtarget PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different )` – vre Mar 10 '20 at 14:30
  • @vre This seems to work, Can I use a GLOB to copy all files? What about making it work for all targets instead of for only one target? You may put it as an answer and I will accept. – mans Mar 10 '20 at 14:40
  • To copy the contents of a directory use `copy_directory` instead of `copy_if_different` in the command. (See https://cmake.org/cmake/help/latest/manual/cmake.1.html#run-a-command-line-tool) Will add a full answer later. – vre Mar 10 '20 at 14:45
  • 1
    Is something like this [answer](https://stackoverflow.com/a/10672739/3987854) what you are looking for? – Kevin Mar 10 '20 at 15:18

0 Answers0