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
punitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aatrish2001Commented:
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
0
aatrish2001Commented:
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
0
punitAuthor Commented:
aatrish2001, question is, why gcc need to be replaced with g++? and from where make program is taking g++ while I mentioned gcc ?
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

evilrixSenior Software Engineer (Avast)Commented:
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!". :)
0
evilrixSenior Software Engineer (Avast)Commented:
...or to put it another way, by default gcc is a C compiler and g++ is a C++ compiler.
0
evilrixSenior Software Engineer (Avast)Commented:
>> 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.
0
evilrixSenior Software Engineer (Avast)Commented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
>> 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.
0
aatrish2001Commented:
since the gcc compile wont automaticall understand that u are compiling cpp file so we use g++ instead of gcc
0
evilrixSenior Software Engineer (Avast)Commented:
>> 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?
0
evilrixSenior Software Engineer (Avast)Commented:
>> 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.
0
Duncan RoeSoftware DeveloperCommented:
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)
0
Duncan RoeSoftware DeveloperCommented:
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.
0
aatrish2001Commented:
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
0
Duncan RoeSoftware DeveloperCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.