bitgregor
bitgregor

Reputation: 1125

Specify how cmake creates visual studio project

I'm setting up cmake for my project and I've set up a testing project for it. When it generates my visual studio 2010 project I want to make it as the project I've had earlier.

How can I exclude some files when I build on windows and others when I build on linux? Like I have WindowWin.cpp and WindowLinux.cpp.


I've tried what you sugested but cant get it working:

#LibProject/src

FILE(GLOB test0_headers $CMakeTest_SOURCE_DIR/LibProject/inc/test.h)
source_group(include0 FILES ${test0_headers})
FILE(GLOB test0_source ${CMakeTest_SOURCE_DIR}/LibProject/src/test.cpp)
source_group(source0 FILES ${test0_source})

FILE(GLOB test1_headers $CMakeTest_SOURCE_DIR/LibProject/inc/test1.h)
source_group(include1 FILES ${test1_headers})
FILE(GLOB test1_source ${CMakeTest_SOURCE_DIR}/LibProject/src/test1.cpp)
source_group(source1 FILES ${test1_source})

include_directories(${test0_headers} ${test1_headers})

add_library(LibProject  ${test0_headers} ${test1_headers} ${test0_source} ${test
1_source})

I kind of got it working now.. only that I want sub folders for headers and source files inside the source group.

set(test_source0 ${CMakeTest_SOURCE_DIR}/LibProject/inc/test.h ${CMakeTest_SOURCE_DIR}/LibProject/src/test.cpp)
source_group(TEST FILES ${test_source0})

set(test_source1 ${CMakeTest_SOURCE_DIR}/LibProject/inc/test2.h ${CMakeTest_SOURCE_DIR}/LibProject/src/test2.cpp)
source_group(TEST2 FILES ${test_source1})

include_directories(${CMakeTest_SOURCE_DIR}/LibProject/inc)
add_library(LibProject  ${test_source0} ${test_source1})

Heres my solution :)

set(test_header
${CMakeTest_SOURCE_DIR}/LibProject/inc/test.h)
set(test_source
${CMakeTest_SOURCE_DIR}/LibProject/src/test.cpp)

source_group(TEST\\Headers FILES ${test_header})
source_group(TEST\\Source FILES ${test_source})

set(test2_header
${CMakeTest_SOURCE_DIR}/LibProject/inc/test2.h)
set(test2_source
${CMakeTest_SOURCE_DIR}/LibProject/src/test2.cpp)
source_group(TEST2\\Headers FILES ${test2_header})
source_group(TEST2\\Source FILES ${test2_source})

include_directories(${CMakeTest_SOURCE_DIR}/LibProject/inc)

add_library(LibProject
${test_header}
${test_source}
${test2_header}
${test2_source})

Upvotes: 8

Views: 9040

Answers (2)

mantler
mantler

Reputation: 889

To group files into a folder in Visual Studio you can use:

# Include files
FILE(GLOB all_headers "include/*.h*")
source_group("include" FILES ${all_headers})

# Source files
FILE(GLOB all_srcs "src/*.cpp")
source_group("source" FILES ${all_srcs})

This will put all of your .h files that are located in the include folder to appear in a folder called include in Visual Studio. Same for your cpp files. You can use this technique to make your own structure in CMake, if that is what you want.

For the purposes of ALL_BUILD and ZERO_CHECK read this answer. To disable the generation of ZERO_CHECK use set(CMAKE_SUPPRESS_REGENERATION true).

To select between OpenGL and DirectX and to setup the appropriate folders, you can use the same technique used above but put them inside a

if(${USE_OPENGL})
    FILE(GLOB ....)
endif()

I think you get the idea.

Upvotes: 7

Nathan Monteleone
Nathan Monteleone

Reputation: 5470

I'll answer the ones I know:

The ALL_BUILD and ZERO_CHECK are used to keep your projects in sync with changes to the CMakeFiles. I don't think there's a way to get rid of those.

To get the .h files where you want them, you need to include them as part of the source. I use something like this:

file(GLOB LocalProjectIncludes "${yourheaderdirectory}/*.h")

Then I append ${LocalProjectIncludes} to the source files for the project. CMake will figure out that they're headers; it won't try to build them.

Upvotes: 4

Related Questions