Home >Backend Development >C++ >Why Does My C Code Get 'undefined reference to 'std::cout'' and How Do I Fix It?

Why Does My C Code Get 'undefined reference to 'std::cout'' and How Do I Fix It?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-31 03:00:14441browse

Why Does My C   Code Get

Undefined Reference to 'std::cout'

When compiling C programs that utilize the standard library, such as , errors like "undefined reference to 'std::cout'" can arise. This occurs when the compiler cannot find the definition of the symbol 'std::cout'.

One potential solution is to use the appropriate C compiler, such as g , which can automatically link to the standard library. Alternatively, when using gcc, explicit linking with -lstdc is necessary, as follows:

gcc main.cpp -lstdc++ -o main.o

To quell warnings during compilation, consider using the -Wall and -Wextra flags:

g++ -Wall -Wextra -Werror -c main.cpp -o main.o

The above is the detailed content of Why Does My C Code Get 'undefined reference to 'std::cout'' and How Do I Fix It?. 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