写宏遇到的问题:warning: backslash and newline separated by space - C

本文介绍了一个关于C语言中调试宏使用的常见陷阱,即在宏定义的行尾多余的空格导致的问题,并提供了一个具体的示例代码。


    原来是因为\ 后面多了一个空格

   附上一个刚刚写得调试用的例子:

#define desay_debug(_string,myValue) \
    do{\
		 c_snprintf(desay_info, \
		          250 - 1, \
		          "\ndesay: File: %s  Line#: %d  Func: %s()\n !!!:message is %s value is %d\n", \
			      __FILE__, \
			      __LINE__, \
			      __func__, \
			      _string, \
			      myValue); \
		desayEvent.ui4_msg_id=DESAY_MESSAGE_ID;\
        desayEvent.ui4_data1=(UINT32)&desay_info[0];\
        a_am_get_app_handle_from_name(&h_app_desay,"homepage");\
        c_app_send_msg(h_app_desay,0,&desayEvent,sizeof(DESAY_MSG_T),NULL,NULL);\
    }while(0)


 

 

linux在编译的时候出现以下问题: CC minidlna.o In file included from minidlna.h:48:0, from minidlna.c:50: sql.h:42:14: error: unknown type name 'sqlite3' int sql_exec(sqlite3 *db, const char *fmt, ...); ^~~~~~~ sql.h:43:19: error: unknown type name 'sqlite3' int sql_get_table(sqlite3 *db, const char *zSql, char ***pazResult, int *pnRow, int *pnColumn); ^~~~~~~ sql.h:44:23: error: unknown type name 'sqlite3' int sql_get_int_field(sqlite3 *db, const char *fmt, ...); ^~~~~~~ sql.h:45:29: error: unknown type name 'sqlite3' int64_t sql_get_int64_field(sqlite3 *db, const char *fmt, ...); ^~~~~~~ sql.h:46:27: error: unknown type name 'sqlite3' char * sql_get_text_field(sqlite3 *db, const char *fmt, ...); ^~~~~~~ sql.h:47:16: error: unknown type name 'sqlite3' int db_upgrade(sqlite3 *db); ^~~~~~~ In file included from minidlna.c:50:0: minidlna.h:72:3: warning: #warning "Your SQLite3 library appears to be too old! Please use 3.5.1 or newer." [-Wcpp] # warning "Your SQLite3 library appears to be too old! Please use 3.5.1 or newer." ^~~~~~~ minidlna.h:104:13: error: unknown type name 'sqlite3' int open_db(sqlite3 **sq3); ^~~~~~~ minidlna.h:117:15: error: unknown type name 'sqlite3' void check_db(sqlite3 *db, int new_db); ^~~~~~~ In file included from minidlna.c:52:0: upnpglobalvars.h:191:40: warning: backslash and newline separated by space "http-get:*:audio/x-monkeys-audio:*," \ upnpglobalvars.h:247:8: error: unknown type name 'sqlite3' extern sqlite3 *db; ^~~~~~~ minidlna.c: In function 'init': minidlna.c:1014:2: warning: statement with no effect [-Wunused-value] for (failnums; failnums < 10; failnums++) ^~~ minidlna.c: In function 'main': minidlna.c:1377:6: error: implicit declaration of function 'sqlite3_libversion_number' [-Werror=implicit-function-declaration] if (sqlite3_libversion_number() < 3005001) ^~~~~~~~~~~~~~~~~~~~~~~~~ minidlna.c:1567:20: error: implicit declaration of function 'sqlite3_total_changes'; did you mean 'sqlite3_threadsafe'? [-Werror=implicit-function-declaration] if (scanning || sqlite3_total_changes(db) != last_changecnt) ^~~~~~~~~~~~~~~~~~~~~ sqlite3_threadsafe minidlna.c: In function 'minidlnaStart': minidlna.c:1674:2: error: implicit declaration of function 'sqlite3_initialize'; did you mean 'sqlite3_malloc'? [-Werror=implicit-function-declaration] sqlite3_initialize(); ^~~~~~~~~~~~~~~~~~ sqlite3_malloc minidlna.c: In function 'minidlnaShutdown': minidlna.c:1818:8: error: implicit declaration of function 'sqlite3_close'; did you mean 'sqlite3_malloc'? [-Werror=implicit-function-declaration] ret = sqlite3_close(db); ^~~~~~~~~~~~~ sqlite3_malloc minidlna.c:1819:6: error: 'SQLITE_OK' undeclared (first use in this function) if (SQLITE_OK != ret) ^~~~~~~~~ minidlna.c:1819:6: note: each undeclared identifier is reported only once for each function it appears in minidlna.c:1826:8: error: implicit declaration of function 'sqlite3_shutdown'; did you mean 'sqlite3_malloc'? [-Werror=implicit-function-declaration] ret = sqlite3_shutdown(); ^~~~~~~~~~~~~~~~ sqlite3_malloc minidlna.c: At top level: minidlna.c:1851:13: error: unknown type name 'sqlite3' int open_db(sqlite3 **sq3) ^~~~~~~ minidlna.c:1917:15: error: unknown type name 'sqlite3' void check_db(sqlite3 *db, int new_db) ^~~~~~~ minidlna.c: In function 'minidlna_startScannerThread': minidlna.c:2067:8: error: implicit declaration of function 'open_db'; did you mean 'opendir'? [-Werror=implicit-function-declaration] ret = open_db(NULL); ^~~~~~~ opendir minidlna.c:2095:14: error: implicit declaration of function 'sql_get_int_field' [-Werror=implicit-function-declaration] updateID = sql_get_int_field(db, "SELECT VALUE from SETTINGS where KEY = 'UPDATE_ID'"); ^~~~~~~~~~~~~~~~~ minidlna.c:2103:2: error: implicit declaration of function 'check_db' [-Werror=implicit-function-declaration] check_db(db, ret); ^~~~~~~~ At top level: minidlna.c:193:12: warning: 'l_rescanFlag' defined but not used [-Wunused-variable] static int l_rescanFlag = 0;
08-19
你遇到的编译警告: ``` warning #1557-D: white space between backslash and newline in line splice ignored ``` 是 TI C6000 编译器(或 GCC 类似风格编译器)中的一种**预处理警告**,表示你在使用定义或字符串拼接时,**在反斜杠 `\` 和换行符之间有空格**,这会导致预处理器忽略这些空格,可能引起意料之外的代码拼接。 --- ## 🔍 警告含义详解 - `white space between backslash and newline`:在定义中,使用 `\` 表示续行符,**但你在 `\` 后面加了空格或 Tab**。 - `line splice ignored`:预处理器会忽略这些空格,直接将下一行拼接上来,但这是不符合标准 C 的行为。 --- ## ✅ 示例:错误的定义 ```c #define MY_MACRO(a, b) \ do { \ printf("%d %d\n", a, b); \ } while (0) \ ``` 如果成这样: ```c #define MY_MACRO(a, b) \ do { \ printf("%d %d\n", a, b); \ } while (0) \ ``` 注意第一行的 `\` 后面有空格或 Tab,就会触发这个警告。 --- ## ✅ 正确法:确保 `\` 后面没有空格 ```c #define MY_MACRO(a, b) \ do { \ printf("%d %d\n", a, b); \ } while (0) ``` > ✅ 注意:最后一行的 `\` 也不是必须的,可以省略。 --- ## ✅ 其他常见场景:字符串拼接中的错误 ```c const char *str = "Hello " \ "World"; ``` ✅ 正确,`\` 后没有空格。 ❌ 错误法: ```c const char *str = "Hello " \ "World"; ``` `\` 后有空格 → 触发警告。 --- ## ✅ 如何修复? 1. **检查所有定义、字符串拼接中使用 `\` 的地方** 2. **确保 `\` 是该行的最后一个字符** 3. **不要在 `\` 后添加任何空格或 Tab** --- ## ✅ 工具辅助检查 如果你使用 **CCS(Code Composer Studio)** 或 **VSCode + C/C++ 插件**,可以开启: - **显示空白字符(Show Whitespace)** - **语法高亮检查** - **启用 `-Wall` 编译选项**,让编译器报告更多潜在问题 --- ## ✅ 总结 | 问题 | 原因 | 解决方法 | |------|------|----------| | `#1557-D` 警告 | `\` 后有空格 | 删除空格,保证 `\` 是行尾最后一个字符 | | 定义或字符串拼接出错 | 拼接行为异常 | 检查所有 `\` 使用 | | 可读性差 | 代码风格混乱 | 使用统一的定义风格 | --- ##
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值