2

I am trying to use LZF C++. However, when I compile it, it give me this error:

g++     -o dist/Debug/GNU-MacOSX/cppapplication_2 build/Debug/GNU-MacOSX/lzf_c.o build/Debug/GNU-MacOSX/lzf_d.o build/Debug/GNU-MacOSX/main.o 
Undefined symbols for architecture x86_64:
  "lzf_compress(void const*, unsigned int, void*, unsigned int)", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1

I already included lzf.h.

Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
Adriansyah
  • 124
  • 11

1 Answers1

4

A quick look at lzf.h shows that it doesn't account for C++. Normally you would see something like

#ifdef __cplusplus
extern "C" {
#endif

/* Function declarations go here */

#ifdef __cplusplus
}
#endif

So that the C++ compiler knows to look for C symbols. I'm guessing what is happening with your code is that the C++ compiler is requesting whatever it mangles the C++ symbol lzf_compress to instead of just the C symbol lzf_compress. However, since the actual code is in a C file your build system probably uses the C compiler (not the C++ compiler) to compile it.

If I were you I would fix the header, and file a bug (with a patch) to get the fix upstream.

If you don't want to fix lzf.h I think you could just do something like

extern "C" {
#include "lzf.h"
}

The other "solution" would be to just compile everything with the C++ compiler. I'm not certain liblzf is valid C++, though, so that may or may not work.

nemequ
  • 16,623
  • 1
  • 43
  • 62
  • Debian already do that liblzf-3.6/debian/patches/0001-Make-sure-that-the-library-is-linked-with-C-symbols.patch – Sérgio Apr 22 '21 at 01:29
  • That's good, but it's probably not a good idea to rely on that unless you *know* your code will never be used anywhere other than a Debian derivative. Basically, unless it's throw-away code you should protect against the `extern "C"` being omitted. That said, you probably shouldn't use LZF for anything unless you have a very good reason; AFAICT it's unmaintained, and there are much better options these days. – nemequ Apr 22 '21 at 19:01