6

I was building this code from this article

I was using the recommended command to build it: gcc -Wall -o keyboard_leds keyboard_leds.c -framework IOKit -framework CoreFoundation

$ curl -O http://osxbook.com/book/bonus/chapter10/kbdleds/download/keyboard_leds.c
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11542  100 11542    0     0   139k      0 --:--:-- --:--:-- --:--:--  140k

$ clang -Wall -o keyboard_leds keyboard_leds.c -framework IOKit -framework CoreFoundation
Undefined symbols for architecture x86_64:
  "_print_errmsg_if_err", referenced from:
      _create_hid_interface in keyboard_leds-d14ed5.o
  "_print_errmsg_if_io_err", referenced from:
      _create_hid_interface in keyboard_leds-d14ed5.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

$

The code is very straightforward, if a little dated (6 years). I cannot see any mention anywhere of the symbols that begin with the underscore. I'm probably missing something simple, but I'm completely stumped about where these symbols come from.

What gives? I can comment out the calls to get it to compile.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
  • 1
    It is a clang C99-compliance thing. You can [**read more about it here**](http://clang.llvm.org/compatibility.html#inline), but in your case the simplest work-around is to remove the `inline` preamble from the *definitions* (where the *code* is, not the prototypes). You could add `-std=gnu89`, but I don't recommend it. – WhozCraig Jul 22 '14 at 00:25
  • Oh that is bizarre. I almost tried that... I tried removing the *declarations* (as I saw them to serve no purpose) but to no avail. Thanks! Make that your answer and i will accept – Steven Lu Jul 22 '14 at 00:29
  • Based on the explanation that was linked, it would appear that compiling with optimizations will result in a successful compilation. I verified this with `-O2`. – Steven Lu Jul 22 '14 at 00:35
  • It will, but obviously its a bear to debug (I can confirm, btw that compiling with `-O2` does indeed build successfully, and it is likely I'm running a clang similar to yours). I'm resisting posting an answer until I can confirm with reasonable assurance this isn't a duplicate of something else. It would seem this should have bit someone in the past. If I can't find anything by this evening I'll post something akin to my first comment. Hope thats ok. – WhozCraig Jul 22 '14 at 00:37
  • Ha! Found one: [This looks to be the same](http://stackoverflow.com/questions/10243018/inline-function-undefined-symbols-error). – WhozCraig Jul 22 '14 at 00:40
  • Dang. Such SO-fu. Nice. – Steven Lu Jul 22 '14 at 00:45
  • 1
    Possible duplicate of [Inline function "undefined symbols" error](https://stackoverflow.com/questions/10243018/inline-function-undefined-symbols-error) – Jean-François Fabre Dec 18 '17 at 20:16
  • @Jean-FrançoisFabre nice. You linked the same one WhozCraig did in 2014 :) – Steven Lu Dec 18 '17 at 20:22
  • Because I stumbled on his link and it really seems to be a duplicate to me. – Jean-François Fabre Dec 18 '17 at 20:23

0 Answers0