9
votes

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.

4
Have you made any attempt at trying to create the makefile? If so post it so we can help you. There is documentation available to get started.Joe
The versions of make vary from one supplier to another. What make are you using? Are you using GNU Make on Linux? Microsoft Make on Windows/Visual C++?Robᵩ
Have you tried writing a makefile that will compile one source file and produce an executable?Beta

4 Answers

18
votes

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.

3
votes

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.

1
votes

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=)
0
votes

The simplest makefile you can create that might work for you is this:

all: examp1.exe examp2.exe examp3.exe

That will use make's default rules to create your three programs.