Eclipse CDT 中 C 标准库头的“未解决的包含”错误

我为 eclipse 设置了 CDT,并编写了一个简单的 hello world C 程序:

#include <stdio.h>


int main(void){
puts("Hello, world.");
return 0;
}

该程序构建并正确运行,但是当我将鼠标放在它上面时,Eclipse 在包含语句旁边一直显示这个黄色问号,表示 "Unresolved inclusion: <stdio.h>"

它不会影响程序的运行,但是我觉得它很烦人。

有人知道怎么移除吗?

276117 次浏览

The compiler Eclipse is using is able to resolve the symbols just fine, so the code will compile fine.

But the code-completion/indexer or preprocessor Eclipse is using doesn't know where stdio.h exists.

You need to specify the filesystem path where stdio.h is located.

The Eclipse documentation describes this in several sections for the compiler:

And if the code-completion/indexer or preprocessor specifically also cannot locate stdio.h:

The exact location of stdio.h will depend on the system you are intending to write the code for. If you are writing code for the same system you are running Eclipse on, then the standard location is /usr/include/stdio.h for Linux, macOS, Cygwin, etc.

If you are cross-compiling for a separate/remote target system (e.g. Android, Raspberry Pi, STM32), then it will be located somewhere in the SDK you installed for that system. You will need to refer to that particular SDK documentation.

  • Select File>>New Project
  • In the Project Wizard, select C/C++>> C++ Project
  • In "Project type" section, select "Makefile Project>> Hello world C++ Project"
  • In "Toolchains" section, select "Linux GCC"

It can solve the problem. (Excuse me for bad English)

I found these answers (including the accepted one) somewhat cryptic.

For me, I had to add the path where stdio.h is located (as @ardnew said). In Eclipse, you open the Properties of your project, expand "C/C++ General" and select "Paths and Symbols".

Make sure you have added the include dir for each language you are using. (In my case, I needed to just add it to GNU C++.)

Screenshot of Eclipse "Project Properties" dialog with "Paths and Symbols" selected. An "add directory path" dialog is overlaid.

just adding to the knowledge base, i just did this on win7 with cygwin.

this is what seems to work for me.

include paths for c:

D:\dev\cygwin\lib\gcc\i686-pc-cygwin\3.4.4\include
D:\dev\cygwin\usr\include

include paths for c++:

D:\dev\cygwin\lib\gcc\i686-pc-cygwin\3.4.4\include
D:\dev\cygwin\lib\gcc\i686-pc-cygwin\3.4.4\include\c++
D:\dev\cygwin\usr\include

this gets me a clean compile of hello world.

In ADT I did the following:

  1. right click on the project and select Properties
  2. expand C/C++ General and select Preprocessor Include Paths, Macros etc.
  3. select CDT User Setting Entries
  4. select Add... from the right hand menu
  5. In the Add Include Directory change Project Path to File System Path
  6. Browse to the directory that contains your include files
  7. stir and repeat as needed

Go to Project > Properties > C/C++ General > Preprocessor Includes > Providers and select both:

  • "CDT GCC Built-in Compiler Settings"
  • "CDT CROSS GCC Built-in Compiler Settings"

For each one of those also select the sub-entry: "Use global provider shared between projects".

Tested on Eclipse 4.8.0 in Ubuntu 16.04 with a C and a C++ hello world.

I'm using Eclipse with Cygwin and this worked for me:

Go to Project > Properties > C/C++ General > Preprocessor Includes... > Providers and select "CDT GCC Built-in Compiler Settings Cygwin [Shared]".

Normally, Eclipse should be able to automatically resolve the standard include files. It does this by calling gcc and asking its configuration. Most likely Eclipse is not finding your gcc (or at least not the version you use for compiling).

Instead of specifying all the standard include paths in project settings, you probably want to make sure Eclipse finds gcc. Add the directory where gcc is found to PATH environment variable before starting Eclipse.

If you want different projects to use different compilers, then you might want to tweak the discovery options. These are hidden by default, so first enable them from Window > Preferences > C/C++ > Property Pages Settings > Display "Discovery Options" page. Then you can find them under C/C++ Build > Discovery Options in project properties.

I'm working with multiple cross compiler configurations, where I need to use different locations for the STD header files (and other environment variables).

The solution was to set up the indexer so it uses the active configuration.
Unfortunately due to some bug in eclipse, the option isn't stored locally, so you have to use the workspace configuration if you want to save the configuration when you open eclipse again.

Window -> Preferences -> C/C++ -> Indexer -> Use active build configuration

This will make eclipse use the right compiler that is set with the project's active Cross GCC configuration.

An error I had configuring Paths and Symbols is that initially I configued the include paths for a different language. I'm working with CDT and Cygwin gnu C++. So you must configure symbols and paths under GNU C++ language.enter image description here

As the top answers note, it's necessary to specify where the build folders are located, which can be added via a dialog reached by right-clicking the project, and selecting Properties->C/C++ General->Paths and Symbols.

The remaining question is what paths need to be added.

If you have gcc set up correctly for command-line access, and need to know what the default include paths it uses are, just ask it; depending on which language you're interested in, use:

gcc -x c -v -E /dev/null
gcc -x c++ -v -E /dev/null

...this will list the default compiler settings that are used when invoking gcc (and this command also works if "gcc" is really an alias for clang, as on OSX).

/dev/null is used as an empty file - we're telling gcc to parse an empty file

-x <language> specifies the language to compile as, necessary because we're not using a file with an extension that specifies the language

-v verbose output, which includes outputting the include paths

-E only perform preprocessing, output the preprocessed file (this prevents gcc from complaining that an empty file doesn't compile correctly)

Toward the bottom will be the list of include directories:

#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks (framework directory)
End of search list.

If you enter the directories listed here, in the order listed, into Eclipse's paths and symbols dialog, Eclipse CDT should be able to find the standard headers, and perhaps some additional headers specific to your OS.

(With thanks to devnull's answer to a related question.)

I m using eclipse based CodeWarrior IDE for embedded projects and i have just solved this problem by deleting and adding again the source adresses to Project Properities->C/C++ General->Path and Sybols-> Include Directories. This means that there lots of reason to take "Unresolved inclusion:" message and there r lots of solution too.

Also set ${COMMAND} to gcc on Linux

Under:

  • Project
  • Properties
  • C/C++ General
  • Preprocessor Include Paths, Macros, etc.
  • Providers
  • CDT GCC Built-in Compiler Settings
  • Command to get compiler specs

Replace:

${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"

with:

gcc -std=c99 -E -P -v -dD "${INPUTS}"

If you don't do this, the Eclipse stdout shows:

Unable to find full path for "-E"

and logs under ${HOME}/eclipse-workspace/.metadata/.log show:

!ENTRY org.eclipse.cdt.core 4 0 2020-04-23 20:17:07.288
!MESSAGE Error: Cannot run program "-E": Unknown reason

because ${COMMAND} ${FLAGS} are empty, and so Eclipse tries to execute the -E that comes next.

I wonder if we can properly define the COMMAND and FLAGS variables on the settings, but I tried to add them as build variables and it didn't work.

C++ version of the question: How to solve "Unresolved inclusion: <iostream>" in a C++ file in Eclipse CDT?

Tested on Eclipse 2020-03 (4.15.0), Ubuntu 19.10, and this minimal Makefile project with existing sources.

Do you check if it's compile from command line? For me helps on Linux, apt install libc-dev.