Exclude source file in compilation using Makefile

2019-01-13 03:15发布

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?

标签: c++ makefile
4条回答
爷的心禁止访问
2楼-- · 2019-01-13 03:34

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))
查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-13 03:36

You can use Makefile subst function:

 EXCLUDE=$(subst src/bar.cpp,,${SRC_FILES})
查看更多
干净又极端
4楼-- · 2019-01-13 03:42

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.

查看更多
等我变得足够好
5楼-- · 2019-01-13 03:51

use find for it :)

SRC_FILES := $(shell find src/ ! -name "bar.cpp" -name "*.cpp")
查看更多
登录 后发表回答