Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

  • it creates a ALL_BUILD and ZERO_CHECK project that I dont want.
  • it puts the .h files into the External Dependencies folder. I want a Include Files folder where all the .h files goes.
  • I also want to group some files under different filters. Like in my core lib I want to group all files related to Maths in a folder and all files related to Event management in another.
  • On the filesystem it puts the project files inside /Lib/src. Probably cause I have it organized that in the code folder, but I dont want that for the project files.
  • I want to set up different configuration so I have DebugOpenGL, DebugDirectX, ReleaseOpenGL, ReleaseDirectX and then setting a flag USE_OPENGL or USE_DIRECTX for the two types of configurations.

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})
like image 960
bitgregor Avatar asked Nov 29 '11 18:11

bitgregor


People also ask

How does CMake work with Visual Studio?

Visual Studio adds CMake items to the Project menu, with commands for viewing and editing CMake scripts. The Solution Explorer displays the folder structure and files. Visual Studio runs CMake and generates the CMake cache file ( CMakeCache. txt ) for the default configuration.

How does CMake generate Makefile?

CMake generates a Unix makefile by default when run from the command line in a Unix-like environment. Of course, you can generate makefiles explicitly using the -G option. When generating a makefile, you should also define the CMAKE_BUILD_TYPE variable.


2 Answers

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.

like image 168
mantler Avatar answered Oct 19 '22 08:10

mantler


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.

like image 24
Nathan Monteleone Avatar answered Oct 19 '22 06:10

Nathan Monteleone