如何阻止Make向传递的参数添加Unicode前缀字节?
问题分析与解决:Make传递参数时出现额外Unicode字节
环境信息
> hostnamectl ... Operating System: Red Hat Enterprise Linux CPE OS Name: cpe:/o:redhat:enterprise_linux:7.9:GA:server Kernel: Linux 3.10.0-1160.88.1.el7.x86_64 Architecture: x86-64
问题现象
通过Make运行C程序时,使用Makefile变量$(x)传递的数字参数前被添加了三个额外字节(0xe2、0x80、0xad),程序会输出“skipping unicode header”提示;但直接在命令行运行程序,或在Makefile中直接写数字参数、使用变量$(y)传递字符串参数时,均无此问题。
相关代码
C程序(test.c)
#include <stdio.h> #include <stdint.h> int main(int argc, char **argv) { if (argc > 1) { while (((argv[1][0] & 0x80) == 0x80) && (argv[1][0] != 0)) { printf("skipping unicode header %#x\n", (uint8_t) argv[1][0]); argv[1]++; } } return 0; }
Makefile
x = 1048570 y = abcdefg all: gcc test.c -o test ./test 1048570 ./test abcdefg ./test $(x) ./test $(y)
运行结果
> make gcc test.c -o test ./test 1048570 ./test abcdefg ./test 1048570 skipping unicode header 0xe2 skipping unicode header 0x80 skipping unicode header 0xad ./test abcdefg
原因分析
这三个额外字节是**零宽非断空格(Zero-Width Non-Breaking Space, U+202D)**的UTF-8编码(E2 80 AD),属于不可见控制字符。问题出在Makefile的x变量定义行中,=和数字1048570之间不小心混入了这个隐藏字符,导致变量x的实际值为[零宽字符]1048570,传递给程序时便带上了这些额外字节。
解决方案
- 使用支持显示不可见字符的文本编辑器打开Makefile(比如Vim执行
:set list,VS Code开启“控制字符”显示),找到并删除x = 1048570行中=和数字之间的隐藏零宽字符。 - 或直接重新输入变量定义行,确保
=和数字之间只有正常空格:x = 1048570 - 修改后重新运行
make,./test $(x)的执行将不再出现额外字节的提示。
内容的提问来源于stack exchange,提问作者tom
相关产品推荐
相关产品推荐

