如何构建包含多个源文件的DLL文件,并在多个文件中导出函数?
一些上下文:我正试图在使用Pelles C(使用C)的Windows上将Lua源代码构建成DLL(学习目的!)。主要针对x64,如果这很重要的话 通过使用my Pelles C中的DLL向导,它会自动生成一个带有示例函数和DLLMain.h的DLLMain.C。这很好,但现在我不确定如何导出所有其他lua函数。只需添加Lua网站告诉我的所有源文件(太简单了,不可能工作,但你永远不知道…),然后使用适当的如何构建包含多个源文件的DLL文件,并在多个文件中导出函数?,dll,lua,Dll,Lua,一些上下文:我正试图在使用Pelles C(使用C)的Windows上将Lua源代码构建成DLL(学习目的!)。主要针对x64,如果这很重要的话 通过使用my Pelles C中的DLL向导,它会自动生成一个带有示例函数和DLLMain.h的DLLMain.C。这很好,但现在我不确定如何导出所有其他lua函数。只需添加Lua网站告诉我的所有源文件(太简单了,不可能工作,但你永远不知道…),然后使用适当的#defines构建它,就可以导出示例函数,我已经使用polib.exe检查了该函数: 某些
#define
s构建它,就可以导出示例函数,我已经使用polib.exe检查了该函数:
某些来源:
自动生成的DLL主文件:
/****************************************************************************
* *
* File : dllmain.c *
* *
* Purpose : Generic Win32 Dynamic Link Library (DLL). *
* *
* History : Date Reason *
* 00/00/00 Created *
* *
****************************************************************************/
#define WIN32_LEAN_AND_MEAN /* speed up */
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <tchar.h>
/*
* Include our "interface" file and define a symbol
* to indicate that we are *building* the DLL.
*/
#define _LUADLLTE_
#include "LUADLLTE.h"
//!!!~~~!!!~~~!!!~~~!!!~~~!!!~~~!!!~~~!!!~~~!!!
#include "lua.h" //I added these 3. Do these not go here?
#include "lauxlib.h" //They are the 3 headers that every Lua tutorial
#include "lualib.h" //includes.
//~~~!!!~~~!!!~~~!!!~~~!!!~~~!!!~~~!!!~~~!!!~~~
/****************************************************************************
* *
* Function: DllMain *
* *
* Purpose : DLL entry and exit procedure. *
* *
* History : Date Reason *
* 00/00/00 Created *
* *
****************************************************************************/
BOOL APIENTRY DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
/*
* Microsoft says:
*
* blah comments blah
*/
break;
case DLL_THREAD_ATTACH:
/*
* Microsoft says:
*
* blah blah
*/
break;
case DLL_THREAD_DETACH:
/*
* blah blah
*/
break;
case DLL_PROCESS_DETACH:
/*
* blah
*/
break;
}
/* Return success */
return TRUE;
}
/****************************************************************************
* *
* Function: SampleFunction *
* *
* Purpose : Sample function which does nothing useful. *
* *
* History : Date Reason *
* 00/00/00 Created *
* *
****************************************************************************/
LUADLLTEAPI int WINAPI SampleFunction(int a, int b)
{
/* TODO: Replace with your own code */
return a * b;
}
luaconf.h,相关(我认为?)部分:
这些定义决定了luaapi调用的构建方式,具体取决于定义的符号。这就是我在头文件中添加它们的原因
我可以向自动生成的头/c文件添加函数,它们将根据polib.exe显示在库中。这让我觉得我需要找到每一个函数调用并将其移动到dllmain文件中,但这听起来并不是最好的方法
这些消息来源并没有真正回答这个问题:
~我找到的所有教程都是单文件DLL教程
~z~这一个很接近,但没有我需要的答案
~Lua网站,在Windows方面不是很清晰/具体
我完全有可能遗漏了一些很明显的东西。我首先浏览了单源/单头文件DLL来了解它,现在我想尝试编译Lua(实际上我想在某个时候尝试了解它)。如果这个问题有点复杂,请告诉我。我认为这是我的DLL知识的问题,而不是Lua的问题,所以我的问题是:
如何构建包含多个源文件的DLL文件,并在多个文件中导出函数?
PS:如果这可能是根本原因的话,我在这个话题上缺少什么基本知识?还有,很抱歉这么长…刚刚注意到
编辑:已解决谢谢你
方法:在Pelles C中,转到项目选项…->“编译器”选项卡,然后在定义预处理器符号行下输入LUA\u BUILD\u AS\u DLL LUA\u LIB
(或LUA\u BUILD\u AS\u DLL LUA\u CORE LUA\u LIB
)
所有要导出的Lua API函数都以Lua_API作为前缀。正如您所指出的,要正确定义LUA_API(对于visualstudio、GCC等),您只需要将LUA_BUILD_定义为_DLL和LUA_LIB
但是,您在头文件中定义它们,因此这些定义只存在于包含该头文件的文件中——即,没有任何Lua文件包含您的头文件
您需要在项目设置/makefile/中定义这些符号,以便在编译Lua源代码时定义它们。Man,就好像我对这样一个简单的疏忽说了那么多。你真是个天才D编辑:现在来测试我是否能真正使用它!一个简单的问题:其他一切都合理正确吗?我可以为3个Lua头添加3个includesdllmain吗?您只需要为在给定模块中实际使用的函数/类型添加头。您当前未使用它们,因此可以删除所有三个。如果要向DLL中添加一些自己的函数,可以使用lua.h
(包含主要的lua类型,如lua\u State
,以及核心lua API例程,如lua\u pushnumber
)和lauxlib.h
(包含辅助例程,如luaL\u checknumber
)。您不需要lualib.h
——加载luadll的主机应用程序将使用该头。
// INCLUDE FILE generated by "Pelles C for Windows, version 3.00".
#ifndef _LUADLLTE_H
#define _LUADLLTE_H
#ifdef _LUADLLTE_
#define LUADLLTEAPI __declspec(dllexport)
//!!!~~~!!!~~~!!!~~~!!!~~~!!!~~~!!!~~~!!!~~~!!!
#define LUA_BUILD_AS_DLL //I added these 3 defines as well.
#define LUA_CORE //See next source excerpt for the
#define LUA_LIB //reasoning.
//!!!~~~!!!~~~!!!~~~!!!~~~!!!~~~!!!~~~!!!~~~!!!
#else
#define LUADLLTEAPI __declspec(dllimport)
#endif /* _LUADLLTE_ */
#ifndef WINAPI
#define WINAPI __stdcall
#endif
LUADLLTEAPI int WINAPI SampleFunction(int, int);
#endif /* _LUADLLTE_H */
/*
@@ LUA_API is a mark for all core API functions.
@@ LUALIB_API is a mark for all auxiliary library functions.
@@ LUAMOD_API is a mark for all standard library opening functions.
** CHANGE them if you need to define those functions in some special way.
** For instance, if you want to create one Windows DLL with the core and
** the libraries, you may want to use the following definition (define
** LUA_BUILD_AS_DLL to get it).
*/
#if defined(LUA_BUILD_AS_DLL) /* { */
#if defined(LUA_CORE) || defined(LUA_LIB) /* { */
#define LUA_API __declspec(dllexport)
#else /* }{ */
#define LUA_API __declspec(dllimport)
#endif /* } */
#else /* }{ */
#define LUA_API extern
#endif /* } */
/* more often than not the libs go together with the core */
#define LUALIB_API LUA_API
#define LUAMOD_API LUALIB_API