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

2018-12-31 03:15发布

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<char[]>(new char[len]);

The Eclipse editor complains about:

Function 'unique_ptr' could not be resolved

The Makefile compilation works fine. How to make Eclipse stop complaining about these sort of errors?

15条回答
只靠听说
2楼-- · 2018-12-31 04:04

To get support for C++14 in Eclipse Luna, you could do these steps:

  • In C++ General -> Preprocessor Include -> Providers -> CDT Cross GCC Built-in Compiler Settings, add "-std=c++14"
  • In C++ Build -> Settings -> Cross G++ Compiler -> Miscellaneous, add "-std=c++14"

Reindex your project and eventually restart Eclipse. It should work as expected.

查看更多
墨雨无痕
3楼-- · 2018-12-31 04:04

I solved it this way on a Mac. I used Homebrew to install the latest version of gcc/g++. They land in /usr/local/bin with includes in /usr/local/include.

I CD'd into /usr/local/bin and made a symlink from g++@7whatever to just g++ cause that @ bit is annoying.

Then I went to MyProject -> Properties -> C/C++ Build -> Settings -> GCC C++ Compiler and changed the command from "g++" to "/usr/local/bin/g++". If you decide not to make the symbolic link, you can be more specific.

Do the same thing for the linker.

Apply and Apply and Close. Let it rebuild the index. For a while, it showed a daunting number of errors, but I think that was while building indexes. While I was figuring out the errors, they all disappeared without further action.


I think without verifying that you could also go into Eclipse -> Properties -> C/C++ -> Core Build Toolchains and edit those with different paths, but I'm not sure what that will do.

查看更多
君临天下
4楼-- · 2018-12-31 04:05

For Eclipse CDT Kepler what worked for me to get rid of std::thread unresolved symbol is:

  1. Go to Preferences->C/C++->Build->Settings

  2. Select the Discovery tab

  3. Select CDT GCC Built-in Compiler Settings [Shared]

  4. Add the -std=c++11 to the "Command to get the compiler specs:" field such as:

${COMMAND} -E -P -v -dD -std=c++11 ${INPUTS}

  1. Ok and Rebuild Index for the project.

Adding -std=c++11 to project Properties/C/C++ Build->Settings->Tool Settings->GCC C++ Compiler->Miscellaneous->Other Flags wasn't enough for Kepler, however it was enough for older versions such as Helios.

查看更多
公子世无双
5楼-- · 2018-12-31 04:08

I had several issues too (Ubuntu 13.04 64-bit, g++-4.8, eclipse Juno 3.8.1, CDT 6.0.0). A lot of things are mentioned above, sorry to repeat those, but additionally I had problems utilizing

std::thread

as part of c++11 (adding -pthread for the linker solves that issue). Anyway, finally these settings worked fine:

Project -> Properties -> C/C++ Build -> Settings -> Miscellaneous. Add the

-std=c++11

flag for the GCC and G++ compilers. Click Apply.

For the linker, same window, Miscellaneous, Linker flags, added the

-pthread

flag. Shared library settings, Shared object name, add the

-Wl,--no-as-needed

flag too. Click Apply.

C/C++ General -> Paths and symbols -> Symbols TAB, GNU C++ selected, Add the

__GXX_EXPERIMENTAL_CXX0X__

(no value)

flag. Click Apply.

C/C++ General -> Preprocessor Include paths.. -> Providers tab : check

CDT GCC built-in Compiler Settings

and for "Command to get compiler specs", add the

-std=c++11

flag. Uncheck Share. Click Apply.

CDT Managages Build Setting Entries, check this too. Uncheck the two others. Click Apply.

Going back to the Entries tab, GNU C++ CDT Managages Build Setting Entries, you should now see your added

__GXX_EXPERIMENTAL_CXX0X__

entry.

That's it. When coding, typing

std::

can now auto-complete the thread class for instance, builds should work fine and there should be no

std::system_error'what(): Enable multithreading to use std::thread: Operation not permitted

at runtime.

查看更多
呛了眼睛熬了心
6楼-- · 2018-12-31 04:12

For the latest (Juno) eclipse cdt the following worked for me, no need to declare __GXX_EXPERIMENTAL_CXX0X__ on myself. This works for the the CDT indexer and as parameter for the compiler:

"your project name" -> right click -> properties:

C/C++ General -> Preprocessor Include Paths, Macros etc. -> switch to the tab named "Providers":

  • for "Configuration" select "Release" (and afterwards "debug")

  • switch off all providers and just select "CDT GCC Built-in Compiler Settings"

  • uncheck "Share setting entries between projects (global provider)"

  • in the "Command to get compiler specs:" add "-std=c++11" without the quotes (may work with quotes too)

  • hit apply and close the options

  • rebuild the index

Now all the c++11 related stuff should be resolved correctly by the indexer.

win7 x64, latest official eclipse with cdt mingw-w64 gcc 4.7.2 from the mingwbuilds project on sourceforge

查看更多
怪性笑人.
7楼-- · 2018-12-31 04:12

I had the same problem on my Eclipse Juno. These steps solved the problem :

  • Go to Project -> Properties -> C/C++ General -> Path and Symbols -> Tab [Symbols].
  • Add the symbol : __cplusplus with the value 201103L
查看更多
登录 后发表回答