Search code examples
c++g++

C++ programs, compiling with g++


I am very aware of compiling C++ programs with g++ in linux environment. But, may be I am missing something, I am getting this strange output/behaviour.

I have source file in test.cpp. To compile this, I did

(1)
g++ -c test.cpp 
g++ -o test test.o
./test

Everything works fine. But when I did compling and linking in same stage, like this

(2)
g++ test.cpp -o test 
./test => Works fine
(3)
g++ -c test.cpp -o test => Doesn't work

In my last case, test is generated but is no more executable; but in my guess it should work fine. So, what is wrong or do I need to change some settings/configuration?

I am using g++ 4.3.3


Solution

  • When you say:

    g++ -c test.cpp -o test
    

    The -c flag inhibits linking, so no executable is produced - you are renaming the .o file.

    Basically, don't do that.