14

Possible Duplicate:
Turn off eclipse errors (that arent really errors)

I'm facing this annoying issue: Eclipse refuses to recognize the std::to_string function, but my program compiles without errors. What am I missing?

According to cppreference the std::to_string function is defined in <string>, so I included it explicitly in the incriminated .cpp file. I also tried this, this and this solutions, with no luck.

Any other suggestions?

EDIT:

I'm using g++ 4.7.2 under Linux.

Community
  • 1
  • 1
Avio
  • 2,700
  • 6
  • 30
  • 50
  • Yes, this is the "master" question: http://stackoverflow.com/questions/13458396/disable-eclipses-error-discovery-c11-false-positives but I was looking for a specific workaround (e.g. the inclusion of an header file somewhere), I'd prefer not to disable Eclipse error checking. – Avio Jan 17 '13 at 15:11

2 Answers2

30

UPDATE: It's been a long time since I posted the original answer and it has become outdated. I double-checked today (Mar 15, 2014): in Eclipse Kepler (Build id 20130614-0229) it is sufficient to

  • add under Project > Properties > C/C++ Build > Settings then on the Tool Settings tab GCC C++ Compiler > Miscellaneous the -std=c++11 flag,

  • then under Window > Preferences > C/C++ > Build > Settings on the Discovery tab chose CDT GCC Built-in Compiler Settings and add the -std=c++11 flag to Command to get compiler specs. On my machine it looks like this after the change:

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

  • clean and rebuild both your project and your index (Project > C/C++ Index > Rebuild) as Eclipse tends to cache error messages and show them even though they are gone after changing the settings.

This works on my machine for sure. If it doesn't on yours, then you might want to give a shot to this: C++11 full support on Eclipse although I am neither sure about the correctness of this approach nor was it necessary to do it on my machine. As of March 7, 2014 users claim that it helped them whereas the above approach didn't.


The original post, now outdated:

It seems like you have run into the common problem with Codan, see my answer here.


It isn't 100% clear how the code compiles. Within Eclipse? Or from command line, properly setting the flags? So just in case:

You are using a C++11 function. Do you pass the -std=c++0x or the -std=c++11 flags to the compiler (assuming gcc)?

You might have to also add __GXX_EXPERIMENTAL_CXX0X__ to your defines (again, assuming gcc) and restart Eclipse.

Community
  • 1
  • 1
Ali
  • 56,466
  • 29
  • 168
  • 265
  • 2
    I'm sorry, I didn't mention that I'm using g++ 4.7.2 under Linux. Yes, I had the `-std=c++0x` flag in the compiler's command line, I also tried with `-std=c++11` and enabling `__GXX_EXPERIMENTAL_CXX0X__`, to no avail. I've looked at your answer, and I suspect I'll end up disabling some of the most annoying errors by hand. Thank you for the answer, +1 for having tried. – Avio Jan 17 '13 at 14:54
  • @Ali: The question says that the project compiles ok, so all your recommendations concerning compile settings are of no avail. He needs an Eclipse upgrade. – Ben Voigt Jan 17 '13 at 14:58
  • @BenVoigt Yes, that's my first guess too. However, it isn't clear how the code compiles. Within Eclipse? Or from command line, properly setting the flags? Or? So I took a shot and told him everything that could possibly lead to errors. But I agree, an Eclipse upgrade and disabling Codan will probably solve his problems. – Ali Jan 17 '13 at 15:28
  • @Avio Please let me know if disabling Codan actually helps. – Ali Jan 17 '13 at 15:29
  • @BenVoigt OK, I updated the answer to clarify why I suggested checking the compiler flag and macro for C++11. Thanks for pointing that out! – Ali Jan 17 '13 at 15:32
  • The code compiles both within Eclipse and from command line (because the makefile is automatically generated by Eclipse). I've disabled the "Function cannot be resolved" checkbox from "Code Analysis" within the workspace preferences. This actually solves the problem, but I'd have rather keep that checkbox on and find a specific workaround for my issue. – Avio Jan 17 '13 at 15:51
  • 2
    @Avio Yes, in general, you should do so, but in case of Eclipse it is a bug in Codan and I don't think you will find a better workaround. I reported that bug and as you can see, nothing has happened :( – Ali Jan 17 '13 at 16:33
  • Ok then, I'll disable that check till better times :) Thank you very much for your help Ali. – Avio Jan 17 '13 at 16:38
  • I am glad it helped! :) And yes, I am also waiting for those better times to come. – Ali Jan 17 '13 at 18:10
  • This worked for me, but ONLY after I did the following: open Project Properties -> C/C++ General -> Preprocessor Include Paths Macros -> Providers [tab]. Select "CDT GCC Built-in Compiler Settings" and use the button 'Move Up' to move it to the top of the list. Click 'Apply' then 'OK'. – MikeyE Dec 30 '15 at 08:01
  • After all this time, and given this question has gained the rank of "famous queston", I'll post what I believe is the solution to this problem. I simply moved away from Eclipse and started using the powerful combo QtCreator + CMake. QtCreator is quite a smart IDE (smarter than Eclipse IMHO) and CMake is a powerful and portable way to generate makefiles. QtCreator seems to respect quite well what CMake tells it to do (no .pro/.pri/.moc involved until you really want to use Qt) so one can choose to have a IDE or not (useful for embedded systems) without having each time to turn to Eclipse. – Avio Feb 16 '16 at 10:24
  • Uh, and in the rare cases when one must make use of a "foreign" library, simply setting the CPATH variable `export CPATH="$CPATH:/opt/libs/myoldlib"` tells to QtCreator to also look into CPATH directories for includes when the CMake project is loaded. – Avio Feb 16 '16 at 10:29
  • I'm getting `cc1plus: error: unrecognized command line option '-std=c++11'` :( I think I might not have c++ 11 even installed... – Katie May 21 '18 at 22:01
  • @KatieSissons I guess you have a really old compiler. Please try `-std=c++0x`. If that does not help, please consider upgrading your compiler. – Ali May 21 '18 at 22:25
