用gcc编译一个DLL [英] Compiling a DLL with gcc

查看:186
本文介绍了用gcc编译一个DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sooooo我正在写一个脚本解释器。基本上,我想要一些类和函数存储在DLL中,但我希望DLL能够在链接到它的程序中寻找函数,例如,

  program dll 
------------------------------------ ----------------
将代码发送给dll ----->解析代码
|
v
代码包含一个函数,
不包含在DLL中
|
函数列表在< ------ /
程序
|中
v
相应的函数,
用户在
程序中定义 - 在这里处理
传递的参数
|
\ -------------->返回值返回
到解析函数

我基本上想知道,我该如何编译一个使用gcc的DLL?那么,我正在使用gcc的windows端口。一旦我编译了一个包含我的类和函数的.dll文件,我如何链接到我的程序?我如何使用DLL中的类和函数? DLL可以调用链接到它的程序的函数吗?如果我创建一个类{...}对象;在DLL中,然后当程序加载DLL时,程序是否可以使用对象?在此之前,我真的需要知道如何在C ++中使用DLL,然后才能继续此项目。



您可以添加更多关于您为什么需要的详细信息该DLL在主程序中调用函数?



我认为这个图解释了它...程序使用DL​​L将一段代码传递给DLL ,它解析代码,并且如果在所述代码中找到函数调用,则调用DLL中的相应函数...例如,如果我传递了a = sqrt(100),那么DLL分析器函数将查找函数调用到sqrt(),并在DLL内将是一个相应的sqrt()函数,它将计算传递给它的参数的平方根,然后它将从该函数返回值并将其放入变量a ...就像任何其他程序一样,但是如果在DLL中没有找到sqrt()函数的相应处理程序(将会有一个本机支持的函数列表),那么它会调用类似的乐趣它将驻留在使用DLL的程序中,以查看是否有用户定义的函数名称。

因此,假设您将DLL加载到程序中你的程序能够解释这种特定语言的脚本,程序可以调用DLL来处理单行代码或者传递脚本的文件名以处理......但是如果你想添加一个命令到符合目的的脚本中你可以说在DLL中设置一个布尔值,告诉它你正在为它的语言添加函数,然后在你的代码中创建一个函数来列出你正在添加的函数(这个DLL会用它的名字来调用它)它所需的函数,如果该函数是代码中包含的用户定义的函数,则该函数将使用DLL传递给它的参数调用相应的函数,并将用户定义的函数的返回值返回给DLL,如果它不存在,它会返回一个错误代码或NULL或其他)。我开始看到,我必须找到另一种方法来使函数调用只有一种方式

解决方案

此链接解释了如何以基本方式完成此操作。



在大图中,当你制作一个dll时,你正在制作一个在运行时加载的库。它包含一些导出的符号。这些符号通常是对方法或函数的引用,以及编译器/链接器傀儡。



当你通常建立一个静态库时,有一个最低限度的傀儡和链接器拉入它需要的代码,并重新包装它在你的可执行文件中。



在一个dll中,你实际上得到两个最终产品(三个真的 - 只是等待):一个dll和一个存根库。该存根是一个静态库,看起来与常规静态库非常相似,除了不是执行代码,每个存根通常是对常规例程的跳转指令。常见的例程加载你的DLL,获取你想调用的例程的地址,然后修补原始跳转指令到那里,所以当你再次调用它时,你最终会进入你的dll。



第三个最终产品通常是一个头文件,告诉你所有关于库中的数据类型。



所以你的步骤是:create你的头文件和代码,构建一个dll,从头文件/代码/某些导出函数列表构建一个存根库。结束代码将链接到存根库,它将加载dll并修复跳转表。



编译器/链接器goo包括诸如确保运行时库在哪里他们是必要的,确保静态构造函数被执行,确保静态析构函数被注册以便执行等等等等。



