Question

I was compiling a c library (to be used by a c++ project) with clang. I got linker errors (specifically, undefined symbol regarding the hqxInit function) when trying to link this library. When I switch to clang++, it works. Checking with nm, clang++ munges the names further. What is going on—and is there a better way to tell the linker that a library is munged-for-c versus munged-for-c++? It seems silly to have to build a c library with c++....

// built with clang

$ nm libhqx.a

libhqx.bak(init.c.o)
04000000 C _RGBtoYUV
00000004 C _YUV1
00000004 C _YUV2
00000000 T _hqxInit

// built with clang++

$ nm libhqx.a 

libhqx.a(init.o):
00000100 S _RGBtoYUV
04000100 S _YUV1
04000104 S _YUV2
00000000 T __Z7hqxInitv
Was it helpful?

Solution

clang and clang++ on most systems are the same executable. One is merely a symbolic link to the other.

The program checks to see what name it is invoked under, and:

  • for clang, compiles code as C
  • for clang++, compiles code as C++

In C++, the compiler generates names for functions differently than C - this is because you can have multiple functions with the same name (but different) parameters. This is called "name mangling" - and that's what you are seeing.

You can use a tool called c++filt to "demangle" the names.

Example:

$ c++filt __Z7hqxInitv
hqxInit()

More information here: why clang++ behaves differently from clang since the former is a symbol link of the latter?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top