Pregunta

I can compile and run c++ example in command line like this:

clang++ -std=c++11 -stdlib=libc++ Test.cpp

But when trying this in eclipse,I got these errors

12:58:18 **** Incremental Build of configuration Debug for project C++Test ****
Info: Internal Builder is used for build

clang++ -L/usr/lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/ -o
C++Test src/C++Test.bc -llibc++ -lstdc++ 

/usr/bin/ld: cannot find -llibc++

clang: error: linker command failed with exit code 1 (use -v to see invocation)

There are two strange things here:

First,I have actually appended "-std=c++11 -stdlib=libc++" to compile command in project build settings,but it seems be ignored.Why?

Second,I have remove stdc++ from link libraries,but it will be added automatically each time,like this:

Update:add a picture to demo this problem stdc++ will be added automatically again after deleting

Then,I tried to remove libc++ from link libraries in project settings,compile it again,got bunch of errors like these:

Info: Internal Builder is used for build
clang++ -L/usr/lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/ -o C++Test src/C++Test.bc -lstdc++ 
/tmp/C++Test-d24e4c.o: In function `main':
/home/alex/workspace/C++Test/Debug/../src/C++Test.cpp:12: undefined reference to `std::__1::cout'
/tmp/C++Test-d24e4c.o: In function `std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<< <std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*)':
/usr/include/c++/v1/ostream:974: undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::sentry(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)'
/tmp/C++Test-d24e4c.o: In function `std::__1::basic_ios<char, std::__1::char_traits<char> >::fill() const':
/usr/include/c++/v1/ios:734: undefined reference to `std::__1::ios_base::getloc() const'
......

"-std=c++11 -stdlib=libc++" ignored still,so how to get it work?

¿Fue útil?

Solución

The foremost problem is why the linker cannot find libc++:

If you want to link a dynamic library, say, libfoo.so, with the -l flag, you use the option -lfoo, not -llibfoo. Therefore to link libc++ you use the option -lc++, not -llibc++, as you have done. In your project's libraries (-l) settings, replace libc++ with c++.

A secondary problem is that the Eclipse-CDT-generated link command:

clang++ -L/usr/lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/ -o
C++Test src/C++Test.bc -llibc++ -lstdc++

is attempting to link libstdc++ (the GCC Standard C++ library) as well as (what is supposed to be) libc++ (the LLVM Standard C++ library). You want to link one or the other, not both:

You say that even if you delete stdc++ from the project's libraries, it is automatically added by the Eclipse build. I cannot reproduce that problem. Perhaps you can double check it:

  • Delete stdc++ from the project's libraries and apply that change.

  • Then look at the linker settings -> All options and see if -lstdc++ appears there. It shouldn't.

  • If it does, look at linker settings -> Miscellaneous -> Linker flags and Other options. Does -lstdc++ appear there? If so, delete it.

  • Otherwise, look at the linker's Expert settings: commandline pattern. If it contains -lstdc++, delete it.

You say also that you have appended the options -std=c++11 -stdlib=libc++ to the compiler Command setting, but this seems to be ignored. They may be appear to be ignored because only the link step of your project is failing, so the compile step is not rerun when you build. However it is misguided to add these options to the compile command:-

  • -std=c++11 is a compile option. You should add it to compiler -> Miscellaneous -> Other flags.

  • -stdlib=libc++ is a linkage option and, if wanted, should be added to linker -> Miscellaneous -> Linker flags. However it is superfluous to use this option if you also link with -lc++ (and do not link -lstdc++), and -lc++ is the more dependable option. I suggest you do not bother with -stdlib=libc++.

In view of your difficulties linking with libc++ one should ask: Do you really need to? The fact that you are building with clang does not oblige you to link libc++. Clang will quite happily link libstdc++.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top