By default, Eclipse CDT (I'm using 9.10 on a Linux VM) links against libstdc++ when the clang toolchain is chosen. For some reason, this option cannot be permanently removed, it will just reappear after closing the settings menu and reopening.
Eclipse also doesn't give me any warnings or errors if I add libc++ to link against, so my program gets compiled with both flags and it runs just fine. I also get no hints either, when I add -stdlib=libc++
, which compiles and runs fine, too.
That leaves me wondering if Eclipse ignores libc++ when libstdc++ is included.
These are the compiler outputs I got from playing around with the flags:
1)
clang++ -std=c++17 -Wall -Wextra -Wpedantic -O3 -emit-llvm -c -fmessage-length=0 -o main.bc ../main.cpp
clang++ -L/usr/lib/ -o "2015-19 P1 v2" main.bc -lc++ -lstdc++.
2)
clang++ -std=c++17 -Wall -Wextra -Wpedantic -O3 -emit-llvm -c -fmessage-length=0 -o main.bc ../main.cpp
clang++ -L/usr/lib/ -o "2015-19 P1 v2" main.bc -lstdc++ -lc++
3)
clang++ -stdlib=libc++ -std=c++17 -Wall -Wextra -Wpedantic -O3 -emit-llvm -c -fmessage-length=0 -o main.bc ../main.cpp
clang++ -L/usr/lib/ -o "2015-19 P1 v2" main.bc -lstdc++ -lc++
4)
clang++ -stdlib=libc++ -std=c++17 -Wall -Wextra -Wpedantic -O3 -emit-llvm -c -fmessage-length=0 -o main.bc ../main.cpp
clang++ -L/usr/lib/ -o "2015-19 P1 v2" main.bc -lc++ -lstdc++
What I would like to know: for each of these four flag placements, which stdlib does my program ultimately get compiled with?
I've found the answer to my question, and for completeness' sake, I'll post it here.
To make sure that my Eclipse CDT project is compiled with libc++, I needed to include the following flag
-stdlib=libc++
under either of those settings
a) Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> LLVM Clang++ -> Command (insert after clang++
)
b) Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> LLVM Clang++ -> Miscellaneous -> Other flags
I also had to include the c++
library for the Linker under
Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> LLVM Clang++ linker -> Libraries -> Libraries (-l)
There's a convenient way to test which stdlib is linked against, using the following code:
#include <iostream>
#include <string_view>
template<typename T>
constexpr auto print() {
std::string_view name = __PRETTY_FUNCTION__;
name.remove_prefix(name.find('=')+2);
name.remove_suffix(1);
return name;
}
int main() {
std::cout << print<std::string>() << std::endl;
}
This will output one of the following, depending on whether or not the -stdlib
flag and the c++ library are set:
// libc++
std::__1::basic_string<char>
// libstdc++
std::__cxx11::basic_string<char>