Dumbo
Dumbo

Reputation: 14132

Creating a makefile for g++ compiler

I have some code that everytime I want to compile I have to do the following command in the linux shell (just an example):

$g++ -I. -I$SYSTEMC_HOME/include -L. -L$SYSTEMC_HOME/lib-linux64 -o sim helloworld.cpp -lsystemc -lm

In this example my source file is helloworld.cpp and I want it to be compiled in a folder called sim in the same folder where the source file is.

How can I automate this process using make or makefile. Also having the make clean option to remove the created folder if called?

Upvotes: 0

Views: 543

Answers (2)

user657267
user657267

Reputation: 21040

If you rename your cpp file to sim.cpp this is probably the simplest Makefile

CPPFLAGS := -I. -I$(SYSTEMC_HOME)/include
LDFLAGS  := -L. -L$(SYSTEMC_HOME)/lib-linux64
LDLIBS   := -lsystemc -lm

sim:

.PHONY: clean
clean: ; $(RM) sim

The implicit rules should handle everything else for you

Upvotes: 1

Joel
Joel

Reputation: 2035

Easy

%.o: %.cpp
     g++ $(YOUR_CXX_FLAGS_HERE) -c $< -o $@

$(YOUR_PACKAGE_NAME_HERE): $(YOUR_OBJECTS_HERE)
     g++ $(SOME_LDFLAGS_HERE) $^ -o $@

clean:
     rm $(YOUR_PACKAGE_NAME_HERE) $(YOUR_OBJECTS_HERE)

.PHONY: clean

There are good tutorials outthere, like this one.

Upvotes: 1

Related Questions