Eclipse CDT: Symbol 'cout' could not be resolved

196,898

Solution 1

Most likely you have some system-specific include directories missing in your settings which makes it impossible for indexer to correctly parse iostream, thus the errors. Selecting Index -> Search For Unresolved Includes in the context menu of the project will give you the list of unresolved includes which you can search in /usr/include and add containing directories to C++ Include Paths and Symbols in Project Properties.

On my system I had to add /usr/include/c++/4.6/x86_64-linux-gnu for bits/c++config.h to be resolved and a few more directories.

Don't forget to rebuild the index (Index -> Rebuild) after adding include directories.

Solution 2

To get rid of symbol warnings you don't want, first you should understand how Eclipse CDT normally comes up with unknown symbol warnings in the first place. This is its process, more or less:

  1. Eclipse detects the GCC toolchains available on the system
  2. Your Eclipse project is configured to use a particular toolchain
  3. Eclipse does discovery on the toolchain to find its include paths and built-in defines, i.e. by running it with relevant options and reading the output
  4. Eclipse reads the header files from the include paths
  5. Eclipse indexes the source code in your project
  6. Eclipse shows warnings about unresolved symbols in the editor

It might be better in the long run to fix problems with the earlier steps rather than to override their results by manually adding include directories, symbols, etc.

Toolchains

If you have GCC installed, and Eclipse has detected it, it should list that GCC as a toolchain choice that a new C++ project could use, which will also show up in Window -> Preferences -> C/C++ -> New CDT Project Wizard on the Preferred Toolchains tab's Toolchains box on the right side. If it's not showing up, see the CDT FAQ's answer about compilers that need special environments (as well as MinGW and Cygwin answers for the Windows folk.)

If you have an existing Eclipse C++ project, you can change the associated toolchain by opening the project properties, and going to C/C++ Build -> Tool Chain Editor and choosing the toolchain you want from the Current toolchain: pulldown. (You'll have to uncheck the Display compatible toolchains only box first if the toolchain you want is different enough from the one that was previously set in the project.)

If you added a toolchain to the system after launching Eclipse, you will need to restart it for it to detect the toolchain.

Discovery

Then, if the project's C/C++ Build -> Discovery Options -> Discovery profiles scope is set to Per Language, during the next build the new toolchain associated with the project will be used for auto-discovery of include paths and symbols, and will be used to update the "built-in" paths and symbols that show up in the project's C/C++ General -> Paths and Symbols in the Includes and Symbols tabs.

Indexing

Sometimes you need to re-index again after setting the toolchain and doing a build to get the old symbol warnings to go away; right-click on the project folder and go to Index -> Rebuild to do it.

(tested with Eclipse 3.7.2 / CDT 8)

Solution 3

Thanks loads for the answers above. I'm adding an answer for a specific use-case...

On a project with two target architectures each with its own build configuration (the main target is an embedded AVR platform; the second target is my local Linux PC for running unit tests) I found it necessary to set Preferences -> C/C++ -> Indexer -> Use active build configuration as well as to add /usr/include/c++/4.7, /usr/include and /usr/include/c++/4.7/x86_64-linux-gnu to Project Properties -> C/C++ General -> Paths and Symbols and then to rebuild the index.

Solution 4

I tried the marked solution here first. It worked but it is kind hacky, and you need to redo it every time you update the gcc. I finally find a better solution by doing the followings:

  1. Project -> Properties -> C/C++ General -> Preprocessor Include Paths, Macros, etc.
  2. Providers -> CDT GCC built-in compiler settings
  3. Uncheck Use global provider shared between projects (you can also modify the global provider if it fits your need)
  4. In Command to get compiler specs, add -std=c++11 at the end
  5. Index->Rebuild

Voila, easy and simple. Hopefully this helps.

Note: I am on Kepler. I am not sure if this works on earlier Eclipse.

Solution 5

I had a similar problem with *std::shared_ptr* with Eclipse using MinGW and gcc 4.8.1. No matter what, Eclipse would not resolve *shared_ptr*. To fix this, I manually added the __cplusplus macro to the C++ symbols and - viola! - Eclipse can find it. Since I specified -std=c++11 as a compile option, I (ahem) assumed that the Eclipse code analyzer would use that option as well. So, to fix this:

  1. Project Context -> C/C++ General -> Paths and Symbols -> Symbols Tab
  2. Select C++ in the Languages panel.
  3. Add symbol __cplusplus with a value of 201103.

The only problem with this is that gcc will complain that the symbol is already defined(!) but the compile will complete as before.

Share:
196,898
Jeff
Author by

Jeff

Updated on August 20, 2020

Comments

  • Jeff
    Jeff over 3 years

    The error is as above. I have what should be all the necessary files include in the eclipse project:

    /usr/include/c++/4.6
    /usr/include
    /usr/include/linux
    /usr/local/include
    

    etc.

    I tried std::cout and using namespace std; cout but it still says unresolved.

    I have imported iostream and cstdlib.

    Also, I'm on Ubuntu 12.04 with eclipse 3.7.2.

    Code snippet:

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include "XPLMDisplay.h"
    #include "XPLMGraphics.h"
    
    int XPluginStart(char * outName,  char * outSig,  char * outDesc) {
        /* ... */
        std::cout << "test" << std::endl;
        /* ... */
    }
    

    using namespace std;


    UPDATE: I had created the eclipse project from existing code. Creating a new c++ project fixes it. I'll accept an answer that explains what setting in the existing project could cause this (so I don't have to cut & paste all my projects).

  • tony gil
    tony gil over 11 years
    perfect solution, kudos. solved my problem with local error messages regarding mysql methods and classes in c++ on adt+eclipse juno bundle with cdt added (ubuntu 12.04). upvote, of course!
  • wall-e
    wall-e about 11 years
    for me, Ubuntu 10.04, have to add /usr/include/c++/4.4/i486-linux-gnu
  • jespestana
    jespestana about 11 years
    I had to add include_directories("/usr/include/x86_64-linux-gnu") to my cmake file, then the generated eclipse project is able to parse vector, cout, string, and all the other present components from the C++ STL library. For those who need more information about configuring Eclipse CDT projects from CMake files see this post: stackoverflow.com/questions/9453851/…
  • hAcKnRoCk
    hAcKnRoCk almost 11 years
    The question was how to resolve the includes. Not how to delete eclipse warnings.
  • resigned
    resigned over 10 years
    Nevertheless this seems to fix the indexer sometimes - at least it worked for me.
  • Mark
    Mark over 9 years
    I just had to rebuild the index, after upgrading my system; the newer eclipse 4.4.1 had had some difficulty while importing workspace / updating plugins etcetera
  • HighCommander4
    HighCommander4 over 7 years
    This is the solution to a different problem, that of C++11 symbols not being resolved. The OP was having trouble getting even C++98 symbols like std::cout resolved.
  • Sandeep
    Sandeep over 2 years
    For me the following worked: 1). [Select Project Name] --> Index --> Search for Unresolved Includes 2). Clean Project 3). Build Project