How to add CMake INSTALL project automatically to build configurations - visual-studio-2013

I'm trying to deploy a couple of .ini files to the binary directory when compiling, using CMake. I did this through the following lines (typed by hand; feel free to edit if you find a typo)
FILE(GLOB INI_FILES "${CMAKE_CURRENT_SOURCE_DIR}/../misc/*.ini")
INSTALL(FILES ${INI_FILES} CONFIGURATIONS Debug DESTINATION ${CMAKE_BINARY_DIR}/bin/Debug)
INSTALL(FILES ${INI_FILES} CONFIGURATIONS Release DESTINATION ${CMAKE_BINARY_DIR}/bin/Release)
This properly generates an INSTALL project in my Visual Studio solution which works if 'compiled' manually. However, the project is not part of the Debug and Release build configurations of the Solution, meaning it won't be executed when compiling the solution. Is there a way to add the project manually to the build configurations in CMake?
Or is this not intended and I need to call CMake differently?
I'm Using CMake 3.0.2 with Visual Studio 2013.
I'm generating my Visual Studio solution like this:
"%VS120COMNTOOLS%..\..\VC\vcvarsall" amd64
cmake .. -G "Visual Studio 12 2013 Win64"

Yes, you can add a custom command to copy the .ini files before you build the project. Assuming your build target is named my_target, you can do something like this:
FILE(GLOB INI_FILES "${CMAKE_CURRENT_SOURCE_DIR}/../misc/*.ini")
add_executable(my_target test.cpp)
foreach(FN ${INI_FILES})
add_custom_command(TARGET my_target
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "${FN}" "${CMAKE_CURRENT_BINARY_DIR}")
endforeach()
It basically uses cmake to copy the .ini files individually over to the build directory.

Related

How to commit a project to SVN which has absolute paths from cmake build? [duplicate]

I am able to CMake build this HelloWorld example project using cmakelists.txt file and generate a visual studio project.
project(helloworld LANGUAGES C CXX)
cmake_minimum_required(VERSION 3.5)
find_package(Idlpp-cxx REQUIRED)
if (NOT TARGET CycloneDDS-CXX::ddscxx)
find_package(CycloneDDS-CXX REQUIRED)
endif()
# Convenience function, provided by the Idlpp-cxx that generates a CMake
# target for the given IDL file. The function calls Idlcpp-cxx to generate
# source files and compiles them into a library.
idl_ddscxx_generate(ddscxxHelloWorldData_lib "HelloWorldData.idl")
add_executable(ddscxxHelloworldPublisher publisher.cpp)
add_executable(ddscxxHelloworldSubscriber subscriber.cpp)
# Link both executables to idl data type library and ddscxx.
target_link_libraries(ddscxxHelloworldPublisher ddscxxHelloWorldData_lib CycloneDDS-CXX::ddscxx)
target_link_libraries(ddscxxHelloworldSubscriber ddscxxHelloWorldData_lib CycloneDDS-CXX::ddscxx)
set_property(TARGET ddscxxHelloworldPublisher PROPERTY CXX_STANDARD 11)
set_property(TARGET ddscxxHelloworldSubscriber PROPERTY CXX_STANDARD 11)
I need to create the same project without cmakelists.txt and CMake
How to do this only using visual studio? where to define those commands in CMakelists.txt in visual studio if I create an empty c++ project
I have tried this making an empty project.
I don't know how to idl_ddscxx_generate and target_link_libraries perform in VS....
idl_ddscxx_generate has to run if IDL file has changed
target_link_libraries is required if I added new source files to the project....
Make a new, empty Visual Studio project.
Copy all source files except the CMake files.
Do whatever you do in a Visual Studio project. Add files, targets, dependecies, … If you are not sure, look up what is written in the CMakeLists.txt file.
Delete all CMake files in your original project and copy your Visual Studio project files.
Add these changes (deleted CMake files, added VS project files) to your Subversion repository, maybe do this in a branch that others can test it, report back, and improve the change. Once done, merge the branch.
Probably, add step 0.: Learn how Visual Studio organizes its project. Make a tutorial, take some training.
Remark: Whatever your problem is with CMake, you missed something. But you can find this out later and revert your changes and pick up CMake up again.

Select MSVS build configuration when generating a buildsystem with CMake

