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 Exchange
🌐
GNU
gcc.gnu.org › onlinedocs › libstdc++
The GNU C++ Library
Short Contents Copyright (C) 2008-2026 FSF Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover ...
Discussions

Force GCC to require #include for standard libraries.
On the version of gcc I'm using (8.3.0) it will warn about implicit function definitions, which is what you get if you try to use stuff without the appropriate headers. e.g.: int main(int argc, char *argv[]) { printf("Hello, World!\n"); } Here we use printf which is in stdio.h, but we don't include it. By default I get some warnings, and a suggestion of what to fix it: $ gcc -o foo foo.c foo.c: In function ‘main’: foo.c:2:3: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration] printf("Hello, World!\n"); ^~~~~~ foo.c:2:3: warning: incompatible implicit declaration of built-in function ‘printf’ foo.c:2:3: note: include ‘’ or provide a declaration of ‘printf’ +#include int main(int argc, char *argv[]) { printf("Hello, World!\n"); ^~~~~~ $ However if I add the -Werror flag, it will fail to compile: $ gcc -Werror -o foo foo.c foo.c: In function ‘main’: foo.c:2:3: error: implicit declaration of function ‘printf’ [-Werror=implicit-function-declaration] printf("Hello, World!\n"); ^~~~~~ foo.c:2:3: error: incompatible implicit declaration of built-in function ‘printf’ [-Werror] foo.c:2:3: note: include ‘’ or provide a declaration of ‘printf’ +#include int main(int argc, char *argv[]) { printf("Hello, World!\n"); ^~~~~~ cc1: all warnings being treated as errors $ Additionally, although it's bad practice to have implicit definitions of functions and the warnings these generate, in a lot of cases (such as with printf) the default definition is close enough for the code to work. More on reddit.com
🌐 r/C_Programming
16
7
August 9, 2022
Compile c program with access to stdlib functions, but without _start and all of the libc init functions using gcc - Stack Overflow
So essentially I want to compile a c program statically with gcc, and I want it to be able to link c stdlib functions, but I want it to start at main, and not include the _start function as well as... More on stackoverflow.com
🌐 stackoverflow.com
c++ - GCC linker can't find standard library? - Stack Overflow
I've been developing a school project in XCode. The final product has to be submitted in source code with a makefile, so I wrote a makefile and to start compiling that way, to make sure I had a wor... More on stackoverflow.com
🌐 stackoverflow.com
-stdlib=libc++ should be conditional on compiler and not hardcoded on macOS
These flags in Makefile should be conditional on compiler and not just OS: ifeq ($(UNAME),Darwin) CFLAGS += -stdlib=libc++ CXXFLAGS += -stdlib=libc++ LDFLAGS += -stdlib=libc++ endif Otherwise build with GCC is broken: /opt/local/bin/gcc-... More on github.com
🌐 github.com
8
December 7, 2022
Top answer
1 of 3
120

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.out
  • clang++ -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.out
  • clang++ -std=gnu++11 input.cxx -o a.out
2 of 3
39

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++11 to 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.

🌐
GitHub
github.com › gcc-mirror › gcc › blob › master › libstdc++-v3 › include › c_compatibility › stdlib.h
gcc/libstdc++-v3/include/c_compatibility/stdlib.h at master · gcc-mirror/gcc
// a copy of the GCC Runtime Library Exception along with this program; // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see · // <http://www.gnu.org/licenses/>. · /** @file stdlib.h · * This is a Standard C++ Library header. */ · #if !defined __cplusplus || defined _GLIBCXX_INCLUDE_NEXT_C_HEADERS ·
Author   gcc-mirror
🌐
GitHub
github.com › gcc-mirror › gcc › blob › master › libstdc++-v3 › include › c_global › cstdlib
gcc/libstdc++-v3/include/c_global/cstdlib at master · gcc-mirror/gcc
// a copy of the GCC Runtime Library Exception along with this program; // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see · // <http://www.gnu.org/licenses/>. · /** @file include/cstdlib · * This is a Standard C++ Library file. You should @c \#include this file · * in your programs, rather than any of the @a *.h implementation files. * * This is the C++ version of the Standard C Library header @c stdlib.h, * and its contents are (mostly) the same as that header, but are all ·
Author   gcc-mirror
🌐
Hacker News
news.ycombinator.com › item
I use Clang with GCC's C++ stdlib. You can do this with a single flag: clang++ -... | Hacker News
November 28, 2022 - clang++ -std=c++2b --gcc-toolchain=/usr/local/gcc-13.0.0-dev Now I get to use LLVM with the latest C++23 support from GCC. Have your cake and eat it, too =) · The C++ compiler and the C++ standard library exist as separate components. You're free to mix and match them.
🌐
GitHub
github.com › gcc-mirror › gcc › blob › master › libstdc++-v3 › include › c_std › cstdlib
gcc/libstdc++-v3/include/c_std/cstdlib at master · gcc-mirror/gcc
// a copy of the GCC Runtime Library Exception along with this program; // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see · // <http://www.gnu.org/licenses/>. · /** @file include/cstdlib · * This is a Standard C++ Library file. You should @c #include this file · * in your programs, rather than any of the @a *.h implementation files. * * This is the C++ version of the Standard C Library header @c stdlib.h, * and its contents are (mostly) the same as that header, but are all ·
Author   gcc-mirror
Find elsewhere
🌐
Reddit
reddit.com › r/c_programming › force gcc to require #include for standard libraries.
r/C_Programming on Reddit: Force GCC to require #include for standard libraries.
August 9, 2022 -

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?

