0

I want to make use of a more recent GCC version. Therefore, I compiled GCC 10 and installed it into a non-standard directory /software/gcc10/. So far so good.

However, I am faced with problems when I actually want to use this new version of GCC. I modified my PATH variable to include /software/gcc10/bin/ such that gcc --version now confirms that the system finds my new GCC 10 compiler (instead of my system's GCC 8 one).

However, when I try to compile a program with this setup, I get errors about undefined references to std::filesystem, which should be part of GCC 10's libstdc++. My assumption here is that while I am now using a more recent compiler, the linker still wants to link against my system's libstdc++ instead of that in /software/gcc10/lib64.

I tried changing LD_LIBRARY_PATH to include the respective directory, but that doesn't seem to have any effect.

Inspecting the output of running gcc with the -v option, I stumbled upon the LIBRARY_PATH environment variable, which according to gcc's docs is used as follows:

The value of LIBRARY_PATH is a colon-separated list of directories, much like PATH. When configured as a native compiler, GCC tries the directories thus specified when searching for special linker files, if it cannot find them using GCC_EXEC_PREFIX. Linking using GCC also uses these directories when searching for ordinary libraries for the -l option (but directories specified with -L come first).

And indeed: Setting the LIBRARY_PATH variable accordingly allows me to compile my program successfully. However, it seems that this is a gcc-specific solution and I can't help but wonder whether there is a more standard way of doing this (that would e.g. also tell clang where to look for the C++ standard library to link against)?

Even better would be a solution that would cause my new GCC 10 compiler to always use the GCC 10 stdlib whilst invocation of the system's GCC 8 compiler would always link against the systems GCC 8 stdlib. Is there a way to tell a specific compiler binary that it has a specific stdlib that it should use?

1 Answer 1

1

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
1
  • Great! That's good to know, thank you!
    – Raven
    Mar 16 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.