开发者

Makefile - Generate Targets from Sources

I'm开发者_StackOverflow trying to build a growing number of .cpp files with the use of make. They all reside in a subdirectory src/. I want to compile each file into it's own executable, residing in bin/. This is what I have tried so far, but it doesn't work, and since I'm new to make, all the Patterns, Variables etc. make it difficult to pinpoint what gets expanded into what and so on.

CXX = g++
CXXFLAGS = -Wall -Wextra -pedantic

SRC = $(wildcard src/*.cpp)
BIN = $(patsubst src/%.cpp,bin/%,$(SRC))

all: $(BIN)

%: scr/%.cpp
    $(CXX) $(CXXFLAGS) $< -o $@

Also, is there a way to show just the contents of BIN for example to allow me to see what is going on?


This is what your final expansion looks like

file: scr/bin/file.cpp

Why not do this?

BIN = $(patsubst src/%.cpp,%,$(SRC))

all: $(addprefix bin/, $(BIN))

bin/% : src/%
    $(GCC) $(FLAGS) $< -o $@

I think that works?

As for showing the contents of BIN

all: $(addprefix bin/, $(BIN))
    @echo $(BIN)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