Top answer
1 of 5
15
On the version of gcc I'm using (8.3.0) it will warn about implicit function definitions, which is what you get if you try to use stuff without the appropriate headers. e.g.: int main(int argc, char *argv[]) { printf("Hello, World!\n"); } Here we use printf which is in stdio.h, but we don't include it. By default I get some warnings, and a suggestion of what to fix it: $ gcc -o foo foo.c foo.c: In function ‘main’: foo.c:2:3: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration] printf("Hello, World!\n"); ^~~~~~ foo.c:2:3: warning: incompatible implicit declaration of built-in function ‘printf’ foo.c:2:3: note: include ‘’ or provide a declaration of ‘printf’ +#include int main(int argc, char *argv[]) { printf("Hello, World!\n"); ^~~~~~ $ However if I add the -Werror flag, it will fail to compile: $ gcc -Werror -o foo foo.c foo.c: In function ‘main’: foo.c:2:3: error: implicit declaration of function ‘printf’ [-Werror=implicit-function-declaration] printf("Hello, World!\n"); ^~~~~~ foo.c:2:3: error: incompatible implicit declaration of built-in function ‘printf’ [-Werror] foo.c:2:3: note: include ‘’ or provide a declaration of ‘printf’ +#include int main(int argc, char *argv[]) { printf("Hello, World!\n"); ^~~~~~ cc1: all warnings being treated as errors $ Additionally, although it's bad practice to have implicit definitions of functions and the warnings these generate, in a lot of cases (such as with printf) the default definition is close enough for the code to work.
2 of 5
8
somehow its GCC compiler can compile C++ with the .c file extension, maybe a bug. Not necessarily a bug. Compilers will often make assumptions based on the extension, but it's perfectly legal (bad form) for a C++ source file to have a .c extension. For that matter, it could have a .foo extension, or none at all.
🌐
Arch Linux Forums
bbs.archlinux.org › viewtopic.php
[unsolved] gcc 12.1.2-2 and stdlib.h for a c++ program / Programming & Scripting / Arch Linux Forums
#define _GLIBCXX_INCLUDE_NEXT_C_HEADERS #include_next <stdlib.h> ... Thanks by advance ! ... What package is this? The problem is most likely the include path which can be impacted by a wide range of compiler flags. Also, why is this thread's title include [unsolved]? ... $ pacman -Ql gcc | grep stdlib.h gcc /usr/include/c++/12.2.1/stdlib.h gcc /usr/include/c++/12.2.1/tr1/stdlib.h $
🌐
GitHub
github.com › gcc-mirror › gcc › tree › master › libstdc++-v3
gcc/libstdc++-v3 at master · gcc-mirror/gcc
April 2, 2020 - Contribute to gcc-mirror/gcc development by creating an account on GitHub.
Author   gcc-mirror
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-4.7.0 › libstdc++ › api › a01065_source.html
libstdc++: cstdlib Source File
00030 * 00031 * This is the C++ version of the Standard C Library header @c stdlib.h, 00032 * and its contents are (mostly) the same as that header, but are all 00033 * contained in the namespace @c std (except for names which are defined 00034 * as macros in C). 00035 */ 00036 00037 // 00038 // ISO C++ 14882: 20.4.6 C library 00039 // 00040 00041 #pragma GCC system_header 00042 00043 #include <bits/c++config.h> 00044 00045 #ifndef _GLIBCXX_CSTDLIB 00046 #define _GLIBCXX_CSTDLIB 1 00047 00048 #if !_GLIBCXX_HOSTED 00049 // The C standard does not require a freestanding implementation to 00050 /
🌐
GitHub
github.com › rstudio › sass › issues › 120
-stdlib=libc++ should be conditional on compiler and not hardcoded on macOS · Issue #120 · rstudio/sass
December 7, 2022 - These flags in Makefile should be conditional on compiler and not just OS: ifeq ($(UNAME),Darwin) CFLAGS += -stdlib=libc++ CXXFLAGS += -stdlib=libc++ LDFLAGS += -stdlib=libc++ endif Otherwise build with GCC is broken: /opt/local/bin/gcc-...
Author   rstudio
🌐
GNU
gcc.gnu.org › onlinedocs › libstdc++ › libstdc++-html-USERS-4.3 › a02013.html
libstdc++: stdlib.h Source File
March 26, 2008 - 00001 // TR1 stdlib.h -*- C++ -*- 00002 00003 // Copyright (C) 2006, 2007 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library.
🌐
Nongnu
nongnu.org › avr-libc › user-manual › group__avr__stdlib.html
avr-libc: <stdlib.h>: General utilities
The random() function computes a sequence of pseudo-random integers in the range of 0 to RANDOM_MAX (as defined by the header file <stdlib.h>).