A. Vuori
2009-Jun-17 08:16 UTC
[LLVMdev] 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; } -------------- $ 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. I'm currently running precompiled llvm-gcc binaries (being unable to compile fresh llvm-gcc at the moment): llvm-gcc (GCC) 4.2.1 (Based on Apple Inc. build 5623) (LLVM build 2.4). I apologize if the issue is due to my old llvm-gcc version. -- Arto Vuori
Fabian Scheler
2009-Jun-17 08:39 UTC
[LLVMdev] Why are functions renamed for .cpp files with llvm-gcc?
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
Pertti Kellomäki
2009-Jun-17 09:01 UTC
[LLVMdev] Why are functions renamed for .cpp files with llvm-gcc?
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. -- Pertti
Sylvere Teissier
2009-Jun-17 09:01 UTC
[LLVMdev] Why are functions renamed for .cpp files with llvm-gcc?
Le mercredi 17 juin 2009 à 11:16 +0300, A. Vuori a écrit :> Hello, > > I wonder why there is a difference in how > llvm-gcc compiles .c and .cpp files.C++ support function overloading, C not. For C++ you have to encode the types with the function name to not have name collision
Owen Anderson
2009-Jun-17 09:04 UTC
[LLVMdev] Why are functions renamed for .cpp files with llvm-gcc?
On Jun 17, 2009, at 1:39 AM, Fabian Scheler wrote:> 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 manglingTo 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 -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2620 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20090617/0f74cceb/attachment.bin>
Frits van Bommel
2009-Jun-17 09:12 UTC
[LLVMdev] Why are functions renamed for .cpp files with llvm-gcc?
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
Matthieu Moy
2009-Jun-17 09:21 UTC
[LLVMdev] Why are functions renamed for .cpp files with llvm-gcc?
Fabian Scheler <fabian.scheler at gmail.com> writes:> The answer is: C++ name manglingand the slightly longer answer is : try int foo(int x) { ... } int foo(void) { ... } int foo(whatever-else y) { ... } (which doesn't compile in C) -- Matthieu
I think there is a latency problem with the mail server. That create answer echo :D> _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Albert Graef
2009-Jun-17 20:32 UTC
[LLVMdev] Why are functions renamed for .cpp files with llvm-gcc?
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. -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr.Graef at t-online.de, ag at muwiinfa.geschichte.uni-mainz.de WWW: http://www.musikinformatik.uni-mainz.de/ag
Maybe Matching Threads
- [LLVMdev] Why are functions renamed for .cpp files with llvm-gcc?
- [LLVMdev] Why are functions renamed for .cpp files with llvm-gcc?
- [LLVMdev] llvm-as parse error
- [LLVMdev] llvm-as parse error
- [LLVMdev] Why are functions renamed for .cpp files with llvm-gcc?