lollancf37
lollancf37

Reputation: 1125

How to organise my files using CMake?

I am having a bit of a problem with CMake regarding the organisation of my code within a solution. I have for an habit to organise my namespace by creating a directory for each. For example if I create something like this :

namespace test { namespace blabla  { ... } }

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

Is there a trick to get it done ?

Upvotes: 8

Views: 19510

Answers (4)

sakra
sakra

Reputation: 65791

Try using the source_group command. After the call to add_executable add source_group statements to structure your project as you wish, e.g.:

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

Upvotes: 14

omikun
omikun

Reputation: 273

The accepted solution does not work for Xcode as of Xcode 6. However, there is a simple workaround:

  1. Delete the references to source files from your Xcode project (delete them in the sidebar, then pick "Remove Reference").
  2. Add the root folder(s) back, make sure "Create Group" is checked, and select the desired target(s).

Tada! Now your files should match the folder structure in Finder.

Upvotes: -2

Andrew
Andrew

Reputation: 51

For grouping projects in VS you could use this way in CMake (ver 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)

Upvotes: 5

J&#246;rgen Sigvardsson
J&#246;rgen Sigvardsson

Reputation: 4887

For Visual Studio: Make sure that all file names are unique. The result of compiling 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 is the case in VS 2008 and earlier versions, and I suspect it's still the case in VS 2010.

I too organise source code the way you do. I ended up using the following naming scheme: if the path to the source file would be Dir/Subdir/AnotherSubDir/File.cpp, then I'd name the file Dir/Subdir/AnotherSubdir/DirSubdirAnotherSubdirFile.cpp. Ugly? Yes. But it beats a project that won't link, and it's easy to figure out what the file name should be. I guess you could just append a sequence number on the file, but I thought it would be uglier. Also, if you forget to make the file name unique, the error isn't all that obvious to spot. Especially when you're tired, and your fiance/wife is waiting...

Upvotes: 2

Related Questions