g++ vs gcc makefile?

duragezic

Lifer
Oct 11, 1999
11,234
4
81
I'm only familiar with makefiles for C++ since I've had a C++ class and not C.

The program I'm doing now is most likely easiest done in C since the base code we were given is all C. I'm not too sure how to mix C and C++ so I figured just straight C is fine since I need to learn it better and I don't need C++ features (though a STL Vector would help with one task!). It's just a simple shell program to use Unix commands and signals to execute commands in a child process, so C seems like the best way to go.

I posted the makefile I used for all of my C++ programs using g++. This whole time I had been compiling this C program with the g++ makefile till I realized I should be using gcc?. It didn't occur to me right away since it compiled and worked with C++/g++ makefile. But one problem I had with the & operator (address of) might be attributed to using g++ since it maybe was interpreting it as a reference which C doesn't have AFAIK.

This same makefile I used for C++/g++ worked fine when I changed any 'g++' to 'gcc'. But the makefile examples on the internet show something quite different.

Both seem to work fine. Is there any difference I should know about and was using g++ for C code a mistake? I know -Wall is used to show warnings with gcc. But I'm not sure which one is correct or proper and I'm not too sure what all: is about. Ideas?
 

LintMan

Senior member
Apr 19, 2001
474
0
71
Your original g++ makefile should work fine.

At my previous workplace, we used g++ to compile our C-ish code all the time. (I say "C-ish" because it was bascally C code, but we had // comments). And C++'s stronger typing can be a big help sometimes.

If you need strict C portability, gcc would be better, and then there's even a -ansi flag for enforce stric ANSI compliance if you're that serious about it. But for your application, it sounds like g++ should be fine.

As for the makefile differences, -g enables extra debug information for use with a debugger, so that might be an important option to add if you want to run a debugger.

The -lm links in the standard math library "libm". You may not need this for your program. I generally don't worry about specifying libraries for the linker unless I get a linker complaint first.