1

On some platforms like Microsoft Windows no need to link against the "Standard C Library" with -lc flag, But on some other platforms it requires linking, But on macOS/OSX though it's Unix-based we don't link with -lc while we need to link on Linux and BSDs...

That made me a bit confused when writing Cross-Platform C libraries, Where and When to/not to link against the "Standard C Library" with -lc flag?

And is linking just for Linux and BSDs? Or also some other Unix platforms requires linking?

6
  • 2
    Sometimes when you invoke the linker directly it might be needed. Why do you wonder? What is the problem you have that lead to your question? Commented Jan 17, 2022 at 13:53
  • 1
    The best available general rule is that nowadays, there are very few C implementations that require you to link the standard library explicitly. I wouldn't worry much about it. Commented Jan 17, 2022 at 13:53
  • 2
    I've never had to manually link the standard library on clang or gcc, meanwhile on windows, VS will automatically add all of the standard Win32 libraries to the linker input list. Commented Jan 17, 2022 at 13:54
  • 3
    This would in my experience only be a problem when doing embedded systems programming, where the compiler is by default a "freestanding" implementation and need not provide all of the standard libs. Such systems without FPU might also have different standard libs depending on if you wish to support software floating point or not and so you need to explicitly tell it which standard lib to use. Commented Jan 17, 2022 at 13:54
  • 1
    When you compile on Windows, one of the include files will contain a directive #pragma comment(lib, "msvcrt.lib"). This get included in the .obj file so the linker can see what library to link. Commented Jan 17, 2022 at 14:08

1 Answer 1

3

Essentially every hosted C implementation requires you to link programs with an implementation of the standard C library.

Some commands for building programs have the inclusion of the standard C library built-in as a default, so you do not need to add it explicitly on the command library. Some commands do not have it as a default, so you do need to add it on the command line.

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

2 Comments

But on Microsoft Windows you don't have to link whatever C compiler... (Or i'm wrong?) Also, If you know anything other than Linux and BSD that requires linking can you mention them in a comment?
@Steria773: Different systems have different commands with different defaults and built-in behaviors. When you successfully build a hosted C program on or for a Microsoft Windows platform, it is linked to or with an implementation of the standard C library.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.