简体   繁体   English

在类声明中定义的C ++函数不会显示为链接器符号

[英]c++ functions defined inside class declaration don't show up as linker symbols

Here's my C++ class, part defined inside class and part outside. 这是我的C ++类,部分在类内部定义,而部分在外部定义。 The problem: doing nm reveals that functions defined in the class declaration don't show up in object file, but those outside do. 问题是:执行nm揭示类声明中定义的函数未显示在目标文件中,而外部函数显示在目标文件中。 What should I do to make the symbols inside the class turn up in the object file as well? 如何使类中的符号也出现在目标文件中? I am using g++ 4 series. 我正在使用g ++ 4系列。

class A {
  public: 
    void this_will_not_show_up() { };
    void this_will_show_up();
};
void A::this_will_show_up() { }

The functions that are defined inline in the class definitions are compiled as inline functions ie the code in this functions is 'copied' to the calling functions. 在类定义中内联定义的函数被编译为内联函数,即该函数中的代码被“复制”到调用函数。 Therefore you don't have any dedicated implementation of the inline function and there is no linker symbol. 因此,您没有内联函数的任何专用实现,并且没有链接器符号。

When you define the method non-inline in a compilation unit it may be that this function is called from a different compilation unit. 当在编译单元中定义非内联方法时,可能是从另一个编译单元调用了此函数。 Therefore this implementation requires functions that can be linked. 因此,此实现需要可以链接的功能。 The compiler just generates a function including a header and a (decorated) name that you find in the linker map. 编译器仅生成一个函数,该函数包括在链接器映射中找到的标题和(修饰的)名称。

It's no problem when you using this in a shared lib since all what your compiler needs to use is defined in the header files. 在共享库中使用它时没有问题,因为编译器需要使用的所有内容都在头文件中定义。 There are libraries like the STL that are nearly completely implemented as header only. 像STL这样的库几乎完全只作为标头实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在 C++ 中的类中定义的方法在引用仅在稍后出现在 class 中的方法时不需要前向声明? - Why don't methods defined in classes in C++ need forward declarations when referring to methods that only show up later in the class? 为什么较新版本的C ++不允许在类声明中定义纯方法? - Why newer versions of C++ don't allow definition of pure method inside class declaration? 多个已定义符号的C / C ++链接器顺序 - C/C++ linker order for multiple defined symbols 在 C++ 中的类声明中声明的函数如何执行? - How functions executed declared inside class declaration in c++? 链接器找不到在虚拟类 C++ 中访问的静态字段的符号 - Linker can't find symbols for static field accessed in virtual class c++ C ++:类模板,链接器错误和未解析的符号 - C++ : Class templates, linker errors, and unresolved symbols C++ 游戏中没有出现 Print 语句 - The Print statements don't show up in the C++ game 标题不会出现在 Dev C++ 中 - Headers don't show up in Dev C++ C ++链接器错误-未内联定义函数的“未定义引用” - c++ linker error - 'undefined reference' to not inline defined functions C++ Visual Studio 中的 Ascii 字符代码与实际字符符号不匹配? - Ascii character codes in C++ visual studio don't match up with actual character symbols?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM