How to organize my files using CMake?

I had a little problem with CMake regarding the organization of my code as part of the solution. I have a habit of organizing my namespace by creating a directory for everyone. For example, if I create something like this:

namespace test { namespace blabla { ... } } 

I would create a directory test and the blabla directory inside it, however CMake does not make them visible in my Visual Studio or Xcode project.

Is there any trick to do this?

+7
source share
4 answers

Try using the source_group command. After calling add_executable add the source_group to structure your project as you wish, for example:

 source_group("test\\blabla" FILES file1.cpp file2.cpp) 
+11
source

To group projects in VS, you can use this method in CMake (after 2.8.3)

 //turn on using solution folders set_property( GLOBAL PROPERTY USE_FOLDERS ON) //add test projects under 1 folder 'Test-projects' FOREACH(TEST ${TESTS_LIST}) add_test(NAME ${TEST} COMMAND $<TARGET_FILE:${TEST}>) set_tests_properties( ${TEST} PROPERTIES TIMEOUT 1) set_property(TARGET ${TEST} PROPERTY FOLDER "Test-projects") ENDFOREACH(TEST) 
+4
source

For Visual Studio: make sure all file names are unique. The compilation result of dir/file.cpp will be obj/file.obj . When the compiler compiles otherdir/file.cpp , the result will be obj/file.obj - the previous object file will be overwritten . This applies to VS 2008 and earlier, and I suspect that this is still in VS 2010.

I also organize the source code like you do. I ended up using the following naming scheme: if the path to the source file is Dir/Subdir/AnotherSubDir/File.cpp , I would name the file Dir/Subdir/AnotherSubdir/DirSubdirAnotherSubdirFile.cpp . Nasty? Yes. But it is superior to a project that will not link, and it is easy to understand what should be the name of the file. I think you could just add a serial number to the file, but I thought it would be uglier. In addition, if you forgot to make the file name unique, the error is not so obvious. Especially when you are tired and your fiance / wife is waiting ...

+2
source

The accepted solution does not work for Xcode with Xcode 6. However, there is a simple way:

  • Remove links to source files from your Xcode project (delete them in the sidebar, then select "Delete Link").
  • Add the root folder back, make sure “Create Group” is checked, and select the desired target (s).

TA-dah! Your files should now match the folder structure in Finder.

-2
source

All Articles