5

As is well-known, when we compile and link c++ programs, the standard library is linked in automatically. Is it possible to avoid this? I've written my own native library and I want to link it only.

3
  • 4
    Yes, implementations have ways to avoid linking in the standard library. However they don't all do the same thing so you'll simply have to check your compiler's documentation. Looks like one pretty common option is passing the flag -nostdlib to the C++ compiler. Commented May 8, 2014 at 5:42
  • This will be compiler-specific, so you should tell us which compiler you're using. Commented May 8, 2014 at 5:43
  • 1
    Then it's just what @bames53 said, -nostdlib is what you're looking for. To make sure that you're not bringing in any dependency without knowing, you may also want to use -nostdinc, which will tell the compiler to not look for stdlib headers. Commented May 8, 2014 at 5:45

2 Answers 2

7

Yes, it is possible, at least if you are using Visual Studio C++ or g++.

Compiler Options

If you use Visual Studio C++, lookup the option /X

If you use g++, lookup the option -nostdinc++.

Linker Options

If you use Visual Studio C++, lookup the option /NODEFAULTLIB.

If you use g++, lookup the option -nostdlib.

Sign up to request clarification or add additional context in comments.

2 Comments

For the compiler, it is -nostdinc++ for g++ and /X in MSVC.
@ManuelAtWork, thanks for the additional info.
2

If you are using g++, you can use g++ option -nostdlib to avoid linking to both standard libraries AND start files automatically; and use option -nodefaultlibs to only avoid linking to standard libraries automatically, it still will link to start files automatically.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.