How to enable C++11/C++0x support in Eclipse CDT?

后端 未结 17 1528
说谎
说谎 2020-11-22 00:13

Eclipse 3.7.1 CDT 1.4.1 GCC 4.6.2

This is an example of a piece of C++11 code:

auto text = std::unique_ptr(new char[len]);
相关标签:
17条回答
  • 2020-11-22 00:43

    I can't yet comment so am writing my own answer:

    It's related to __GXX_EXPERIMENTAL_CXX0X__ and it's valid for Eclipse Juno and CDT 8.x.

    Some parts of this answer are already covered in other answers but I want it to be coherent.

    To make it possible to build using stdc++11, one have to add specific flag for compiler. You can do that via project properties. To modify project properties RMB andProject properties or ALT + ENTER. Then C/C++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Miscellaneous -> Other Flags. Put -std=c++11 at the end of line, for GCC it will look something like: -c -fmessage-length=0 -std=c++11. By adding -stdc++11 flag compiler (GCC) will declare __GXX_EXPERIMENTAL_CXX0X__ by itself.

    At this point you can build project using all the goodness of C++11.

    The problem is that Eclipse has it's own parser to check for errors - that's why you're still getting all the nasty errors in Eclipse editor, while at the same time you can build and run project without any. There is a way to solve this problem by explicitly declaring __GXX_EXPERIMENTAL_CXX0X__ flag for the project, one can do that (just like Carsten Greiner said): C/C++ General -> Paths and Symbols -> Symbols -> GNU C++. Click "Add..." and past __GXX_EXPERIMENTAL_CXX0X__ (ensure to append and prepend two underscores) into "Name" and leave "Value" blank. And now is the extra part I wanted to cover in comment to the first answer, go to: C/C++ General -> Preprocessor Include Path Macros etc. -> Providers, and Select CDT Managed Build Setting Entries then click APPLY and go back to Entries tab, under GNU C++ there should be now CDT Managed Build Setting Entries check if inside there is defined __GXX_EXPERIMENTAL_CXX0X__ if it is -> APPLY and rebuild index you should be fine at this point.

    0 讨论(0)
  • 2020-11-22 00:44

    Instruction For Eclipse CDT 4.4 Luna and 4.5 Mars

    First, before creating project, configure Eclipse syntax parser:

    Window -> Preferences -> C/C++ -> Build -> Settings -> Discovery -> CDT GCC Build-in Compiler Settings

    in the text box entitled Command to get compiler specs append -std=c++11

    Now you can create project, configuration depends on what kind of project you created:

    For project created as: File -> New -> Project -> C/C++ -> C++ Project

    Right click on created project and open

    Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Dialect

    Put -std=c++11 into text box entitled other dialect flags or select ISO C++11 from the Language standard drop down.

    For CMake project

    Generate eclipse project files (inside your project)

    mkdir build
    cd build
    cmake -G"Eclipse CDT4 - Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug ..
    

    Then import generated directory to eclipse as standard eclipse project. Right click project and open

    Properties -> C/C++ General -> Preprocessor Include Paths, Marcos etc. -> Providers

    enable CDT GCC Build-in Compiler Settings and move it higher than Contributed PathEntry Containers (This is important)

    Last Common Step

    recompile, regenerate Project ->C/C++ Index and restart Eclipse.

    0 讨论(0)
  • 2020-11-22 00:47

    Eclipse C/C++ does not recognize the symbol std::unique_ptr even though you have included the C++11 memory header in your file.

    Assuming you are using the GNU C++ compiler, this is what I did to fix:

    Project -> Properties -> C/C++ General -> Preprocessor Include Paths -> GNU C++ -> CDT User Setting Entries

    1. Click on the "Add..." button

    2. Select "Preprocessor Macro" from the dropdown menu

      Name: __cplusplus     Value:  201103L
      
    3. Hit Apply, and then OK to go back to your project

    4. Then rebuild you C++ index: Projects -> C/C++ Index -> Rebuild

    0 讨论(0)
  • 2020-11-22 00:47

    For me on Eclipse Neon I followed Trismegistos answer here above , YET I also added an additional step:

    • Go to project --> Properties --> C++ General --> Preprocessor Include paths,Macros etc. --> Providers --> CDT Cross GCC Built-in Compiler Settings, append the flag "-std=c++11"

    Hit apply and OK.

    Cheers,

    Guy.

    0 讨论(0)
  • 2020-11-22 00:48

    I don't know if it is only me, the highest ranked solution doesn't work for me, my eclipse version is just normal eclipse platform installed by using sudo apt-get install eclipse in Ubuntu But I found a solution which adopts method together from both the highest ranked solution and the second, what I did to make it work is described as below (Note that the other steps like creating a C++ project etc. is ignored for simplicity)

    Once you have created the C++ project

    (1) C/C++ General -> Paths and Symbols -> Symbols -> GNU C++. Click "Add..." and paste GXX_EXPERIMENTAL_CXX0X (ensure to append and prepend two underscores) into "Name" and leave "Value" blank.

    (2) Under C/C++ Build (at project settings), find the Preprocessor Include Path and go to the Providers Tab. Deselect all except CDT GCC Builtin Compiler Settings. Then untag Share settings entries … . Add the option -std=c++11 to the text box called Command to get compiler specs

    After performed above 2 and 2 only steps, it works, the eclipse is able to resolve the unique_ptr, I don't know why this solution works, hope that it can help people.

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