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 a开发者_开发技巧ppear in my Visual studio or Xcode project.
Is there a trick to get it done ?
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)
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)
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...
The accepted solution does not work for Xcode as of Xcode 6. However, there is a simple workaround:
- Delete the references to source files from your Xcode project (delete them in the sidebar, then pick "Remove Reference").
- 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.
精彩评论