Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake - support of Visual Studio filters

I have a cmake file that generate a solution with several sub projects, but I wish a "filter" (VS specific feature) to group all my thirdparty libraries together.

An example, for now I use the ZLIB library, it appear as a project, I use the following:

add_subdirectory(zlib)

To add such filter, I have try the following :

add_subdirectory(zlib)
FILE(GLOB_RECURSE ZLIB_SOURCE "zlib/*")
SOURCE_GROUP("THIRDPARTY" FILES ${ZLIB_SOURCE})

In this example I wish to put the "zlib" project into a "THIRDPARTY" filter.

But nothing is changed in my solution ! I use VS2017 and cmake 3.8

Any idea ?

like image 986
CDZ Avatar asked Mar 09 '23 01:03

CDZ


1 Answers

There are two ways of separating all your own and third-party code of an application in the solution explorer.

  1. Separate multiple Projects and put them into folders which are on top-level.
    Do the following:

    • put this on top of your main CMakeLists.txt
      set_property(GLOBAL PROPERTY USE_FOLDERS ON)
    • after defining your targets add this extra bit
      add_executable(MyLib .....) set_target_properties(MyLib PROPERTIES FOLDER "Libraries");

      Your project-explorer will then look like this:

      enter image description here
      credit goes to these guys: http://cmake.3232098.n2.nabble.com/Solution-folders-td6043529.html

  2. To separate multiple source-files inside a project you can do the following:

    • collect all files of a module with:
      set(VARIABLE_NAME src/module/fileName1.cpp src/module/fileName2.cpp)
    • make it appear within a filter:
      source_group("Source Files\\module" FILES ${VARIABLE_NAME})
    • group all previously generated filters together:
      set(SOURCE_FILES "${VARIABLE_NAME}")
    • finally make everything appear inside the project explorer:
      add_executable(projectName "${SOURCE_FILES}")

The above works for me under CMake 3.6 and Visual Studio 2015, so it should also work with VS2017 and Cmake 3.8.
It looks like this when finished for all files of the project:

project explorer

Since you are developing with VS here's another hint that I think is very useful: you can define VS's startup project by the following command. This way you don't have to change anything in VS after remaking the project with CMake.
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ProjectName)

like image 124
simmue Avatar answered Mar 16 '23 21:03

simmue