How to organise my files using CMake?

17,519

Solution 1

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)

Solution 2

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)

Solution 3

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...

Share:
17,519
lollancf37
Author by

lollancf37

During the day I'm a consultant working .NET consultant. At night I have a strong interest in C++, Computer graphics and AI. I'd like to get better in things like networks, Python and Erlang but struggle to find the time since my plate is already quite full. I enjoy movies with a lot of free violence, all kinds of books from philosophy to SF/F and graphics novel (from every country). I also like skating and talking with interesting people. That's all folks !

Updated on July 22, 2022

Comments

  • lollancf37
    lollancf37 almost 2 years

    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 ?

  • lollancf37
    lollancf37 over 12 years
    It seems to do the trick ! Thank you. However I just get the folder "blabla" and not a folder "test" with blabla on it. Any idea ?
  • lollancf37
    lollancf37 over 12 years
    I get what you are saying but I didn't this problem but I'll keep it in mind since it will most likely happens. Thanks
  • Jörgen Sigvardsson
    Jörgen Sigvardsson over 12 years
    Sorry for being a bit off topic, but I figured I should give you a heads up. Cheers!
  • sakra
    sakra over 12 years
    Nested source groups (using backslashes) work for me with CMake 2.8.5 and Visual Studio 2008 Pro.
  • lollancf37
    lollancf37 over 12 years
    I didn't test yet on vs but Xcode. I'll keep you posted. Thanks again !
  • lollancf37
    lollancf37 over 12 years
    It works great with visual studio but not xcode for some reason. The more I use xcode for C++, the more I think I should move to eclipse, even the link_directories that I set up works fine with VS but not Xcode.
  • Infinite
    Infinite about 9 years
    Small side-note: Visual Studio Express does not support folders that's why USE_FOLDERS has to be set in the first place