Thursday, August 26, 2010

Compiling cpp with gcc

Hi,
I know the title is a bit confusing. But believe me, i am not trying to confuse :).
In very layman terms , g++ is nothing but a special version of gcc.
Ignoring all the complexities, the most trivial work it does is making cpp the default
language, which results in stdcpp library to get linked by default.

So, if you want to play a trick and compile your cpp code with gcc, just link the stdc++ lib by yourself.

$ gcc file1.cpp -lstdc++ -o output

This will do the trick.
Have tested it with basic helloworld, so if your complex template containing code doesn't compile with this, don't complain, more over you should give a thought about what template is actually doing to your code :).

1 comment:

  1. In place of $ gcc file1.cpp -lstdc++ -o output
    $ gcc file1.cpp -ld -o output
    or $ gcc file1.cpp -o output -ld
    can be applied.
    One more thing at the time of compilation gcc gives no problem with .cpp files, but for linking you have to include the c++ library.

    ReplyDelete