I have a multiplatform CMake project, and occasionally I have to build it manually for Windows. I generate a buildsystem like this:
cmake -DCMAKE_BUILD_TYPE=Release -G"Visual Studio 16 2019" -A x64 ../path/to/source
Then I open *sln file and press F7 to build. It runs for 40 minutes, and after that I understand that I didn't select proper configuration in the combobox. It's annoying! When command line option was -DCMAKE_BUILD_TYPE=Release, but combobox was set to Debug, build fails after spending a decent time.
Is it possible to generate an MSVS project with build configuration selected from command line?
Note that I'm aware of msbuild command and it's -p:Configuration=xxxxx flag. The question is about cases when for some reason you need to build from Visual Studio's GUI.
Changing the selected configuration for the GUI is not possible with CMake at this moment.
The main reason for this is that this information is stored in the .suo file generated by Visual Studio. CMake itself only generates the project and solution files, while the .suo file will be generated by Visual Studio itself.
Alternatively, use CMake's command line build option for this. After configuring your project and generating the VS .sln file from CMake as usual, simply run:
cmake --build <path_to_build_directory> --config Release
This works independently of the selected generator and is the most reliable way of building CMake projects.

Generate Visual Studio Project File to build a library with qmake syntax and where to find the project file?

I have an old script to create Visual Studio Project Files to build libraries. The syntax is
cd Kernel
cd core
qmake -t vclib
cd ccMigration
qmake -t vclib
cd ..
and so on for all of the different directories.
I understand why we use qmake and vclib, but what I am not sure is what is the use of -t in this line:
qmake -t vclib
Looking at this : http://doc.qt.io/archives/qt-5.5/qmake-platform-notes.html it says:
Creating Visual Studio Project Files This section describes how to
import an existing qmake project into Visual Studio. qmake is able to
take a project file and create a Visual Studio project that contains
all the necessary information required by the development environment.
This is achieved by setting the qmake project template to either vcapp
(for application projects) or vclib (for library projects).
This can also be set using a command line option, for example:
qmake -tp vc
Why do they use -tp and is the old script syntax correct by using -t?
After I run the script it takes about 1/2 seconds and where can I find the project files for Visual Studio?
Finally, When I open the project files how can I build a library using Visual Studio? Can someone point me to a place where I can read some material?
Thank you for all the help!
Edit: I ran the script and it created .vcproj files in the directories that qmake was run. Example: In folder Kernel -> core it created ccCore.vcproj project file. When I open the file in Visual Studio 2017 it wanted me to perform a One-way upgrade that will automatically make functional changes to the project in order to open it. Is this okay?

How to debug cmake target after install

I have a cross-platform project which uses cmake in order to generate Visual Studio solution files. The project has external dependencies (.dlls, resources etc) and the only place where the executable can be run is the installation directory. In that directory I have access all the resources, plugins, translations etc. I can install the project both in debug or release in that directory.
How can I debug a project in the installed location?
There are two problems with this case:
Sometimes I may debug the main application (Main.exe) (a target in cmake project)
Sometimes I may debug some plugins that Main.exe loads when started (I have a different cmake project for the plugins)
Is there a clean way of doing this in Visual C++ without actually create some custom project that is configured to start each time the Main.exe from the installed location? ("C:\Program Files\MainProject\Main.exe" )
Thank you,
Iulian
I managed to do it by 'configuring' with cmake a .user file for the specified project.
The only thing that the user needs to do is to use a template like in this bitbucket project.
In the project you can find a template file.
The cmake script command needed is:
CONFIGURE_FILE(
"${PROJECT_SOURCE_DIR}/scripts/windows/VS201x_Template.vcxproj.user.in"
"${PROJECT_BINARY_DIR}/INSTALL.vcxproj.user"
)
If you need a custom .user file you can always do it manually by saving the generated Visual Studio and creating a template from it similarly to the above example.

How do I get Visual Studio to output new files in a different directory (ideally the source directory from a out of source cmake build)

I am in the process of adding cmake support to a project and noticed that, after generating an out of source Visual Studio project, adding new files through Visual Studio puts them in the Build directory, and not in the Source directory. This is suboptimal.
Is there any way to change where VS adds new files, and can I set this in a CMakeLists.txt file?
You can choose the ouput directory for CMake like this:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${MY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MY_DIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${MY_DIR})
For Visual Studio projects use this:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${MY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${MY_DIR})

Resources