“Unresolved inclusion” error with Eclipse CDT for C standard library headers

前端 未结 14 2152
甜味超标
甜味超标 2020-11-28 04:18

I set up CDT for eclipse and wrote a simple hello world C program:

#include 

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


        
相关标签:
14条回答
  • 2020-11-28 04:57

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

    0 讨论(0)
  • 2020-11-28 04:58

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

    0 讨论(0)
  • 2020-11-28 04:59

    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.

    0 讨论(0)
  • 2020-11-28 05:00

    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:

    • Adding include paths and symbols
    • C/C++ Project Properties, Paths and Symbols, Includes

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

    • Setting up include paths and macros for C/C++ indexer
    • C/C++ Project properties: Preprocessor Include Paths, Macros, etc.

    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.

    0 讨论(0)
  • 2020-11-28 05:00

    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.

    0 讨论(0)
  • 2020-11-28 05:03

    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.

    0 讨论(0)
提交回复
热议问题