OK, this is probably very basic, but I can’t find the problem. I’m trying to link my C++ program with a C shared library (YAJL to name it) with -lyajl ; the linker seems to find it (it does not complain for that) but cannot find the symbols I use in my code :
$> g++ test.c -lyajl
Undefined symbols for architecture x86_64:
"yajl_tree_parse(char const*, char*, unsigned long)", referenced from:
_main in ccEqsAaJ.o
"yajl_tree_get(yajl_val_s*, char const**, yajl_type)", referenced from:
_main in ccEqsAaJ.o
"yajl_tree_free(yajl_val_s*)", referenced from:
_main in ccEqsAaJ.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
$>
I made a minimalist test.c file to isolate the behavior. Well, what’s very strange is that gcc seems perfectly OK with the code (as, incidentally, test.c is just C code, no C++ feature here):
$> gcc test.c -lyajl
$>
What is gcc doing that g++ doesn’t seem to be able to do right ?
Could it be that the symbols in libyajl.so are C linkage, but your code (or the header file you are using) does not declare them
extern "C"? In that case, compiling your code as C++ would result in name-mangled symbols in your object file, which would not match the (C linkage, unmangled) symbols in the library.The solution would be to write, in the header file, something like this: