The LIBRARY_PATH environment variable is pretty standard. It is known to majority of compilers.
You should also use C_INCLUDE_PATH and/or CPLUS_INCLUDE_PATH. These two a more gcc specific (other compilers prefer INCLUDE without language separation).
You can also ignore the environment variables completely and specify the correct libstdc++ directly in the command line.
g++ main.cpp /software/gcc10/.../libstdc++.a
Answer from White Owl on Stack ExchangeForce GCC to require #include for standard libraries.
Compile c program with access to stdlib functions, but without _start and all of the libc init functions using gcc - Stack Overflow
c++ - GCC linker can't find standard library? - Stack Overflow
-stdlib=libc++ should be conditional on compiler and not hardcoded on macOS
On Linux: In general, all commonly available linux distributions will use libstdc++ by default, and all modern versions of GCC come with a libstdc++ that supports C++11. If you want to compile c++11 code here, use one of:
g++ -std=c++11 input.cxx -o a.out(usually GNU compiler)g++ -std=gnu++11 input.cxx -o a.out
On OS X before Mavericks: g++ was actually an alias for clang++ and Apple's old version of libstdc++ was the default. You could use libc++ (which included c++11 library support) by passing -stdlib=libc++. If you want to compile c++11 code here, use one of:
g++ -std=c++11 -stdlib=libc++ input.cxx -o a.out(clang, not GNU compiler!)g++ -std=gnu++11 -stdlib=libc++ input.cxx -o a.out(clang, not GNU compiler!)clang++ -std=c++11 -stdlib=libc++ input.cxx -o a.outclang++ -std=gnu++11 -stdlib=libc++ input.cxx -o a.out
On OS X since Mavericks: libc++ is the default and you should not pass any -stdlib=<...> flag. Since Xcode 10, building against libstdc++ is not supported at all anymore. Existing code built against libstdc++ will keep working because libstdc++.6.dylib is still provided, but compiling new code against libstdc++ is not supported.
clang++ -std=c++11 input.cxx -o a.outclang++ -std=gnu++11 input.cxx -o a.out
When is it necessary to use use the flag
-stdlib=libstdc++for the compiler and linker when compiling with gcc?
Short answer: never
Longer answer: -stdlib is a Clang flag and will not work with any version of GCC ever released. On macOS sometimes the gcc and g++ commands are actually aliases for Clang not GCC, and the version of libstdc++ that Apple ships is ancient (circa 2008) so of course it doesn't support C++11. This means that on macOS when using Clang-pretending-to-be-GCC, you can use -stdlib=libc++ to select Clang's new C++11-compatible library, or you can use -stdlib=libstdc++ to select the pre-C++11 antique version of libstdc++ that belongs in a museum. But on GNU/Linux gcc and g++ really are GCC not Clang, and so the -stdlib option won't work at all.
Edit: Since I wrote this answer, GCC was changed to conditionally support the -stdlib flag, but for most platforms that support is disabled by default. Even when it's enabled, the default is -stdlib=libstdc++ so you still never need to say that explicitly. GCC will still automatically use libstdc++.
Does the compiler automatically use libstdc++?
Yes, GCC always uses libstdc++ unless you tell it to use no standard library at all with the -nostdlib or -nostdlib++ option (in which case you either need to avoid using any standard library features, or use -I and -L and -l flags to point it to an alternative set of header and library files).
I am using gcc4.8.2 on Ubuntu 13.10 and I would like to use the c++11 standard. I already pass
-std=c++11to the compiler.
You don't need to do anything else. GCC comes with its own implementation of the C++ standard library (libstdc++) which is developed and tested alongside GCC itself so the version of GCC and the version of libstdc++ are 100% compatible. If you compile with -std=c++11 then that enables the C++11 features in g++ compiler and also the C++11 features in the libstdc++ headers.
Yesterday I made a post here about gcc compiling code it shouldn't. I deleted it because I figured out it was a problem with the toolchain llvm-mingw64, which includes Clang, GCC and G++, and somehow its GCC compiler can compile C++ with the .c file extension, maybe a bug.
Now, I switched to regular mingw and it works as it should. Except that when I took CS a long time ago, I remember I was forced to #include stdlib/stdio if I wanted to use functions, otherwise it wouldn't compile.
I want to know if it's possible to enforce that behavior, because it seems the current GCC version will compile and run code that needs that standard library even though it's not included.
I wish it enforced that, is there away around this issue?
To link C++ code, you need to use the g++ command, not the gcc command.
When compiling, the gcc command knows that it's compiling C++ code because of the .cpp suffix (but it's a good idea to use the g++ command anyway).
When linking, the gcc command just sees a bunch of .o files. The g++ command differs from the gcc command in that it knows where to find the C++-specific libraries.
(The fact that the name gcc refers both to the command, which is usually used to compile C code, and the "GNU Compiler Collection" as a whole, can be a little confusing.)
I know this is old, but if you are compiling and linking C++, you can specify the standard library yourself. Add -lstdc++ at end of your command.
As the error message says, the gcc compiler has no such commandline option as -stdlib. The LLVM clang compiler does.
That is because clang offers you the choice of linking the LLVM standard C++ library (libc++) or the
GNU standard C++ library (libstdc++), whereas gcc supports only libstdc++.
Delete the option -stdlib=libc++. You might as well also replace -std=c++0x with -std=c++11,
since the former denotes experimental support for the 2011 C++11 standard, applicable for gcc versions
4.3 through 4.6.
It might also be useful to install the clang/clang++ compiler and use this when it comes to the build step (because you are likely to run into gcc issues with other Python packages):
python configure.py
export CC=/usr/bin/clang
export CXX=/usr/bin/clang++
python setup.py build
make -j 4
python setup.py install
cd ../
python -c "import pyopencl"