debugcppCritical
Error "undefined reference to 'std::cout'"
Viewed 0 times
errorstdundefinedcoutreference
Problem
Shall this be the example:
It throws the error:
main.cpp:(.text+0xa): undefined reference to
main.o: In function
main.cpp:(.text+0x4c): undefined reference to
Also, this example:
throws the error:
main.cpp:(.text+0xa): undefined reference to
main.o: In function
main.cpp:(.text+0x4c): undefined reference to
Note: I am using Debian 7 (Wheezy).
#include
using namespace std;
int main()
{
cout << "Hola, moondo.\n";
}It throws the error:
gcc -c main.cpp gcc -o edit main.o main.o: In function main':main.cpp:(.text+0xa): undefined reference to
std::cout'
main.cpp:(.text+0xf): undefined reference to std::basic_ostream >& std::operator>(std::basic_ostream >&, char const*)'main.o: In function
__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0x3d): undefined reference to std::ios_base::Init::Init()'main.cpp:(.text+0x4c): undefined reference to
std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
Also, this example:
#include
int main()
{
std::cout << "Hola, moondo.\n";
}throws the error:
gcc -c main.cpp gcc -o edit main.o main.o: In function main':main.cpp:(.text+0xa): undefined reference to
std::cout'
main.cpp:(.text+0xf): undefined reference to std::basic_ostream >& std::operator>(std::basic_ostream >&, char const*)'main.o: In function
__static_initialization_and_destruction_0(int,int)': main.cpp:(.text+0x3d): undefined reference to std::ios_base::Init::Init()'main.cpp:(.text+0x4c): undefined reference to
std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
Note: I am using Debian 7 (Wheezy).
Solution
Compile the program with:
`
g++ -Wall -Wextra -Werror -c main.cpp -o main.o
^^^^^^^^^^^^^^^^^^^^
as cout is present in the C++ standard library, which would need explicit linking with -lstdc++ when using gcc; g++ links the standard library by default.
With gcc, (g++ should be preferred over gcc)
gcc main.cpp -lstdc++ -o main.o`
Context
Stack Overflow Q#28236870, score: 491
Revisions (0)
No revisions yet.