我发现了如何使用 Makefile 添加带有日期和内部版本号的链接变量的绝妙示例:
OBJECTS=main.o
BUILD_NUMBER_LDFLAGS = -Xlinker --defsym -Xlinker __BUILD_DATE=$$(date +'%Y%m%d')
BUILD_NUMBER_LDFLAGS += -Xlinker --defsym -Xlinker __BUILD_NUMBER=$$(cat buildnumber.num)
a.out: $(OBJECTS) buildnumber.num
$(CC) $(LDFLAGS) $(BUILD_NUMBER_LDFLAGS) -o $@ $(OBJECTS)
buildnumber.num: $(OBJECTS)
@if ! test -f buildnumber.num; then echo 0 > buildnumber.num; fi
@echo $$(($$(cat buildnumber.num)+1)) > buildnumber.num
但是我在 CMakeLists.txt 中重复这一点的所有尝试都失败了...... :-( 例如:
ADD_CUSTOM_COMMAND(OUTPUT buildnumber.num
COMMAND @if ! test -f buildnumber.num; then echo 0 > buildnumber.num; fi
COMMAND @echo $$(($$(cat buildnumber.num) + 1)) > buildnumber.num)
file (GLOB_RECURSE MAIN_SOURCES FOLLOW_SYMLINKS ./src/*.cpp)
add_custom_target(buildnumber.num DEPENDS ${MAIN_SOURCES})
我将非常感谢如何在 cmake 中实现这一点。