简体   繁体   English

如何在共享库中执行某些功能

[英]How to execute some function in a shared library

I'm wondering how can a program "recoglize" some function in some executable or shared library if they're not compiled together. 我想知道如果程序没有一起编译,怎么能“重新识别”某些可执行文件或共享库中的某些功能。 I know that sounds confusing, here's a much detailed explaination: 我知道这听起来令人困惑,这是一个非常详细的解释:

Suppose I have a program called "A". 假设我有一个名为“ A”的程序。 Now I want to put some additional compiled shared library "B" in some specified location so that "A" can run function in it, without changing the code of A itself. 现在,我想在某些指定位置放置一些附加的已编译共享库“ B”,以便“ A”可以在其中运行功能,而无需更改A本身的代码。 Of course I can use also some additional file like txt file, and of course the source code is visible. 当然,我也可以使用一些其他文件,例如txt文件,并且源代码是可见的。 How should I design the whole system so that it can work? 我应该如何设计整个系统,使其能够正常工作?

If you think about it as a game, EG a card game, here is the situation. 如果您将其视为游戏,例如将其视为纸牌游戏,那么情况就是这样。 You have the game, and you insist on writing purely C++ (so you have to compile everything). 您拥有游戏,并且坚持只编写C ++(因此必须编译所有内容)。 Now you already have the publised game. 现在您已经拥有了发布的游戏。 But some DIYer want to add some new card to the game (they can't change the executable itself since what if there are more that one DIY?). 但是有些DIYer想要在游戏中添加一些新卡(它们不能更改可执行文件本身,因为如果有多个DIY,该怎么办?)。 You can do the card data part by txt or db or whatever, but for the "effect" part, you have to code and compile them to something. 您可以通过txt或db或其他方式来处理卡片数据,但是对于“效果”部分,您必须进行编码并将其编译为某种形式。 How should the main program call the card "effect" function in compiled library? 主程序应如何在已编译的库中调用卡片的“效果”功能?

And by the way I know I can do it with sript language like LUA or whatever, but what if I want to do it "the compiled way"? 顺便说一句,我知道我可以使用LUA之类的sript语言来完成此操作,但是如果我想以“编译方式”进行操作,该怎么办?

Here is the more specific example: 这是更具体的示例:

A.cpp: int main(){ FuncA(); A.cpp:int main(){FuncA(); FuncB(); FuncB(); FuncC(); FuncC(); ... } // and something else ...} //其他

B.cpp: int TheFunction(){ .... } // that's it B.cpp:int TheFunction(){....} //就这样

The program don't know the existence of "B", but it is designed so that if I can compile B to some kind of B.so, and put it in a specified folder, "A" can execute the TheFunction() 该程序不知道“ B”的存在,但它的设计目的是,如果我可以将B编译为某种B.so,并将其放在指定的文件夹中,则“ A”可以执行TheFunction()

I know that sound really wield, but here is what I am thinking about: 我知道声音确实会发出,但是我在想什么:

When compiling B, use some way to have the compiler generate some kind of function pointer list to a txt file. 编译B时,请使用某种方式使编译器生成指向txt文件的某种函数指针列表。 And the program A reads that file, and runs the function the pointer points to in B. 然后程序A读取该文件,并在B中运行指针指向的功能。

Is that possible? 那可能吗? Or there's something I actually understood wrong? 还是我实际上理解错了?

Dynamic linking is not something that's covered by the C++ standard so how this works is implementation dependent. 动态链接不是C ++标准所涵盖的,因此它的工作方式取决于实现。 On Windows for example you would build a DLL and then load it dynamically using LoadLibrary() and get a pointer to the function you want to call using GetProcAddress() . 例如,在Windows上,您将构建一个DLL,然后使用LoadLibrary()动态加载它,并使用GetProcAddress()获得指向您要调用的函数的指针。 Other platforms offer similar functionality using different APIs. 其他平台使用不同的API提供类似的功能。

Of course C++ is a statically compiled language so you need to know the signature of a function before you can call it. 当然,C ++是静态编译的语言,因此您需要先了解函数的签名,然后才能调用它。 Your program will have to define some standard function interface for 'plugins' to implement in DLLs in order to be able to call into code that is written after the original program is built. 您的程序必须为“插件”定义一些标准功能接口,以便在DLL中实现,以便能够调用在原始程序构建后编写的代码。

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

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