Home > Net >  Link explicitly to libc (-lc)
Link explicitly to libc (-lc)

Time:09-17

Should I explicitly link my program or library against libc, when using libc symbols?

i.e., cc ... -lc

The question applies to the Makefile in case of a program, and both the Makefile and the pkg-config file in case of a library.

And as a side question: what if I don't use any libc symbols at all? That would probably be a very useless program, but I'm curious.

I seem to recall some article or book recommending doing so, but now I can't find it. Is there any situation in which doing so or not doing it can be problematic?

Edit:

I'm normally using gcc and clang. (I also edited the third paragraph to explicitly ask about Makefiles and pkg-config files.)

CodePudding user response:

Should I explicitly link my program or library against libc, when using libc symbols?

No. From POSIX cc:

The standard C-language library is automatically available to the C-language program.

what if I don't use any libc symbols at all?

Then still do not link explicitly against libc and let compiler link with it automatically.

Is there any situation in which doing so or not doing it can be problematic?

When you are writing a kernel, bare-metal software, or other low-level program specific to some environment you are working with, and you do not want to link the standard library. Also, when you do not accept the license of the library.

CodePudding user response:

what if I don't use any libc symbols at all?

Then your program is freestanding and does not require linking with the standard library.

If you use gcc you need to compile it with -nostdlib command-line option.

  • Related