【发布时间】:2018-08-01 20:33:41
【问题描述】:
我确定我没有看到明显的东西,但这里是:
我编写了一个小工具.h 文件,其中包含我在项目的几个文件中使用的 2 个宏:
#ifndef _TOOLS_H_
#define _TOOLS_H_
#define is_in_range(x, a, b) ((x) >= (a)) && ((x) < (b))
#define clamp(x, a, b)\
(((x) < (a)) ? (a) : (((x) > (b)) ? (b) : (x)))
#endif
我使用clamp 和is_in_range 在每个源文件中添加了#include "tools.h",但它们在编译时似乎被忽略了。
例如,
C:/SGDK134/bin/gcc -m68000 -Wall -fno-builtin -Iinc -Isrc -Ires -IC:/SGDK134/inc -IC:/SGDK134/res -BC:/SGDK134/bin -O3 -fuse-linker-plugin -fno-web -fno-gcse -fno-unit-at-a-time -fomit-frame-pointer -flto -c src/camera.c -o out/src/camera.o
src/camera.c: In function 'camera_set_focus':
src/camera.c:130:6: warning: implicit declaration of function 'clamp' [-Wimplicit-function-declaration]
x = clamp(fix32ToInt(obj->x) - 128, 0, current_stage.pwidth - 320);
我还有其他几个,每个源文件一个,引用tools.h。
当然,编译也会中止:
C:/SGDK134/bin/gcc -BC:/SGDK134/bin -n -T C:/SGDK134/md.ld -nostdlib out/sega.o @out/cmd_ C:/SGDK134/lib/libmd.a C:/SGDK134/lib/libgcc.a -o out/rom.out
C:\Users\xxxx\AppData\Local\Temp\cczl66At.ltrans0.ltrans.o: In function `main':
.text.startup+0x3ec): undefined reference to `clamp'
.text.startup+0x972): undefined reference to `clamp'
.text.startup+0x9c8): undefined reference to `clamp'
.text.startup+0xb5c): undefined reference to `is_in_range'
.text.startup+0xce0): undefined reference to `clamp'
make.exe": *** [out/rom.out] Error 1
我错过了什么?
【问题讨论】:
-
请注意,通常不应创建以下划线开头的函数或变量名称。 C11 §7.1.3 Reserved identifiers (部分)说: — 所有以下划线开头的标识符以及大写字母或另一个下划线始终保留供任何使用。 — 所有以下划线开头的标识符在普通名称空间和标记名称空间中始终保留用作具有文件范围的标识符。 另请参阅What does double underscore (
__const) mean in C? -
为什么要定义宏?定义函数并让优化编译器处理其余的事情。除非您有具体的性能数据证明这是必要的,否则可能不是。
-
我建议使用 GCC 的
-H选项来报告包含哪些头文件。您可能会发现正在使用不同的tools.h文件。虽然我提到_TOOLS_H_不是标题保护宏的好选择(因为它是保留给系统使用的),但它可能不是问题的直接根源。 -
在标题的第一行添加
#error tools并确保您实际上包含 that tools.h 而不是其他一些 tools.h。跨度> -
@user3386109 还有一种行之有效的方法,只是在 sdhfusdfbgdrubguerwbtwebterte 中混搭并期待编译器错误!
标签: c gcc c-preprocessor