我正在使用STM32f4系列芯片上的嵌入式应用程序的makefile编译代码.
当我做:
Makefile文件:
DEFINES += -DGIT_HASH="test hash"
Run Code Online (Sandbox Code Playgroud)
在我的main.c:
const char * git_hash = GIT_HASH;
Run Code Online (Sandbox Code Playgroud)
当我打印出来时git_hash,我明白了test hash.
我想做的是:
Makefile文件:
COMMIT_HASH = $(shell git rev-parse HEAD)
DEFINES += -DGIT_HASH=$(COMMIT_HASH)
Run Code Online (Sandbox Code Playgroud)
在我的main.c:
const char * git_hash = GIT_HASH;
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
<command-line>:0:10: error: 'c1920a032c487a55b1b109d8774faf05e2ba42d0' undeclared here (not in a function)
src/run/main.c:173:25: note: in expansion of macro 'GIT_HASH'
const char * git_hash = GIT_HASH;
Run Code Online (Sandbox Code Playgroud)
我想知道为什么COMMIT_HASH不像字符串一样对待它.任何见解将不胜感激.
请记住,这#define会导致预编译器为字符替换执行直接字符.所以在你的第一个例子中,
const char * git_hash = GIT_HASH;
Run Code Online (Sandbox Code Playgroud)
变
const char * git_hash = "test hash";
Run Code Online (Sandbox Code Playgroud)
编译器很好用,因为它看到了一个文字字符串.
但是,在你的第二个例子中,它变成了
const char * git_hash = c1920a032c487a55b1b109d8774faf05e2ba42d0;
Run Code Online (Sandbox Code Playgroud)
现在当编译器完成它的工作时,它会看到一个变量名,而不是字符串文字,正如我想你想的那样.要解决此问题,您需要确保哈希用引号括起来.一种可能的解决方案是将Makefile更改为DEFINES += -DGIT_HASH=\"$(COMMIT_HASH)\".