I've recently swithed from borland C++ 5.5 to MinGW. When I try to compile a .c file using gcc it compiles fine, but when I try to compile a .cpp file using gcc it produces a bunch of errors with c++ specific constructs. If I compile .cpp files with g++ then it works fine. My question is should not gcc call appropriate compilers based on the file extensions? Why does it work for .c files and not for .cpp files. As far as I knew gcc stands for GNU Compiler Collection and it should invoke appropriate compilers on its own. Why is it acting just like a C only compiler?

Recommended Answers

All 8 Replies

>As far as I knew gcc stands for GNU Compiler Collection and
>it should invoke appropriate compilers on its own.
It does invoke the appropriate compiler, but gcc doesn't know how to link C++ libraries while g++ does. I'd be willing to bet that the errors you're getting are linker errors.

can u plz tell the errors or the code which gives error....does it show
cpp: Usage:... Error

I'd be willing to bet that the errors you're getting are linker errors.

Yes, they are linker errors like undefined reference to XYZ. So how do I link C++ programs using gcc with ease?

>So how do I link C++ programs using gcc with ease?
You use g++. :rolleyes: The alternative is to manually add all of your libraries to the linker, which is far from "with ease".

Or, link it this this:

gcc exefile.exe objfile.obj -stdcxx

Or something like that. If that doesn't work, try libstd or libcxx Hope this helps.

It's usually -lstdcxx.

using lstdcxx , libstd and libcxx gives the following error

-----------------------------------
 ld.exe: cannot find -libcxx
 collect2: ld returned 1 exit status
 -----------------------------------

using stdcxx says unrecognised command line option.

I was actually trying to configure only one tool in my editor for compiling both C and cpp codes. If I have to use two different command line options then I still have to configure two different tools.

...
then use g++ for both c and c++ code since it can handle both (of course there is a switch of gcc that makes it behave like g++ but I dont have it in my head)...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.