现在对于你的main问题:我如何在dll中编写可扩展的代码?有许多可能的方法 - 一种典型的方法是定义一个纯粹的抽象类(aka接口)来定义一个行为,并将其传递给一个处理例程或创建一个例程来注册接口来完成工作,然后处理例行公事要求注册服务机构处理一件工作。


Sooooo I'm writing a script interpreter. And basically, I want some classes and functions stored in a DLL, but I want the DLL to look for functions within the programs that are linking to it, like,

       program                dll
----------------------------------------------------
send code to dll----->    parse code
                              |
                              v
                          code contains a function,
                          that isn't contained in the DLL
                              |
list of functions in   <------/
program
      |
      v
corresponding function,
user-defined in the
program--process the
passed argument here
      |
      \-------------->    return value sent back
                          to the parsing function

I was wondering basically, how do I compile a DLL with gcc? Well, I'm using a windows port of gcc. Once I compile a .dll containing my classes and functions, how do I link to it with my program? How do I use the classes and functions in the DLL? Can the DLL call functions from the program linking to it? If I make a class { ... } object; in the DLL, then when the DLL is loaded by the program, will object be available to the program? Thanks in advance, I really need to know how to work with DLLs in C++ before I can continue with this project.

"Can you add more detail as to why you want the DLL to call functions in the main program?"

I thought the diagram sort of explained it... the program using the DLL passes a piece of code to the DLL, which parses the code, and if function calls are found in said code then corresponding functions within the DLL are called... for example, if I passed "a = sqrt(100)" then the DLL parser function would find the function call to sqrt(), and within the DLL would be a corresponding sqrt() function which would calculate the square root of the argument passed to it, and then it would take the return value from that function and put it into variable a... just like any other program, but if a corresponding handler for the sqrt() function isn't found within the DLL (there would be a list of natively supported functions) then it would call a similar function which would reside within the program using the DLL to see if there are any user-defined functions by that name.

So, say you loaded the DLL into the program giving your program the ability to interpret scripts of this particular language, the program could call the DLLs to process single lines of code or hand it filenames of scripts to process... but if you want to add a command into the script which suits the purpose of your program, you could say set a boolean value in the DLL telling it that you are adding functions to its language and then create a function in your code which would list the functions you are adding (the DLL would call it with the name of the function it wants, if that function is a user-defined one contained within your code, the function would call the corresponding function with the argument passed to it by the DLL, the return the return value of the user-defined function back to the DLL, and if it didn't exist, it would return an error code or NULL or something). I'm starting to see that I'll have to find another way around this to make the function calls go one way only

解决方案

This link explains how to do it in a basic way.

In a big picture view, when you make a dll, you are making a library which is loaded at runtime. It contains a number of symbols which are exported. These symbols are typically references to methods or functions, plus compiler/linker goo.

When you normally build a static library, there is a minimum of goo and the linker pulls in the code it needs and repackages it for you in your executable.

In a dll, you actually get two end products (three really- just wait): a dll and a stub library. The stub is a static library that looks exactly like your regular static library, except that instead of executing your code, each stub is typically a jump instruction to a common routine. The common routine loads your dll, gets the address of the routine that you want to call, then patches up the original jump instruction to go there so when you call it again, you end up in your dll.

The third end product is usually a header file that tells you all about the data types in your library.

So your steps are: create your headers and code, build a dll, build a stub library from the headers/code/some list of exported functions. End code will link to the stub library which will load up the dll and fix up the jump table.

Compiler/linker goo includes things like making sure the runtime libraries are where they're needed, making sure that static constructors are executed, making sure that static destructors are registered for later execution, etc, etc, etc.

Now as to your main problem: how do I write extensible code in a dll? There are a number of possible ways - a typical way is to define a pure abstract class (aka interface) that defines a behavior and either pass that in to a processing routine or to create a routine for registering interfaces to do work, then the processing routine asks the registrar for an object to handle a piece of work for it.

这篇关于用gcc编译一个DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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