Link to home
Start Free TrialLog in
Avatar of punit
punit

asked on

in Makefile using gcc but getting error g++ not found !?

I am new to C++ development. Have some file and trying to compile using Makefile. Makefile is :
------------------------
simp_obj = autSk.o sk.o autexp_main.o
simp_obj2= csk.o sk.o autexp2_main.o
all : autexp autexp2

autexp: $(simp_obj)
        ./gcc -o autexp $(simp_obj)

autexp2: $(simp_obj2)
        ./gcc -o autexp2 $(simp_obj2)

Socket: sk.cpp
ssk: sk.cpp
csk: csk.cpp
autexp_main: autexp_main.cpp
autexp2_main: autexp2_main.cpp

clean:
        rm -f *.o autexp autexp2
------------------------

and compiling as ( pwd is the directory that contains my source files):
makefile -f ./Makefile

and getting  follwoing error:
g++    -c -o sk.o sk.cpp
make: g++: Command not found

Why it is picking g++ compiler while I exclusively using gcc ?
(I copied gcc in my current dir i.e. source dir. I think it should not be related to the problem)
Thanks,
PUNIT
Avatar of Rahul Singh
Rahul Singh
Flag of India image

all: hello

hello: main.o factorial.o hello.o
      g++ main.o factorial.o hello.o -o hello

main.o: main.cpp
      g++ -c main.cpp

factorial.o: factorial.cpp
      g++ -c factorial.cpp

hello.o: hello.cpp
      g++ -c hello.cpp

clean:
      rm -rf *o hello



use this as a refrence where hello is the name of exe u can change it and u cant give ./g++ like this its should be in above syntaxxxx if u want to link library
just put -lib name for ex

int his line   hello: main.o factorial.o hello.o
      g++ main.o factorial.o hello.o -o hello -lpthread
Avatar of punit
punit

ASKER

aatrish2001, question is, why gcc need to be replaced with g++? and from where make program is taking g++ while I mentioned gcc ?
Avatar of evilrix
The semantics of gcc and g++ are different. When you build with gcc the compiler doesn't automatically assume you are building C++ code so won't automatically link against all standard C++ libraries so you'll have to manually provide the additional command line to do this. When you compile with g++ you are effectively telling gxx, "look buddy I really do want to link against all those big and nasty C++ libraries so just flipping do it!". :)
...or to put it another way, by default gcc is a C compiler and g++ is a C++ compiler.
>> from where make program is taking g++ while I mentioned gcc ?
It's based upon the file extension: .cpp means c++ code so g++ is being used, .c means c code so gcc will be used.
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> It's based upon the file extension: .cpp means c++ code so g++ is being used, .c means c code so gcc will be used.

Exactly.

A rule like this :

        file.o : file.cpp

without anything else tells make to pick the compiler suited to compile the file.cpp source file into the file.o object file. Since file.cpp has a .cpp extension, it will pick a C++ compiler, so it will pick g++. If you explicitly want to use gcc, then do this :

        CFLAGS = -Wall
        CC = gcc

        file.o : file.cpp
              $(CC) $(CFLAGS) -c $? -o $@

Note that $@ is the name of the file to be made (file.o in this case), and $? is the list of the dependents (file.cpp in this case).

You can also make that a general rule that compiles .cpp files to .o files :

        %.o : %.cpp
              $(CC) $(CFLAGS) -c $< -o $@


However, as evilrix already said, if the file extension is .cpp, then it probably contains C++ code, and you shouldn't compile it with gcc, but with g++. If it doesn't contain C++ code, then you should give it a .c extension instead of a .cpp extension.
since the gcc compile wont automaticall understand that u are compiling cpp file so we use g++ instead of gcc
>> since the gcc compile wont automaticall understand that u are compiling cpp file so we use g++ instead of gcc
Didn't I already say that?
>> g++: Command not found
Often, g++ has to be installed seperately from gcc. If you can tell us what Linux distro you are using I can tell you how to install it.
If you look at the output from "man gcc" you will see an explanation. gcc invokes the right compiler for the file suffix, including

.c C compiler gcc (a different gcc)
.cc, .cpp, .cxx C++ compiler g++
.f FORTRAN compiler (was g77, recently is g99 I think)

There are also variants that don't or do specify to run through the C preprocessor (cpp, also built-in to gcc). It's all in the man page, and "info gcc" tells you a whole lot more - it's actually the gcc manual.

So by naming your file with a .cpp suffix, you have told gcc that it is a C++ file. If it's actually C, rename it and amend your Makefile. Be sure though, that it's not C++ (hint: look for the "::" operator, if you see it then it *is* C++).
As posted earlier, the g++ compiler is an optional part of gcc install or may be packaged separately. You may also need other packages like libstdc++-devel (that name is a guess)
I see you copied gcc to rour home directory - possibly that is your problem. gcc actually has associated with it an entire directory tree containing components of the compiler (like the real C compiler, private librarioes, private and "fixed" public header files and so on). Don't think you can just copy gcc - you should do a full install from the source directory specifying your home directory as the prefix.
or u can see this here in we r taking variable and assigning value to it for exampel in case of c prgram we will make CC=gcc and in cpp we can make it as g++ like that we can take variable for compile option as well as libraries which u can see in the exampel

OBJS = main.o header.o
CC = g++
DEBUG = -g
CFLAGS = -Wall -c $(DEBUG)
LFLAGS = -Wall $(DEBUG)
LIB = -lpthread
new : $(OBJS)
      $(CC) $(LFLAGS) $(OBJS) -o new $(LIB)

main.o : main.cpp header.h
      $(CC) $(CFLAGS) main.cpp

header.o : header.cpp header.h
      $(CC) $(CFLAGS) header.cpp



clean:
      rm -rf *.o *~ new
The asker's problem is not with his Makefile. His problem could be that he copied gcc into his home directory - he should remove that copy (providing gcc also resides elsewhere, e.g. /usr/bin/gcc or whatever). His problem may be that g++ was not installed anywhere in which case he needs to install it. A file ending .cpp needs to compiled with g++ - gcc knows that.