Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between clang and clang++ when building a library?

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
like image 947
Kaolin Fire Avatar asked Dec 14 '12 00:12

Kaolin Fire


People also ask

What is Clang library?

Clang: a C language family frontend for LLVM. The Clang project provides a language front-end and tooling infrastructure for languages in the C language family (C, C++, Objective C/C++, OpenCL, CUDA, and RenderScript) for the LLVM project.

What is the difference between Clang and clang ++?

The only difference between the two is that clang links against only the C standard library if it performs a link, whereas clang++ links against both the C++ and C standard libraries.

What Clang is used for?

The Clang tool is a front end compiler that is used to compile programming languages such as C++, C, Objective C++ and Objective C into machine code. Clang is also used as a compiler for frameworks like OpenMP, OpenCL, RenderScript, CUDA and HIP.

Why do people use Clang?

Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.


1 Answers

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?

like image 109
Marshall Clow Avatar answered Oct 03 '22 04:10

Marshall Clow