10

In my case eclipse believes __cplusplus is defined to 199711L but I'm quite certain that this should be defined to something along the lines of 201103L because the libstdc++ v3 uses

#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else

in most of the new C++11 headers such as <future> and basic_string.h (where the definition of std::to_string is) which is included by <string>. Although when compiling with g++ (Built by MinGW-builds project) 4.8.0 20121225 (experimental) I get absolutely no error. This strange behaviour apparently confuses eclipse and makes it fail to properly phrase the included files.

Defining __cplusplus to something over 201103L before including the C++11 files should fix the bogus eclipse syntax errors such as Symbol 'shared_ptr' could not be resolved.

#undef __cplusplus
#define __cplusplus 201900L

After making the redefinition you'll want to right click on the project Index -> Rebuild & Freshen all files or even better restart eclipse all together.

Edward A
  • 2,291
  • 2
  • 18
  • 31
  • 1
    I've tried your `__cplusplus` solution, with no luck. However, looking at `basic_string.h` I've seen that the `#ifdef` that encompasses all the `to_string` definitions (`#if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \ && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))`) is grayed out. However if I try to copy/paste the same `#ifdef` within my code, it's not grayed out. I've tried to define (again) `__GXX_EXPERIMENTAL_CXX0X__` before everything else, to no avail. – Avio Jan 17 '13 at 16:26
  • @Avio I'm sorry to hear my suggestion didn't work, it did work for me with `shared_ptr, packaged_task etc`. But you may want to try and check `basic_string.h` to see exactly which one is grayed out and `#undef and #define` those to what ever value it wants (not only `__GXX_EXPERIMENTAL_CXX0X__` and even if in the current file is not grayed out). Then rebuild the index, freshen files and restart eclipse. – Edward A Jan 17 '13 at 16:44
  • +1 this worked well. I even added __GXX_EXPERIMENTAL_CXX0X__ and where it used to help, it no longer did it, this solution is what I had to use. std::to_string wasn't working. Using eclipse kepler with mingw64/GCC 4.8.2. –  Nov 22 '13 at 03:09
  • 4
    Eclipse Kepler, C++ general ---> Preprocessor Include Paths, Macro etc ---> Entires ---> GNU C++ ---> CDT User Setting Entires add __cplusplus 201103L – jean Mar 14 '14 at 13:53
  • 2
    @jean Your answer works on Luna: `C++ General` -> `Preprocessor Include Paths` -> in the left pane under the `Entries` tab highlight `GNU C++`, then on the right pane highlight `CDT User Settings`, then click `add` off to the right. In the resulting pop-up, select `#preprocessor Macro`, for `name` use **__cplusplus**, and for `value` use **201103L**. Once complete right click the project under `Project Explorer` -> select `Index` -> `Rebuild`. Then wait, the red line under `std::to_string` will disappear. – Rik Jan 29 '15 at 21:41
  • @Rik Thank you so much for your example. I'm using Eclipse Mars and I had so many red wiggles it was driving me mad. You'd think that this problem should have been sorted out by now. – Smithy Aug 03 '15 at 08:33
  • solves my issue.. Thank man.. – raz Feb 20 '20 at 15:38