• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Creating a makefile

sak

Joined
8/16/09
Messages
84
Points
18
I am trying to write a makefile which can do the following:

- compile my files: Main.cpp and Headers.h (Headers.h is called in Main.cpp and contains some header declarations) to create the executable Main.exe
- compiler is g++
- after compiling, runs the executable Main.exe

I did create a makefile after doing some reading on the internet but it doesn't work. Can anyone suggest something working?
 
Until you post your makefile and say what exactly doesn't work there is not too much to say.
 
One simple Makefile may look like this. Please note "^I" means tab. The executable generated by g++ don't have to suffix with ".exe", which is Windows PE(Portable Executable) format. Linux uses ELF.

#cat Makefile
Main: Main.cpp Headers.h
^Ig++ Main.cpp -g -o Main

clean:
^Irm -fr *.o Main

all: Main
----
To compile and run your program, do like this.

#make;./Main
 
If you really want to run executable from your makefile, then you could certainly do, and one possible approach is as follows (beware of indented lines, you should use tabs there, as mentioned in previous reply):
C++:
.PHONY: all run

all: Main run

Main: Main.cpp Headers.h
    g++ -o Main Main.cpp

run:
    ./Main
But this is plain ugly, so it's much better that you run the make, and then run your program in the same command, again as shown in previous reply, or even better this way (so that the executable will be run only if build step successful):
C++:
make && ./Main
 
Make sure you didn't confuse spaces for tabs, or vice versa, for Makefiles. This is one of the nastiest problems.
 
Back
Top