21

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;
};
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
spatara
  • 893
  • 4
  • 15
  • 28
  • 3
    What do you mean that g++ doesn't do anything? – Troubadour Feb 25 '12 at 19:45
  • 3
    `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). – hmjd Feb 25 '12 at 19:46
  • 3
    @hmjd: g++ **is** gcc, it just automatically links against libstdc++. You can get the same effect with gcc by supplying `-lstdc++`. – Oliver Charlesworth Feb 25 '12 at 19:48
  • What @hmjd said: after running `g++` you want to run the file which, with no other options, is called `a.out`. – smparkes Feb 25 '12 at 19:49
  • by it doesn't do anything I mean it doesn't print to the console...which i think it should – spatara Feb 25 '12 at 19:56
  • 3
    @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. – Mike Seymour Feb 25 '12 at 20:12
  • 2
    @spatara If you don't like the silence, you can pass the `--verbose` flag and it'll tell you more than you want to know. – Daniel Fischer Feb 25 '12 at 20:41
  • g++ dosen't do anything? Look at the new file in the folder. Not doing anything is compilation success! – ihsoy ih Dec 20 '12 at 15:10

5 Answers5

86
g++ main.cpp

or

gcc main.cpp -lstdc++
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
kev
  • 155,172
  • 47
  • 273
  • 272
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hmjd
  • 120,187
  • 20
  • 207
  • 252
5

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bisarch
  • 1,388
  • 15
  • 31
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

Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
-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".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 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?](https://stackoverflow.com/questions/221185/)*. – Peter Mortensen May 08 '22 at 13:52