Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile that compiles all cpp files in a directory into separate executable

Tags:

c++

makefile

I am now studying C++. I want a makefile which will compile all of the cpp files in the current directory to separate executables. For example:

In a directory there are 3 c++ files, such as examp1.cpp, examp2.cpp and examp3.cpp. I want a makefile which will compile and link them and give examp1.exe, examp2.exe and examp3.exe

I have created a bash script to compile all of them and create exes but I think; that's not the exact way to do this.

I have a a Makefile for ".c", but that does not seem to work here. It is only creating object files and not actually linking it. It is as follows:

SRCS=$(wildcard *.c)
OBJS=(SRCS:.c=.o)
all: $(OBJS)

The above code compiles all the new and modified ".c" files to ".o" files with same name in the current directory.

The bash script I am using to create executables is as follows:

for i in ./*.cpp
do
   g++ -Wno-deprecated $i -o `basename $i .cpp`".exe"
done

This means I want whatever ".cpp" files I put in that directory, by using a simple "make all" or anything like that it should compile.

like image 258
kaushik Avatar asked Mar 20 '12 12:03

kaushik


3 Answers

My answer builds on top of the answer by @Haatschii

I don't prefer to have the .out prefix to my binaries. Also I used his existing Make syntax to perform clean as well.

CXX=clang++
CXXFLAGS=-Wall -Werror -std=c++11

all: $(patsubst %.cpp, %.out, $(wildcard *.cpp))

%.out: %.cpp Makefile
        $(CXX) $(CXXFLAGS) $< -o $(@:.out=)

clean: $(patsubst %.cpp, %.clean, $(wildcard *.cpp))

%.clean:
        rm -f $(@:.clean=)
like image 129
zapstar Avatar answered Oct 14 '22 07:10

zapstar


A minimal Makefile that does what you want would be:

#Tell make to make one .out file for each .cpp file found in the current directory
all: $(patsubst %.cpp, %.out, $(wildcard *.cpp))

#Rule how to create arbitary .out files. 
#First state what is needed for them e.g. additional headers, .cpp files in an include folder...
#Then the command to create the .out file, probably you want to add further options to the g++ call.
%.out: %.cpp Makefile
    g++ $< -o $@ -std=c++0x

You'll have to replace g++ by the compiler you're using and possibly adjust some platform specific setting, but the Makefile itself should work.

like image 24
Haatschii Avatar answered Oct 14 '22 05:10

Haatschii


This is the Makefile that I use

CC = gcc
CFLAGS = -g -O2 -std=gnu99 -static -Wall -Wextra -Isrc -rdynamic -fomit-frame-pointer
all: $(patsubst %.c, %.out, $(wildcard *.c))
%.out: %.c Makefile
    $(CC) $(CFLAGS) $< -o $@ -lm
clean:
    rm *.out                      

You should paste it somewhere in your home and whenever you change the dirctory just copy it there. I use an alias in my ~/.basrc to copy it

alias get_makefile_here='cp ~/Makefile ./'

Simply press make and bam, you're done. Also notice the fact that once you're done with the old files it will not rebuild their executable.

like image 44
Shubham Chaudhary Avatar answered Oct 14 '22 06:10

Shubham Chaudhary