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?

Dani AI

Generated

Short answer: those “undefined reference” messages are linker errors — gcc will invoke the C++ front end for .cpp files but it does not automatically add the C++ runtime/library when linking; g++ does. That’s why compiling with g++ works while using gcc for the final link fails. This is the behavior documented in the GCC manual. (gcc.gnu.org)

Practical fixes:

  • Easiest: use g++ as your single editor tool for C++ builds (it will compile by extension and automatically link the C++ standard library). Example:

    g++ foo.cpp -o foo
  • If you must use gcc to drive the link step, explicitly add the standard C++ library to the link line (the correct linker flag is -lstdc++ — note the two plus signs). Example:

    gcc -c foo.cpp        # compile step (produces foo.o via the C++ front end)
    gcc foo.o -o foo -lstdc++

Both approaches are equivalent at link time when -lstdc++ is supplied; g++ simply appends that (and some C++-specific link options) for you. (stackoverflow.com)

A few extra notes tied to the thread: was right that it’s a link issue; was close to the idea of a library flag — the spelling matters. On MinGW you may also run into runtime DLL/search-path issues for libstdc++; if you want a very small C++ runtime you can link libsupc++ instead in special freestanding builds (advanced). Use the -x option to force language selection if you ever need to override suffix-based behavior. (gcc.gnu.org)

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.