Home >Backend Development >C++ >Why Does Compiling `info.c` with GCC Require the `-lstdc ` Flag?

Why Does Compiling `info.c` with GCC Require the `-lstdc ` Flag?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-29 19:25:21497browse

Why Does Compiling `info.c` with GCC Require the `-lstdc  ` Flag?

Compiling C Programs with GCC

While GCC is capable of compiling C code, the compiler errors encountered when attempting to compile "info.c" are due to linker errors. These errors stem from the lack of linking to the standard C library.

To resolve this issue, add the -lstdc flag to the compilation command:

gcc info.C -lstdc++

Alternatively, a simpler solution is to utilize g , which does the following:

  • Selects the appropriate back-end compiler based on file extension, similar to GCC.
  • Compiles all C source files as C , contrasting with GCC's compilation of .c files as C.
  • By default, includes libstdc in the linking step, regardless of input languages.

The following excerpt from a comment by Rup succinctly explains the differences:

"GCC will select the correct back-end compiler based on file extension. However, it links binaries against just the standard C and GCC helper libraries by default. In contrast, g includes libstdc in its linking step by default, regardless of input languages."

The above is the detailed content of Why Does Compiling `info.c` with GCC Require the `-lstdc ` Flag?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn