-1

我使用 Makefile 代码中的以下命令编译了一个 .cc 文件:

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.cc 生成的 .o 文件,发现并没有一个名为rtp_session_rtp_recv的函数,而函数名改为_Z20rtp_session_rtp_recvP11_RtpSessionj

同时,还有其他几个函数更改了它们的名称,例如rtp_session_rtp_send -> _Z20rtp_session_rtp_sendP11_RtpSessionP4msgb

但是rtp_session_set_remote_addr_full等函数根本没有改变。

附加字符的含义是什么?我该如何处理这个问题?

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

nm rtpsession_inet.o

读取 .o 文件。(包括名称错误的所有函数都带有T标记,表示引用存在)

谢谢!

4

2 回答 2

1

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?

于 2013-08-14T23:02:29.990 回答
0

附加字符是编译器添加的符号“装饰”,用于标识函数/方法签名,即返回类型和参数。它帮助运行时确定在任何给定调用中调用多个同名(重载)函数中的哪一个。

于 2013-08-14T22:52:58.877 回答