Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse CDT: Symbol 'cout' could not be resolved

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).

like image 325
Jeff Avatar asked May 29 '12 17:05

Jeff


4 Answers

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.

like image 194
vitaut Avatar answered Oct 23 '22 21:10

vitaut


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)

like image 25
rakslice Avatar answered Oct 23 '22 23:10

rakslice


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.

like image 16
Jack Kelly Avatar answered Oct 23 '22 23:10

Jack Kelly


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.

like image 8
Andy Shiue Avatar answered Oct 23 '22 22:10

Andy Shiue