0

I'm sure I don't see something obvious, but here it is :

I've written a small tools.h file containing 2 macros I use in several files of my project :

#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

I added a #include "tools.h" in every source file using clamp and is_in_range, but they seem ignored at compilation.

For example,

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);

I have several others, one for each source file referring to tools.h.

And, of course, the compilation aborts:

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

What am I missing?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
T. Tournesol
  • 310
  • 2
  • 14
  • 4
    Note that you should not create function or variable names that start with an underscore, in general. [C11 §7.1.3 Reserved identifiers](https://port70.net/~nsz/c/c11/n1570.html#7.1.3) says (in part): — _All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use._ — _All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces._ See also [What does double underscore (`__const`) mean in C?](https://stackoverflow.com/a/1449301/15168) – Jonathan Leffler Aug 01 '18 at 20:34
  • 4
    Why are you defining macros? Define functions and let the optimizing compiler take care of the rest. Unless you have concrete performance data proving this is necessary, it probably isn't. – tadman Aug 01 '18 at 20:35
  • 5
    I suggest using the `-H` option to GCC to report on which header files are included. You may find that there is a different `tools.h` file that is being used. Although I mentioned that `_TOOLS_H_` is not a good choice for a header guard macro (because it is reserved for the system to use), it probably isn't the immediate source of the trouble. – Jonathan Leffler Aug 01 '18 at 20:38
  • 7
    Put a `#error tools` on the first line of your header and make sure that you're actually including *that* tools.h and not some other tools.h. – user3386109 Aug 01 '18 at 20:39
  • 1
    try to replace your include protection by `#pragma once` in case some other header uses the same `_TOOLS_H_` identifier. – Jean-François Fabre Aug 01 '18 at 20:39
  • 2
    @user3386109 There's also the tried and true method of just mashing in sdhfusdfbgdrubguerwbtwebterte and expecting a compiler error! – tadman Aug 01 '18 at 20:49
  • 2
    @tadman While an effective technique, I can never manage to get the spelling down without looking it up. – Christian Gibbons Aug 01 '18 at 20:52
  • 1
    @ChristianGibbons: True, but I find `ZaphodBeeblebroxLovesPanGalacticGargleBlasters` quite effective too, and it's easier to remember the spelling of that! – Jonathan Leffler Aug 01 '18 at 20:54
  • 2
    Your is_in_range macro is wrong, you need paranethesis around the whole macro as well. – Fredrik Aug 01 '18 at 21:02
  • 1
    user3386109 is right : there's indeed a tools.h in the library I'm using ! – T. Tournesol Aug 01 '18 at 21:13
  • 1
    And thanks Fredrik for pointing it, and others for valuable tips (-H, #pragma once and provoking an error). – T. Tournesol Aug 01 '18 at 21:15
  • 1
    "What am I missing?" --> Post lacks the code for `src/camera.c` to diagnose the issue. Review [mcve] – chux - Reinstate Monica Aug 01 '18 at 21:25

1 Answers1

2

The answer was given by user3386109 : there was a file named tools.h in the library I was using.

T. Tournesol
  • 310
  • 2
  • 14