Peeter Joot's (OLD) Blog.

Math, physics, perl, and programming obscurity.

Archive for October 11th, 2011

name mangling link errors with mixed C and C++

Posted by peeterjoot on October 11, 2011

I’ve been scratching my head trying to figure out one last link error after I made some code changes. From the linker I got:

ld: 0711-317 ERROR: Undefined symbol: .ossCacheSysFunctionPointers

yet my symbol appeared to be there:

$ rm -rf .t
$ mkdir .t
$ cd .t
$ ar -xX64 ../libSTdb2osse.a
$ nm -X64 *.o | grep ossCacheSysFunctionPointers
.ossCacheSysFunctionPointers U           -
.ossCacheSysFunctionPointers() T        1600
ossCacheSysFunctionPointers() D        2120          24
ossCacheSysFunctionPointers() d        1984           8

Do you spot the problem? I’ve got the symbol with C++ mangling, but referenced using a C prototype. This is much easier to see with name mangling disabled

$ nm -C -X64 *.o | grep ossCacheSysFunctionPointers
.ossCacheSysFunctionPointers U           -
.ossCacheSysFunctionPointers__Fv T        1600
ossCacheSysFunctionPointers__Fv D        2120          24
ossCacheSysFunctionPointers__Fv d        1984           8

ie: my symbol is there with C++ mangling. That little () in the nm output was hard to spot today! Perhaps I should dig out my glasses?

the lesson to learn here, which I’ve “learned” a few times, apparently not well enough, is to not put prototypes in .C files. Put them in an appropriate header where all the files that include the header will see the same linkage. Of course this doesn’t help if the file that defines the function doesn’t include the function and ends up with a different linkage.

Posted in C/C++ development and debugging. | Tagged: , , , | 4 Comments »