help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: makefile for generating exe from single cpp


From: Paul Smith
Subject: Re: makefile for generating exe from single cpp
Date: Sat, 18 Oct 2014 10:37:43 -0400

> On Fri, Oct 17, 2014 at 1:51 PM, Paul Smith <address@hidden> wrote:
> > If you provide us the output of the make command we can be more helpful.

On Sat, 2014-10-18 at 07:48 +0100, Amit Chaudhuri wrote:
> address@hidden:~/prog/cpp/test> make -n
> g++    -c -o main.o main.cpp
> cc   main.o   -o main

This is all I asked for, and all we needed to see.

Here it's clear that make DOES use "g++" to compile your C++ file into
a .o file, because make can see that in order to build a .cpp file into
a .o file it needs to use a C++ compiler.

However the next step, to turn a .o file into an executable, can be done
in any of a large number of ways.  The .o file could be generated from a
C compiler, C++ compiler, FORTRAN compiler, or various other types of
compilers.  Since make can choose only one by default it chooses to use
the C linker.

There are various solutions depending on your situation.  If you really
only have one source file and that's all you'll ever have, you can
change your makefile like this:

    main: main.cpp

(have the executable depend on the source file not the object file).
Now make can guess that it needs to use the C++ compiler and linker
since it's doing things in one step.  And since you only have one file
to compile anyway you're not losing any efficiency.

If you have more than one source file, you should either change the
built-in LINK.o variable to use the C++ compiler:

    LINK.o = $(CXX) $(LDFLAGS) $(TARGET_ARCH)

    main: main.o foo.o bar.o baz.o

Or else write the link command explicitly:

    main: main.o foo.o bar.o baz.o
            $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]