cmake and eclipse: default include paths?

2019-04-22 15:29发布

问题:

I have a project that builds with CMake system, and I like to import it in Eclipse. However, when I generate eclipse project files with 'cmake -G "Eclipse CDT4 - Unix Makefiles"' there are no default include paths in Eclipse project(such as /usr/include' or the gcc path for standard headers).

How to fix that in most right way?

System: linux gcc 4.3.3 cmake 2.6.4 eclipse 3.5.1

回答1:

You have to go to the project properties (right button over the project), "C/C++ include paths and symbols" and add them here as "external include paths".



回答2:

In your CMakeLists.txt try adding the following two lines:

find_path(STDIO_INCLUDE_PATH stdio.h)
include_directories("${STDIO_INCLUDE_PATH}/dummy/../")

The first line looks up the path for stdio.h, which is located in /usr/include on my system. The second adds this folder to the CMake include path. The /dummy/../ part was added to trick CMake into adding the folder (it wouldn't otherwise), and will eventually get stripped off.

This works for me with CMake 2.8.8 and Eclipse 3.7.2.



回答3:

try

cmake -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_ECLIPSE_VERSION=<put here your eclipse version>  ../src

for my case was

cmake -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_ECLIPSE_VERSION=4.2  ../src

I hope it helps



回答4:

You can parse the list of directories directly from the compiler, so you know you are getting the exact same set that CMake is using (assuming your CMake setup is using the same compiler). Then, split them into a list with separate_arguments, and add them with the include_directories command.

execute_process(
    COMMAND echo
    COMMAND bash -c "g++ -E -Wp,-v - 2>&1"
    COMMAND awk "/^#include .* starts here:$/,/^End of search list/ { if ($0 ~ /^ /) { print } }"
  OUTPUT_VARIABLE SYS_INCLUDES_OUT)
separate_arguments(SYS_INCLUDES UNIX_COMMAND ${SYS_INCLUDES_OUT}) # create a list
include_directories(${SYS_INCLUDES}) # add list to includes

They will then be included in the generated Eclipse project.