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
Release: 2024-12-31 03:00:14
Original
451 people have browsed it

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
Copy after login

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

g++ -Wall -Wextra -Werror -c main.cpp -o main.o
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template