32

I am trying to find a way to get full C++11 support on Eclipse.

In Eclipse Juno I had a lot of new features with __GXX_EXPERIMENTAL_CXX0X__ but I didn't find support for std::threads, std::mutex, etc.

How can I have completion for all C++11 functions?

EDIT : Solution

Go to Project -> Properties -> C/C++ General -> Path and Symbols -> Tab [Symbols]. Add the symbol : __cplusplus with the value 201103L

hichris123
  • 10,145
  • 15
  • 56
  • 70
Jerome
  • 1,225
  • 2
  • 12
  • 23
  • 1
    I have no problems with Eclipse Kepler. But it did require adding quite a lot of symbols to the configuration. What are you finding it doesn't do? – Joe Feb 27 '14 at 16:40
  • This : "std::mutex _myMutex;" produces a "Type 'std::mutex' could not be resolved" error on Eclipse Juno – Jerome Feb 27 '14 at 16:42
  • In the end I used gcc to spit out all it's defined symbols, as my build would have, and then inserted them into eclipse. It spat out a LOT of symbols, but either manually inserting them or writing a little app to convert it to XML for eclipse to import them sorted my C++11 issues. gcc -dM -E - < /dev/null will do it. Though use the actual compiler and compiler flags your build really uses. – Joe Feb 27 '14 at 16:44
  • Note that IDE is not necessary related to a specific toolchain. – Jarod42 Feb 27 '14 at 16:49
  • I don't really get your answer "Joe". How did you manage to solve the C++11 issues in Eclipse using gcc symbols? I don't think that all the symbols given by "gcc -dM -E - < /dev/null" are linked to C++11. – Jerome Feb 27 '14 at 16:52
  • @Joe Interesting. I have been trying to solve this for 3 years... Did that resolve ALL of the problems? The most upvoted [Eclipse CDT C++11/C++0x support](http://stackoverflow.com/q/9131763/341970) didn't help me. And into which XML should those define be added? Could you share your app with us? – Ali Feb 27 '14 at 18:14
  • @Jerk31 Many of those symbols are C++11 related. Regarding your question, how do you mean *"there is no support for std::thread"*? Does your code compile? The IDE sure gives you [false positives](http://stackoverflow.com/q/14131939/341970), but did you try compiling it? Because it probably will, despite the bogus error messages. – Ali Feb 27 '14 at 18:18
  • It compiles yes. I've post a working solution in my edit. It's solved ;) – Jerome Feb 27 '14 at 18:18
  • @Jerk31 Good to know, I will also give it a shot, thanks! – Ali Feb 27 '14 at 18:19
  • @Ali Yes. It sorted out all my problems but then I'm cross-compiling to an embedded platform, so I need to match the Eclipse indexers view of the world to my remote compilers. The "Paths and Symbols" config section of Eclipse lets you import and export settings. That file is a trivial xml file. – Joe Feb 28 '14 at 14:21
  • @Jerk31 The accepted answer and your edit says different things. Which one solved your problem? – Ali Feb 28 '14 at 14:24
  • 1
    @Ali : yes we discussed with Petr Budnik about this subject in a private conversation and we went to the conclusion that the solution I posted in "edit" was the best one. As this conversation gave me the solution I marked it as "accepted answer". – Jerome Mar 07 '14 at 19:12
  • @Jerk31 OK, thanks, I will give it a shot. Unfortunately, I remember having problems with C++03 codes as well... :( – Ali Mar 07 '14 at 19:43

2 Answers2

17

Eclipse works with C+11 support (Indexer and such) just fine: both Juno and Kepler. The thing is that you have to let Eclipse know that you are going to compile with C++11 support. This is done by providing additional flag -std=c++11 to CDT GCC Builtin Compiler Settings in C/C++ -> Build -> Settings -> Discovery [tab] so it will read something like:

${COMMAND} -E -P -v -dD "${INPUTS}" -std=c++11

And that is all you have to do with Kepler/Juno to get C++11 code highlighted correctly.

Note, this is workspace-wide setting. You can do the same on per project basis if you don't want to set it in workspace.

lapk
  • 3,838
  • 1
  • 23
  • 28
  • Still doesn't work for std::thread, std::condition_variable, etc – Jerome Feb 27 '14 at 17:28
  • After you did that, did you refresh and re-index? Because, I'm looking at `std::thread test;` right now and it's properly highlighted and gives hovering help. Might worth trying closing and re-opening the project too... You have the last Juno update, I assume. – lapk Feb 27 '14 at 17:29
  • 1
    Maybe, I should add that I use Eclipse Kepler right now. But it worked in Juno for me too, I am quite sure of it. Oh, and with this you do not need to define `__GXX_EXPERIMENTAL_CXX0X__` symbol... And just to be certain, you have the headers available to Eclipse? I mean, it does find standard `thread` and `mutex` headers in include path? – lapk Feb 27 '14 at 17:40
  • It's just super weird, Eclipse is finding the headers but says "Type 'std::mutex' could not be resolved" even if I create again the project... – Jerome Feb 27 '14 at 17:47
  • Can you open, say, the `mutex` header and see what Eclipse highlighted as active code? The header has a few `#ifdef` on different constants like `__cplusplus`. Highlighting active code sections will tell you what Eclipse thinks about these values... You mentioned you are on Ubuntu. Are you using `gcc`? Which version if I may ask?.. Also, Eclipse requires to hit `Apply` every time you leave a tab. Just double check you have `-std=c++11` for the new project you created in its properties in `CDT GCC Builtin Compiler Settings`. – lapk Feb 27 '14 at 17:57
  • Using gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1. When I open "" I see at the beginning `#if __cplusplus < 201103L # include ` and then `else...` in gray color. Does that mean that my "__cplusplus" variable doesn't fill the requirements? – Jerome Feb 27 '14 at 18:03
  • 3
    No, it's good. Gray color means this is inactive code. It means Eclipse recognizes C++ version no less than C++11 (201103 = 2011, March, which is when committee approved C++11)... `gcc` version is fine too. It must be some glitch in Eclipse indexer, I'd say. Try right clicking on project and `Index -> Freshen All Files` and then `Index -> Rebuild`. – lapk Feb 27 '14 at 18:13
  • I've found a SUPER UGLY solution working !! `#undef __cplusplus #define __cplusplus 201900L`. Thanks for everything :) – Jerome Feb 27 '14 at 18:15
  • Wow, hold on. `__cplusplus` is a standard macro defined by compilers. Adding `-std=c++11` as we discussed makes indexer to assume (so to speak) that compiler defines that macro to `201103L` (among other things) to use C++11. To redefine this macro is a very UGLY and DANGEROUS hack indeed because standard headers and 3rd party libraries might rely on this value to be equal `201103L` exactly. You might not being able to compile after that... There is something wrong with your Eclipse CDT installation. You should not be needing such hack to get the proper indexing... – lapk Feb 27 '14 at 18:30
  • Huh you're right : `/usr/include/c++/4.8/cstdio:119:11: error: '::gets' has not been declared using ::gets;` But the indexer gets good values... Any other ideas? – Jerome Feb 27 '14 at 18:39
  • Let's talk in chat, ok? – lapk Feb 27 '14 at 18:43
  • @lapk i did all this and nothing seems to work, the compiler is still using -std=c++98 flag, I have messed around with this for days. What could be wrong? – Pavel Sep 16 '16 at 04:38
  • @Pavel By "all this" you mean the method described in the post, not in comments? Because comments describe an ugly hack... The way it is described in the post should work. Are you sure your Eclipse installation is not broken? – lapk Sep 19 '16 at 04:00
  • @lapk yes, post, how could it be broken? I have eclipse neon. I tried the eclipse juno as well, and it's still the same :( – Pavel Sep 19 '16 at 05:07
  • @Pavel Just so we have an understanding: these settings are only for code highlighting/indexer and such in editor, not actual compiler flags for compilation. Compilation flags are set elsewhere. – lapk Sep 22 '16 at 04:58
  • @lapk oh, I see, that makes sense. – Pavel Sep 22 '16 at 05:00
  • I found that in some cases, this is not enough, and the C++ Builder dialect also needs to be set to C++11, as in this post https://www.eclipse.org/forums/index.php/t/470456/ – Chris Watts Aug 08 '17 at 09:07
-1

Is there even a compiler which fully supports C++11? If you are on Windows, then I think you should give Microsoft Visual Studio Express Edition 2013 a try. Its C++11 support is not complete but IMO fairly good, and it looks like it will get even better in the near future.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62