domlao
domlao

Reputation: 16029

Exclude source file in compilation using Makefile

Is it possible to exclude a source file in the compilation process using wildcard function in a Makefile?

Like have several source files,

src/foo.cpp
src/bar.cpp
src/...

Then in my makefile I have,

SRC_FILES = $(wildcard src/*.cpp)

But I want to exclude the bar.cpp. Is this possible?

Upvotes: 95

Views: 72118

Answers (5)

Iwan de Jong
Iwan de Jong

Reputation: 51

If you're trying to add that all into one call:

g++ -g $(filter-out excludedFile.cpp, $(wildcard *.cpp)) -o main where the normal one would be g++ -g *.cpp -o main

And if you want to run it as well:

g++ -g $(filter-out excludedFile.cpp, $(wildcard *.cpp)) -o main && ./main

Upvotes: 0

Beta
Beta

Reputation: 99094

If you're using GNU Make, you can use filter-out:

SRC_FILES := $(wildcard src/*.cpp)
SRC_FILES := $(filter-out src/bar.cpp, $(SRC_FILES))

Or as one line:

SRC_FILES = $(filter-out src/bar.cpp, $(wildcard src/*.cpp))

Upvotes: 173

ma11hew28
ma11hew28

Reputation: 126329

The Unix glob pattern src/[!b]*.cpp excludes all src files that start with b.

That only would work, however, if bar.cpp is the only src file that starts with b or if you're willing to rename it to start with a unique character.

Upvotes: 5

K1773R
K1773R

Reputation: 982

use find for it :)

SRC_FILES := $(shell find src/ ! -name "bar.cpp" -name "*.cpp")

Upvotes: 19

Dima Chubarov
Dima Chubarov

Reputation: 17169

You can use Makefile subst function:

 EXCLUDE=$(subst src/bar.cpp,,${SRC_FILES})

Upvotes: 7

Related Questions