52

I have the following code in eclipse for c++ and it's underlining string and cout and saying could not be resolved.

#include <string>
#include <iostream>

using namespace std;


int main()
{
    string s;
    s = "hello world";
    cout << s;
    return 0;

}

Anyone know why?

edit: screenshot

Edit: I have found a solution thanks everyone (see answers).

enter image description here

Vanuan
  • 31,770
  • 10
  • 98
  • 102
124697
  • 22,097
  • 68
  • 188
  • 315
  • But it compiles and works OK, right? I have had this issue myself and I think it's related to eclipse being unable to find the include directory for the toolchain you are using. Check the settings. – trojanfoe Oct 26 '11 at 15:31
  • Related question http://stackoverflow.com/questions/5977542/eclipse-cdt-unresolved-inclusion-of-stl-header – Vanuan Feb 07 '12 at 19:42
  • Check this out, I think the answer is not that complicated : http://stackoverflow.com/questions/7433448/eclipse-cdt-symbol-null-could-not-be-resolved – Mohi65 Jun 03 '13 at 08:56
  • another possible reason: I forgot install g++. For example in Fedora25, `dnf install gcc-c++-6.3.1-1.fc25.x86_64 libstdc++-devel-6.3.1-1.fc25.x86_64`. Then rebuild index. –  Apr 21 '17 at 05:28
  • After following C++ tutorial on youtube, I found you needed the following code: "using namespace std; " , clean project , then recompile and run. – unixcreeper Jan 12 '18 at 14:03
  • Fixed by a combination of things my end - (1) convert to C++ project, (2) selecting toolchain (3) stl-unresolved paths fix (follow this - https://stackoverflow.com/a/7663879/4361073) – parasrish Mar 15 '18 at 04:01
  • Does this answer your question? [How to solve "Unresolved inclusion: " in a C++ file in Eclipse CDT?](https://stackoverflow.com/questions/10373788/how-to-solve-unresolved-inclusion-iostream-in-a-c-file-in-eclipse-cdt) – Ciro Santilli OurBigBook.com Apr 23 '20 at 18:21

9 Answers9

74

I've also had this issue.

I've found out that it is because Eclipse couldn't find all include headers.

Easy fix:

This simple and quick solution might fix your problem (for example, when the Eclipse project was moved to a different location on disk, then imported again in Eclipse), if not, jump to the next section (Detailed fix).

  1. Go to project > properties > C/C++ Build > Tool Chain Editor
  2. Change the Current toolchain to any other value, click Apply
  3. Set the Current toolchain to the original value, click Apply
  4. Compile your project

Detailed fix:

Before proceeding check if your toolchain is properly installed.

  1. Switch to a new workspace.
  2. Remove .cproject file and the ".settings" folder
  3. Import your project as Makefile project (or just create a new if you prefer CDT Build system)
  4. Go to project-> properties->C/C++ Build->Toolchain editor. Choose your toolchain.
  5. Press project->Index->Rebuild
  6. If the problem isn't resolved, change system language to English and try the above steps again.

Outdated answer:

This answer has been outdated. Proceed if nothing of the above helps

If the previous steps don't help we'll need to setup include directories manually (not recommended though)

  1. Search all unresolved headers using "Right click on Project > Index > Search for unresolved includes".
  2. Search their locations using "find /usr/include/ -name vector -print"
  3. Put include folder paths to "Right click on Project > Properties > C++ General/Path and Symbols/C++"
  4. Run "Right click on Project > Index > Rebuild"
  5. Start from step 1 if there are any unresolved symbols left.
Vanuan
  • 31,770
  • 10
  • 98
  • 102
  • 5
    Eclipse can't resolve standard library: Thank you very much - just what I was looking for. Seems like there ought to be a more automated way of doing this. I ended up all the way down in /usr/include/c++/4.7.0/i686-pc-linux-gnu/bits/c++config.h before Eclipse stopped complaining. Home some of these key words index well on google. – Barton May 21 '12 at 03:35
6

I've just replied to the related question given by Vanuan (Eclipse CDT: Unresolved inclusion of stl header), and this is my answer :

You could also try use "CDT GCC Built-in Compiler Settings". Go to the project properties > C/C++ General > Preprocessor Include Path > Providers tab then check "CDT GCC Built-in Compiler Settings" if it is not.

None of the other solutions (play with include path, etc) worked for me for the type 'string', but this one fixed it.

Community
  • 1
  • 1
Virtual
  • 449
  • 6
  • 15
6

The problem was that I needed to have both minGW and MSYS installed and added to PATH.

The problem is now fixed.

124697
  • 22,097
  • 68
  • 188
  • 315
4

You need to ensure your environment is properly setup in Eclipse so it knows the paths to your includes. Otherwise, it underlines them as not found.

3

I had the same problem. Adding include path does work for all except std::string.

I noticed in the mingw-Toolchain many system header files *.tcc

I added filetype *.tcc as "C++ Header File" in Preferences > C/C++/ File Types. Now std::string can be resolved from the internal index and Code Analyzer. Perhaps this is added to Eclipse CDT by default in feature.

I hope this helps to someone...

PS: I'm using Eclipse Mars, mingw gcc 4.8.1, Own Makefile, no Eclipse Makefilebuilder.

Gabor
  • 80
  • 9
1

I had the same problem. Change the CurrentBuilder in Properties/C/C++ Build/ToolChainEditor to another value and apply it. Then again change it the original value. It works.

0

I had this issue with AOSP (clang).

Add external\libcxx\include to includes and _LIBCPP_COMPILER_CLANG to symbols

lukee
  • 843
  • 9
  • 12
0

Hello here is a simple solution,

Just go to File -> Convert to a C/C++ Autotools Project Select your project files appropriately.

Inclusions will be added to your project file

Basri
  • 1
0

Set ${COMMAND} to g++ on Linux

Under "Preprocessor Include Paths, Macros, etc." and "CDT GCC Built-in Compiler Settings" there is an undefined ${COMMAND} variable if you imported the sources from an existing Makefile project.

Eclipse tries to run that command to parse its stdout to find headers, but ${COMMAND} is not set by default, and so it is not able to do so.

I have explained this in more detail at: How to solve "Unresolved inclusion: <iostream>" in a C++ file in Eclipse CDT?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985