Tik0
Tik0

Reputation: 2699

CMake generated c++ project with system and standard includes

Regarding the title I found a lot of discussions but unfortunately no proper/universal answer. For Eclipse CDT one could set the includes globally, but if your compiler changes you have to do it again. Thus, I wrote the following working minimal example of a CMakeFile.txt to set the includes, which are used by the compiler.

# Check wheter required CMake Version is installed
cmake_minimum_required(VERSION 2.8.7 FATAL_ERROR)

# Set the project name to the name of the folder
string (REGEX MATCH "[^/]+$" PROJECT_NAME "${CMAKE_CURRENT_BINARY_DIR}")
message (STATUS "Set PROJECT_NAME to ${PROJECT_NAME}")
project ("${PROJECT_NAME}")

# Grep the standard include paths of the c++ compiler
execute_process(COMMAND echo COMMAND ${CMAKE_CXX_COMPILER} -Wp,-v -x c++ - -fsyntax-only ERROR_VARIABLE GXX_OUTPUT)
set(ENV{GXX_OUTPUT} ${GXX_OUTPUT})
execute_process(COMMAND echo ${GXX_OUTPUT} COMMAND grep "^\ " OUTPUT_VARIABLE GXX_INCLUDES)

# Add directories to the end of this directory's include paths
include_directories(
    ${GXX_INCLUDES}
)

# Defines executable name and the required sourcefiles
add_executable("${PROJECT_NAME}" main.cpp)

The greping of the includes is some pain in the a**, but it works. Another point is, that it does not work for cmake above cmake 2.8.7 concerning this bug http://public.kitware.com/Bug/view.php?id=15211 . So, I want to know if anyone have a better way to set the system includes?

Upvotes: 0

Views: 261

Answers (1)

Tik0
Tik0

Reputation: 2699

I found a way to solve it for even higher versions of cmake than cmake 2.8.7. The point is, I have to work with lists seperated by ;. As zaufi mentioned, there is of course the build in possibility to add the standard includes, but this do only work with you standard environment, not with e.g. cross compiler environments.

So here is the working CMakeLists.txt:

# Check wheter required CMake Version is installed
cmake_minimum_required(VERSION 2.8.7 FATAL_ERROR)

# Set the project name to the name of the folder
string (REGEX MATCH "[^/]+$" PROJECT_NAME "${CMAKE_CURRENT_BINARY_DIR}")
message (STATUS "Set PROJECT_NAME to ${PROJECT_NAME}")
project ("${PROJECT_NAME}")

# Grep the standard include paths of the c++ compiler
execute_process(COMMAND echo COMMAND ${CMAKE_CXX_COMPILER} -Wp,-v -x c++ - -fsyntax-only ERROR_VARIABLE GXX_OUTPUT)
set(ENV{GXX_OUTPUT} ${GXX_OUTPUT})
execute_process(COMMAND echo ${GXX_OUTPUT} COMMAND grep "^\ " COMMAND sed "s#\ ##g" COMMAND tr "\n" "\\;" OUTPUT_VARIABLE GXX_INCLUDES)

# Add directories to the end of this directory's include paths
include_directories(
    ${GXX_INCLUDES}
)

# Defines executable name and the required sourcefiles
add_executable("${PROJECT_NAME}" main.cpp)

Upvotes: 1

Related Questions