0

I've compiled a binary (my_test.o) and am trying to link it against a library provided by a manufacturer (libmfghost.so.2).

The manufacturer has provided example code and an example makefile, which works to compile examples and link them with the library. I started by copying the flags but at this point I am directly referencing their rules.mk in my Makefile, to ensure I have the exact same compile and link flags.

When I try to link, I get "undefined reference to mfg_device_init()". The linker can't find the reference, however:

  • If I typo the -lmfghost, it gives an error that it can't find the other library, so with it written correctly, it clearly is finding the library
  • If I typo the name of the function call, the compile fails, as the function isn't in the header. I'm certain I have the prototype matched
  • nm libmfghost.so.2 | grep device_init shows that the symbol is in the shared object.

Minimal example that reproduces this:

clang++ -o my_test -lmfghost -L../sdk/builds/debug/lib my_test.o

Why can't ld find the function in a library that it clearly sees and clearly has the function in it?

Brydon Gibson
  • 1,179
  • 3
  • 11
  • 22
  • Hint: tools like `ldd`, `objdump`, `nm`, `readelf` and similar can be *very* useful in seeing what symbols are exported from a library (and how). – Jesper Juhl Jul 21 '22 at 15:41
  • Aren't you supposed to put the library reference after the object file? I.e. `clang++ -o my_test -L../sdk/builds/debug/lib my_test.o -lmfghost` – john Jul 21 '22 at 15:51

0 Answers0