Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse indexer proper c++11 syntax highlighting when generating projects with cmake

I know you can enable proper syntax highlighting with the GXX_EXPERIMENTAL hack described here: Eclipse CDT indexer does not know C++11 containers

But i think, when generating projects with cmake, one should never need to touch the project settings at all.

So. Is there a simpler solution?

like image 790
user1283078 Avatar asked Aug 21 '13 13:08

user1283078


3 Answers

The answer is pretty simple.

The eclipse cdt generator ignores the definitions added with add_definitions(...) when parsing the symbols. Instead it uses the CMAKE_CXX_COMPILER_ARG1. So all you have to do is: Add -DCMAKE_CXX_COMPILER_ARG1=-std=c++11 when invoking cmake

Generating project files from commandline:

cmake ../../src -G"Eclipse CDT4 - MinGW Makefiles" -DCMAKE_ECLIPSE_VERSION=4.2 -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER_ARG1=-std=c++11

Generating projects from cmake gui:

- Select source and build directory.
- now BEFORE hitting configure or generate. Press "Add Entry" and add a new entry. Name:CMAKE_CXX_COMPILER_ARG1 Type:STRING Value:-std=c++11
- press Generate and create the Eclipse project

It is important to set the CMAKE_CXX_COMPILER_ARG1 upfront before hitting configure or generate the first time!

That´s it. The project will be generated with correct symbols. indexer, syntax highlighting and autocompletion should work as intended without changing any project settings by hand.

like image 87
user1283078 Avatar answered Nov 13 '22 20:11

user1283078


As already mentioned, the invocation of the project generators runs before the parsing of the CMakeLists.txt. Thus, any definitions inside of CMakeLists.txt don't have any effect on the generated project files.

In case of eclipse-project generation, the capabilities of the compiler are requested inside CMakeExtraGeneratorDetermineCompilerMacrosAndIncludeDirs.cmake. In line 23 the variable CMAKE_CXX_FLAGS is parsed which is evaluated in line 30. Especially this CMAKE_CXX_FLAGS variable can only be set while invoking cmake from command line.

Recommodation of proper cmake invokation: cmake -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_CXX_FLAGS="-std=c++11" (or replace c++11 with eg. c++14 or any other standard you like)

Hint: The result from evaluating line 30 can be seen from running the following command as an example: touch /tmp/dummy; /usr/bin/c++ -v -E -x c++ -std=c++11 -dD /tmp/dummy. It outputs all the defines from the compiler which are parsed into the eclipse-project:

...
#define __STDC__ 1
#define __cplusplus 201103L
...
like image 4
Tik0 Avatar answered Nov 13 '22 20:11

Tik0


In version 3.1 of cmake a new variable CMAKE_CXX_STANDARD was introduced that can activate C++11 support. Try something like this:

cmake ../../src -G "Eclipse CDT4 - Unix Makefiles" -DCMAKE_CXX_STANDARD=11

This seems to carry through to the generated Eclipse project. I tried it with verison 3.2.0-rc2 and Eclipse recognized C++11 features like std::shared_ptr<>.

Some documentation links:

  • CMAKE_CXX_STANDARD
  • Optional Compile Features (read towards the bottom of the section)
like image 3
Ry Lowry Avatar answered Nov 13 '22 20:11

Ry Lowry