c++visual-studiocmake

Is it possible to add files to a CMake generated solution folder in Visual Studio?


This question is more or less a warmup of this question:

how to get cmake to add files to msvcs solution tree

It never got a valid answer so I want to repose it slightly different:

Is it possible to use the cmake solution folders that where introduced with cmake 2.8.3 to add files directly to the vs solution? I want to do the cmake equivalent of VS->Solution->Add->Existing Item. So my file will appear in a folder that belongs to the solution and not to a project.

I found examples how the solution folders can be used to group targets into folders with code like this:

set_property( GLOBAL PROPERTY USE_FOLDERS ON)
set_property(TARGET ${TARGET_NAME} PROPERTY FOLDER "Test") 

So can I add a file instead of a target to the folder?


Solution

  • This is probably not possible.

    CMake organizes its assets into projects and targets. The project is what corresponds to Visual Studio's solution file while each target will generate a Visual Studio project inside the respective solution.

    The problem is now that CMake does not allow to add files to CMake projects. In fact, a CMake project is little more than a collection of targets and offers almost no customization options. Hence the USE_FOLDERS option you mentioned can only be used to group VS projects inside a solution, but not single files.

    The closest to what you ask would probably be to add a custom target that holds your files. However, this will still generate a VS project (which also contains a bunch of other stuff besides your files) inside the solution and not a plain folder.