Home >Backend Development >C++ >Why Do I Get Unresolved Linker Symbols When Compiling C Code?
Unresolved Linker Symbols When Compiling C Code
When attempting to compile C code using the C front-end, such as by invoking gcc, you may encounter errors due to unresolved symbols related to the standard C library. This occurs when the linker is not properly configured to include the necessary C libraries.
Identifying the Issue
The error message typically provides clues about the missing symbols, such as "std::cout" or "std::ios_base::Init". These symbols are essential for C functionality.
Resolution
To resolve the issue, ensure that you are using the appropriate compiler that understands and links C code, such as g or clang . These compilers automatically include the correct libraries.
If you are using gcc or clang directly, you can add the "-lstdc " flag to the link line to explicitly specify the C standard library.
Additional Information
g++ example.cpp clang++ example.cpp
If you are using clang, you can use the "-v" flag to view the link line and verify that it includes the "-lstdc " flag:
clang -v example.cpp
Conclusion
By using the appropriate compiler and ensuring that the C standard library is included, you can prevent linker errors when compiling C code.
The above is the detailed content of Why Do I Get Unresolved Linker Symbols When Compiling C Code?. For more information, please follow other related articles on the PHP Chinese website!