This very simple C++ program using the standard library doesn't compile with GCC [duplicate]
Asked Answered
S

5

20

I can't figure out why this isn't working...

I am working in Linux.

g++ doesn't do anything.

gcc prints the following:

/tmp/ccyg7NDd.o: In function `main':
test.cc:(.text+0x14): undefined reference to `std::cout'
test.cc:(.text+0x19): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
test.cc:(.text+0x21): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cc:(.text+0x29): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccyg7NDd.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cc:(.text+0x51): undefined reference to `std::ios_base::Init::Init()'
test.cc:(.text+0x56): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccyg7NDd.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Code:

#include <iostream>
#include <stdio.h>

int main(){

    std::cout << "test " << std::endl;
    return 0;
};
Sarmiento answered 25/2, 2012 at 19:44 Comment(8)
What do you mean that g++ doesn't do anything?Selfevident
gcc is the C compiler, you need to use g++. What do you mean g++ does not do anything? You have to execute the compiled binary after you build it (i.e. when g++ completes).Wiseman
@hmjd: g++ is gcc, it just automatically links against libstdc++. You can get the same effect with gcc by supplying -lstdc++.Bremen
What @Wiseman said: after running g++ you want to run the file which, with no other options, is called a.out.Diorama
by it doesn't do anything I mean it doesn't print to the console...which i think it shouldSarmiento
@spatara: The compiler will only print something if there's a problem. Like most Unix-style programs, silence means success. You should find a program called a.out, or something else if you gave a -o option to the compiler; running that should print test to the console.Sangfroid
@Sarmiento If you don't like the silence, you can pass the --verbose flag and it'll tell you more than you want to know.Emera
g++ dosen't do anything? Look at the new file in the folder. Not doing anything is compilation success!Vernation
P
86
g++ main.cpp

or

gcc main.cpp -lstdc++
Porterporterage answered 25/2, 2012 at 19:47 Comment(0)
W
22

gcc is the C compiler. You need to use g++ (or use gcc with option -lstdc++ as pointed out by others). If by nothing is printed after you use g++ is what you mean, you have to execute the compiled binary after you build it (i.e., when g++ completes).

main.cpp:

#include <iostream>

int main(){

    std::cout<<"test "<<std::endl;
    return 0;
};

Build:

g++ main.cpp -o main

Execute:

./main

Output:

test
Wiseman answered 25/2, 2012 at 19:49 Comment(0)
D
5

This is C++ code and so you should use executable g++ and not executable gcc.

Also, #include<stdio.h>, is not needed.

Dauntless answered 25/2, 2012 at 19:48 Comment(0)
N
2

I think you are mistakenly linking with the C compiler command instead of the C++ compiler command. Try this:

g++ test.cc -o test

Null answered 25/2, 2012 at 19:48 Comment(0)
A
-1

I think the underlying issue for this is that the binary name "test" is actually already apart of the Linux system. Typing in "man test" displays a manual for the test binary. I had the exact same issue. It was resolved simply by compiling the binary to something other than "test".

Adaurd answered 28/8, 2014 at 23:33 Comment(1)
But the real issue was that "test" was used instead of "./test"(?). The canonical question is How can I compile and run C/C++ code in a Unix console or Mac terminal?.Vulcanism

© 2022 - 2024 — McMap. All rights reserved.