简体   繁体   English

如何在库中使用静态函数

[英]How to use a static function inside a library

I have a library libxx and inside that library there is a static function foo(). 我有一个库libxx,并且在该库中有一个静态函数foo()。 I want to write another function bar() which uses the function foo() as a subroutine. 我想编写另一个函数bar(),它使用函数foo()作为子例程。

Is there any way to do it without writing bar() on the file which the function foo() sits on and rebuilding the library? 有什么方法可以在不将foo()所在的文件上写入bar()并重建库的情况下完成此操作?

Declaring a function static within a translation unit (object file / library) pretty much invalidates any assertions about that function's implementation. 在转换单元(目标文件/库)中声明static函数几乎会使有关该函数实现的任何声明无效。 It could be completely inlined. 可以完全内联。 It might use short-cuts with the calling conventions that would be incorrect when called externally. 它可能使用快捷方式以及在外部调用时不正确的调用约定。

On some OS/ABIs, the function will not be 'visible' in the sense that a .globl directive with ELF/Mach-O would provide, and the linker will prevent it, or the loader won't resolve it. 在某些OS / ABI上,该功能在具有ELF/Mach-O.globl指令将提供的意义上是“不可见的”,并且链接程序将阻止它,否则加载程序将无法解析它。 In a shared library, it might not preserve position-independence outside its local use. 在共享库中,它可能不会在其本地使用范围之外保留位置独立性。 The point is, you just can't do this safely or portably. 关键是,您无法安全或方便地执行此操作。

In short, you need to recompile the function as non-static, ie, a global / visible symbol in the library, or have a static version available to your bar() function. 简而言之,您需要将函数重新编译为非静态函数,即库中的全局/可见符号,或者为bar()函数提供静态版本。 Say, via static inline . 说,通过static inline

Not within standard C/C++. 不在标准C / C ++中。

The function has an address which is callable or other functions which may allow the foo to be called indirectly. 该函数具有一个可调用的地址或其他允许foo被间接调用的函数。

But these are bad solutions 但是这些都是不好的解决方案

It sounds like you have the source code to libxx. 听起来您具有libxx的源代码。 If that's the case, and you'd like bar() to exist in the same library as foo() then you'll need to recompile libxx after adding bar() to it. 如果是这种情况,并且您希望bar()与foo()存在于同一库中,则需要在添加bar()之后重新编译libxx。

If your goal is to call libxx::foo() from another program/library then you can just link libxx to your other project (which has bar() in it). 如果您的目标是从另一个程序/库调用libxx :: foo(),则只需将libxx链接到另一个项目(其中包含bar())。 Once the library is linked to your main project all you need to do is call into the correct namespace and invoke foo(). 一旦库链接到您的主项目,您所需要做的就是调用正确的名称空间并调用foo()。 Linking a library to your main project will require using a linker. 将库链接到主项目将需要使用链接器。 Different development systems will have different ways of invoking the linker. 不同的开发系统将具有不同的调用链接器的方式。 For researching this further, I would advise that you search for "c++ linking to static library X " where X is your development system (ex: visual studio, xcode). 为了对此进行进一步的研究,我建议您搜索“链接到静态库X的 c ++”,其中X是您的开发系统(例如:visual studio,xcode)。 I'm assuming this is a static library based on the tag you put on the question, but maybe you meant that foo() was a static method. 我假设这是一个基于您在问题上添加的标记的静态库,但也许您是说foo()是静态方法。 It shouldn't matter if it's a static method or not, linking will work in the same way. 不管它是否是静态方法都没有关系,链接将以相同的方式起作用。 If you are confused about what I mean when I write "static library" then maybe researching the difference between static and dynamically linked libraries will clarify. 如果您对编写“静态库”时的意思感到困惑,那么研究静态和动态链接库之间的区别可能会很清楚。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM