编译C语言代码时出现‘)’在LEVELNAME前的奇怪错误求助
编译错误:
expected ‘)’ before ‘LEVELNAME’ 解决方法 错误原因
错误出在BANNER宏的定义行:
#define BANNER \ "Welcome to " LEVELNAME ", brought to you by https://exploit.education"
LEVELNAME是未定义的宏,预处理器会将其替换为空内容,导致字符串拼接逻辑失效,最终在printf("%s\n", BANNER);语句中出现语法错误——编译器无法识别残缺的字符串结构,因此抛出expected ‘)’ before ‘LEVELNAME’的提示。
两种解决方式
方式1:编译时通过参数定义LEVELNAME
在编译命令中添加-D参数直接定义宏,命令如下:
gcc stack-zero.c -o stack0 -DLEVELNAME=\"stack-zero\"
或者用单引号避免转义:
gcc stack-zero.c -o stack0 -DLEVELNAME='stack-zero'
方式2:修改代码显式定义LEVELNAME
在代码的#include块之后添加LEVELNAME的宏定义,确保字符串拼接合法:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define LEVELNAME "stack-zero" // 添加这一行 #define BANNER \ "Welcome to " LEVELNAME ", brought to you by https://exploit.education" char *gets(char *); int main(int argc, char **argv) { struct { char buffer[64]; volatile int changeme; } locals; printf("%s\n", BANNER); locals.changeme = 0; gets(locals.buffer); if (locals.changeme != 0) { puts("Well done, the 'changeme' variable has been changed!"); } else { puts( "Uh oh, 'changeme' has not yet been changed. Would you like to try " "again?"); } exit(0); }
修改后重新编译即可正常生成可执行文件。
内容的提问来源于stack exchange,提问作者Samyak Jain
相关产品推荐
相关产品推荐

