为什么.o文件中的函数名与.cc文件中的函数名在编译后不同? [英] Why does the function name in .o file different from the one in .cc file after compiling?

查看:471
本文介绍了为什么.o文件中的函数名与.cc文件中的函数名在编译后不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下命令编译了.cc文件,该文件位于Makefile代码中:

  bin / bash ../ libtool --silent --tag = CXX --mode = compile g ++ -DHAVE_CONFIG_H -I。 
-I .. -I ../ include / -I .. -g -O2 -MT rtpsession_inet.lo -MD -MP -MF
.deps / rtpsession_inet.Tpo -c -o rtpsession_inet。 lo rtpsession_inet.cc

.cc中有一个名为 rtp_session_rtp_recv 的函数文件。然而,据说,当我使用由Makefile生成的库时,找不到该函数的引用。



所以我检查了rtpsession_inet生成的.o文件。 cc,并发现函数名称更改为 _Z20rtp_session_rtp_recvP11_RtpSessionj 时,没有名为 rtp_session_rtp_recv 的函数。



,还有其他几个函数改变了他们的名字,eg rtp_session_rtp_send - > _Z20rtp_session_rtp_sendP11_RtpSessionP4msgb



但不会更改 rtp_session_set_remote_addr_full 所有。



其他字符是什么意思?如何处理这个问题?



我在Linux中编译文件并使用命令

  nm rtpsession_inet.o 

以读取.o文件。 (所有函数,包括具有不正确名称的函数都有T标签,这意味着引用存在)



谢谢!

解决方案

这称为名称调整。 p>

这是为了链接器的好处。 C ++编译器能够基于它们的参数类型来解析同名的多个函数。例如,您可能有一个名为 print 的函数,它接受一个 int 参数,另一个函数接受 char * 参数。编译器根据传递给它的参数类型来知道生成调用的对象。



但是,跨翻译单元的调用必须由链接器解析,通常不知道C ++重载规则,并且只能基于名称来解析调用。所以C ++编译器装饰了足够的信息来解析重载。



C ++标准没有指定如何做,但如果你看看名字


我如何处理这个问题?


编译器和链接器应正确解析调用。你指的是什么问题?


I compiled an .cc file with the following command, which is in Makefile code:

bin/bash ../libtool --silent --tag=CXX   --mode=compile g++ -DHAVE_CONFIG_H -I.
-I.. -I../include/ -I..    -g -O2 -MT rtpsession_inet.lo -MD -MP -MF 
.deps/rtpsession_inet.Tpo -c -o rtpsession_inet.lo rtpsession_inet.cc

There is a function named rtp_session_rtp_recv in the .cc file. However, it is said that the reference of this function cannot be found when I use the library generated by the Makefile.

So I checked the .o file generated by rtpsession_inet.cc and find that there is not a function named rtp_session_rtp_recv while the function name is changed to _Z20rtp_session_rtp_recvP11_RtpSessionj.

Meanwhile, there are several other functions changes their name, e.g. rtp_session_rtp_send -> _Z20rtp_session_rtp_sendP11_RtpSessionP4msgb.

But functions such as rtp_session_set_remote_addr_full are not changed at all.

What is the additional characters' meaning? How can I deal with this problem?

I compile the file in Linux and use command

nm rtpsession_inet.o

to read the .o file. (All the functions including the one with incorrect name are with T tag, which means the reference exists)

Thanks!

解决方案

This is called name mangling.

It's for the benefit of the linker. A C++ compiler is able to resolve multiple functions of the same name, based on their argument types. For example, you might have a function called print that takes an int argument, and another that takes a char* argument. The compiler knows which one to generate a call to based on what type of argument you pass to it.

Calls across translation units, though, have to be resolved by the linker, which typically is not aware of C++ overloading rules and has to resolve calls based only on the name. So the C++ compiler decorates the name with enough information to resolve the overload.

The C++ standard doesn't specify how this is done, but if you look at the name you can probably work out how the mangled name is generated.

How can I deal with this problem?

The compiler and linker should resolve calls correctly. What problem are you referring to?

这篇关于为什么.o文件中的函数名与.cc文件中的函数名在编译后不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