Why are functions renamed for .cpp files with llvm-gcc?

Hello,

I wonder why there is a difference in how
llvm-gcc compiles .c and .cpp files.

Example:

---bar.cpp----
int bar() {
    return 42;
}

Hi Arto,

I wonder why there is a difference in how
llvm-gcc compiles .c and .cpp files.

Example:

---bar.cpp----
int bar() {
   return 42;
}
--------------

$ llvm-gcc -emit-llvm -c bar.cpp

Now running bar.o through llvm-dis gives:
--------------------------------
define i32 @_Z3barv() nounwind {
< clip >
}
--------------------------------

Above, function 'bar' has been renamed to '_Z3barv'.

If, however, 'bar.cpp' is renamed 'bar.c',
we get 'define i32 @bar()' as expected.

The answer is: C++ name mangling

Ciao, Fabian

A. Vuori kirjoitti:

I wonder why there is a difference in how llvm-gcc compiles .c and .cpp files.

[...]

Above, function 'bar' has been renamed to '_Z3barv'.

If, however, 'bar.cpp' is renamed 'bar.c',
we get 'define i32 @bar()' as expected.

Look at what c++filt says about _Z3barv:

$ c++filt _Z3barv
bar()

The reason is that .cpp is taken to be a C++ file, and
names are mangled accordingly.

C++ support function overloading, C not.
For C++ you have to encode the types with the function name to not have
name collision

To elaborate a little bit, it's how C++ encodes namespaces, function overloading, etc. It's a standard thing, and necessary for cross-compiler compatibility. If you want your C++ functions to appear like C functions, you need to use extern "C" { }

--Owen

A. Vuori wrote:

Above, function 'bar' has been renamed to '_Z3barv'.

If, however, 'bar.cpp' is renamed 'bar.c',
we get 'define i32 @bar()' as expected.

This is not llvm-gcc specific; it happens for regular gcc too. It's needed to support overloading multiple functions called 'bar' with different parameters, (which C++ supports but C does not). _Z3barv just means "bar()", i.e. a function named 'bar' with no parameters (there's a program called c++filt to demangle these names).

This is called name mangling. See also http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_C.2B.2B

Fabian Scheler <fabian.scheler@gmail.com> writes:

The answer is: C++ name mangling

and the slightly longer answer is :

try

int foo(int x) { ... }
int foo(void) { ... }
int foo(whatever-else y) { ... }

(which doesn't compile in C)

I think there is a latency problem with the mail server.
That create answer echo :smiley:

Thanks to all for elaborating. So this was just a hole in my C++
knowledge.

Sylvere Teissier wrote:

I think there is a latency problem with the mail server.
That create answer echo :smiley:

Yeah, I've noticed that before...

A. Vuori wrote:

---bar.cpp----
int bar() {
    return 42;
}
--------------

Try extern "C" int bar() instead, then you get C binding and the name
mangling goes away.