当我 运行 `make debug` 时,附加一个额外的构建标志
Append the build flags with extra one when I run `make debug`
在我的例子中,我想进行一些调试构建,以便能够在我的构建系统中 运行 gdb:
HOST ?=127.0.0.1
PORT ?=7070
CCFLAGS="-std=c++11"
CPP=g++ ${CCFLAGS}
CFLAGS_DBG=" -g -DDEBUG"
TEST_BUILD_PATH="./build/tests"
BUILD_PATH=./build/release
TESTS_SRC_PATH="./tests"
DEBUGER=gdb
# NORMAL TARGETS
build_commandParser: ./src/tools/command_parser.cpp ./src/tools/command_parser.h
${CPP} -Wall -g -c ./src/tools/command_parser.cpp -o ${BUILD_PATH}/command_parser.o
build_network: ./src/socket/network.cpp ./src/socket/network.h
${CPP} -Wall -g -c ./src/socket/network.cpp -o ${BUILD_PATH}/network.o
build_simpleCommand: ./src/socket/simple_command.cpp ./src/socket/simple_command.h
${CPP} -Wall -g -c ./src/socket/simple_command.cpp -o ${BUILD_PATH}/simple_command.o
build: build_simpleCommand build_commandParser build_network ./src/main.cpp
${CPP} -pthread -Wall -o ${BUILD_PATH}/server ${BUILD_PATH}/command_parser.o ${BUILD_PATH}/network.o ${BUILD_PATH}/simple_command.o ./src/main.cpp
run: build
${BUILD_PATH}/server -h ${HOST} -p ${PORT}
debug: build
${DEBUGER} --args ${BUILD_PATH}/server -h ${HOST} -p ${PORT}
clean:
find ./build ! -name '.gitkeep' -type f -exec rm -f {} + && find ./tests ! -name *.h -type f -exec rm -f {} +
如您所见,我尝试将 CFLAGS_DBG
变量单元化以传递调试标志。但如您所见,它并没有这样做:
g++ -pthread -Wall -std=c++11 -c ./src/socket/simple_command.cpp -o ./build/release/simple_command.o
g++ -pthread -Wall -std=c++11 -c ./src/tools/command_parser.cpp -o ./build/release/command_parser.o
g++ -pthread -Wall -std=c++11 -c ./src/socket/network.cpp -o ./build/release/network.o
g++ -pthread -Wall -std=c++11 -o ./build/release/server ./build/release/command_parser.o ./build/release/network.o ./build/release/simple_command.o ./src/main.cpp
我也试过这个设置:
HOST ?=127.0.0.1
PORT ?=7070
CCFLAGS=-pthread -Wall -std=c++11
CCFLAGS_DBG=
CPP=g++
TESTGEN=cxxtestgen
TEST_BUILD_PATH="./build/tests"
BUILD_PATH=./build/release
TESTS_SRC_PATH="./tests"
DEBUGER=gdb
# NORMAL TARGETS
build_commandParser: ./src/tools/command_parser.cpp ./src/tools/command_parser.h
${CPP} ${CCFLAGS} ${CCFLAGS_DBG} -c ./src/tools/command_parser.cpp -o ${BUILD_PATH}/command_parser.o
build_network: ./src/socket/network.cpp ./src/socket/network.h
${CPP} ${CCFLAGS} ${CCFLAGS_DBG} -c ./src/socket/network.cpp -o ${BUILD_PATH}/network.o
build_simpleCommand: ./src/socket/simple_command.cpp ./src/socket/simple_command.h
${CPP} ${CCFLAGS} ${CCFLAGS_DBG} -c ./src/socket/simple_command.cpp -o ${BUILD_PATH}/simple_command.o
build: build_simpleCommand build_commandParser build_network ./src/main.cpp
${CPP} ${CCFLAGS} ${CCFLAGS_DBG} -o ${BUILD_PATH}/server ${BUILD_PATH}/command_parser.o ${BUILD_PATH}/network.o ${BUILD_PATH}/simple_command.o ./src/main.cpp
run: build
${BUILD_PATH}/server -h ${HOST} -p ${PORT}
.PHONY: debug-flags
debug-flags: CCFLAGS+=-g -DDEBUG
debug: debug-flags build
${DEBUGER} --args ${BUILD_PATH}/server -h ${HOST} -p ${PORT}
clean:
find ./build ! -name '.gitkeep' -type f -exec rm -f {} + && find ./tests ! -name *.h -type f -exec rm -f {} +
我仍然得到上面的输出。所以我希望在使用 make debug
构建时能够指定额外的标志并为链接器设置新标志。你有什么办法吗?
正如问题评论中所说,
这里的构建过程有点不卫生。
我建议你改变它。
话虽这么说,但也可以做您想做的事,而且非常简单。
因此,通过阐述,
素描:
$ cat Makefile
flags := normal
debug: flags := overridden
build:
: $@ flags: ${flags}
debug: build
: $@ done
这里我们有一个目标特定变量flags
。
flags
通常是 normal
。
然而,
在构建 debug
或其任何依赖项时 它变成 overridden
.
build
以一种意料之中的方式使用 $flags
:
$ make build
: build flags: normal
令人惊讶的是,当您通过 debug
构建 build
时使用的值是 debug
的目标特定值:
$ make debug
: build flags: overridden
: debug done
建议
请勿对您的原始 makefile 执行此操作。
如果你这样做,你的生活将变得悲惨。
考虑:
make build
- 编辑
make debug
你最终会得到某种科学怪人——一个用比特建造的怪物。
如果你运气不好,它甚至可能 link.
您成功的唯一保证是每次 执行一次干净的构建。
糟糕的 makefile 的典型标志。
在我的例子中,我想进行一些调试构建,以便能够在我的构建系统中 运行 gdb:
HOST ?=127.0.0.1
PORT ?=7070
CCFLAGS="-std=c++11"
CPP=g++ ${CCFLAGS}
CFLAGS_DBG=" -g -DDEBUG"
TEST_BUILD_PATH="./build/tests"
BUILD_PATH=./build/release
TESTS_SRC_PATH="./tests"
DEBUGER=gdb
# NORMAL TARGETS
build_commandParser: ./src/tools/command_parser.cpp ./src/tools/command_parser.h
${CPP} -Wall -g -c ./src/tools/command_parser.cpp -o ${BUILD_PATH}/command_parser.o
build_network: ./src/socket/network.cpp ./src/socket/network.h
${CPP} -Wall -g -c ./src/socket/network.cpp -o ${BUILD_PATH}/network.o
build_simpleCommand: ./src/socket/simple_command.cpp ./src/socket/simple_command.h
${CPP} -Wall -g -c ./src/socket/simple_command.cpp -o ${BUILD_PATH}/simple_command.o
build: build_simpleCommand build_commandParser build_network ./src/main.cpp
${CPP} -pthread -Wall -o ${BUILD_PATH}/server ${BUILD_PATH}/command_parser.o ${BUILD_PATH}/network.o ${BUILD_PATH}/simple_command.o ./src/main.cpp
run: build
${BUILD_PATH}/server -h ${HOST} -p ${PORT}
debug: build
${DEBUGER} --args ${BUILD_PATH}/server -h ${HOST} -p ${PORT}
clean:
find ./build ! -name '.gitkeep' -type f -exec rm -f {} + && find ./tests ! -name *.h -type f -exec rm -f {} +
如您所见,我尝试将 CFLAGS_DBG
变量单元化以传递调试标志。但如您所见,它并没有这样做:
g++ -pthread -Wall -std=c++11 -c ./src/socket/simple_command.cpp -o ./build/release/simple_command.o
g++ -pthread -Wall -std=c++11 -c ./src/tools/command_parser.cpp -o ./build/release/command_parser.o
g++ -pthread -Wall -std=c++11 -c ./src/socket/network.cpp -o ./build/release/network.o
g++ -pthread -Wall -std=c++11 -o ./build/release/server ./build/release/command_parser.o ./build/release/network.o ./build/release/simple_command.o ./src/main.cpp
我也试过这个设置:
HOST ?=127.0.0.1
PORT ?=7070
CCFLAGS=-pthread -Wall -std=c++11
CCFLAGS_DBG=
CPP=g++
TESTGEN=cxxtestgen
TEST_BUILD_PATH="./build/tests"
BUILD_PATH=./build/release
TESTS_SRC_PATH="./tests"
DEBUGER=gdb
# NORMAL TARGETS
build_commandParser: ./src/tools/command_parser.cpp ./src/tools/command_parser.h
${CPP} ${CCFLAGS} ${CCFLAGS_DBG} -c ./src/tools/command_parser.cpp -o ${BUILD_PATH}/command_parser.o
build_network: ./src/socket/network.cpp ./src/socket/network.h
${CPP} ${CCFLAGS} ${CCFLAGS_DBG} -c ./src/socket/network.cpp -o ${BUILD_PATH}/network.o
build_simpleCommand: ./src/socket/simple_command.cpp ./src/socket/simple_command.h
${CPP} ${CCFLAGS} ${CCFLAGS_DBG} -c ./src/socket/simple_command.cpp -o ${BUILD_PATH}/simple_command.o
build: build_simpleCommand build_commandParser build_network ./src/main.cpp
${CPP} ${CCFLAGS} ${CCFLAGS_DBG} -o ${BUILD_PATH}/server ${BUILD_PATH}/command_parser.o ${BUILD_PATH}/network.o ${BUILD_PATH}/simple_command.o ./src/main.cpp
run: build
${BUILD_PATH}/server -h ${HOST} -p ${PORT}
.PHONY: debug-flags
debug-flags: CCFLAGS+=-g -DDEBUG
debug: debug-flags build
${DEBUGER} --args ${BUILD_PATH}/server -h ${HOST} -p ${PORT}
clean:
find ./build ! -name '.gitkeep' -type f -exec rm -f {} + && find ./tests ! -name *.h -type f -exec rm -f {} +
我仍然得到上面的输出。所以我希望在使用 make debug
构建时能够指定额外的标志并为链接器设置新标志。你有什么办法吗?
正如问题评论中所说, 这里的构建过程有点不卫生。 我建议你改变它。
话虽这么说,但也可以做您想做的事,而且非常简单。 因此,通过阐述, 素描:
$ cat Makefile
flags := normal
debug: flags := overridden
build:
: $@ flags: ${flags}
debug: build
: $@ done
这里我们有一个目标特定变量flags
。
flags
通常是 normal
。
然而,
在构建 debug
或其任何依赖项时 它变成 overridden
.
build
以一种意料之中的方式使用 $flags
:
$ make build
: build flags: normal
令人惊讶的是,当您通过 debug
构建 build
时使用的值是 debug
的目标特定值:
$ make debug
: build flags: overridden
: debug done
建议
请勿对您的原始 makefile 执行此操作。 如果你这样做,你的生活将变得悲惨。 考虑:
make build
- 编辑
make debug
你最终会得到某种科学怪人——一个用比特建造的怪物。 如果你运气不好,它甚至可能 link.
您成功的唯一保证是每次 执行一次干净的构建。 糟糕的 makefile 的典型标志。