Some context: I’m trying to build the Lua source into a DLL (learning purposes!), on Windows with Pelles C (using C). Targeting x64 mainly, if that is significant.
By using the DLL wizard in my Pelles C, it autogenerates a DLLMain.c with a sample function and DLLMain.h. This is fine, except now I’m not sure how to export all the other lua functions as well. Simply adding all the source files (which is too simple to possibly ever work, but you never know…) that the Lua website tells me to and then building it with appropriate #defines just exports the sample function, which I’ve checked using polib.exe:

Some source:
Auto-generated DLL main:
/****************************************************************************
* *
* 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;
}
Aut-generated header:
// 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 */
luaconf.h, relevant (i think?) portion:
/*
@@ 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
These defines determine how the lua api calls get built, depending on what symbols are defined. That’s why I added them in the header file.
I can add functions to the autogenerated header/c file and they will show up in the lib according to polib.exe. This leads me to think that I need to go and find every single function call and move it to the dllmain file, but that doesn’t sound like the optimal way to do this.
These sources didn’t really answer this question:
would appreciate for some one pointing me to good DLL tutorial
~All the tutorials I have found are single-file DLL tutorials.
Compiling & Decompiling dll files
~This one came close, but no answer along the lines I need.
~The Lua website and build documentation aren’t very clear/specific when it comes to Windows.
It’s entirely possible I’m missing something pretty obvious. I went through single source/single header file DLL’s first to get a feel for it, and now I want to try compiling Lua (which i actually want to try to learn about at some point). Let me know if this question is a little convoluted. I figure it is a problem with my DLL knowledge, not with Lua, so my question is:
How do you build a DLL file containing multiple source files, with functions to be exported in multiple files?
PS: What fundamental know-how am I missing regarding this topic, if that may be the root cause? Also, sorry for the length….just noticed…
Edit: Solved! Thank you Mud.
Method: In Pelles C, go to Project Options… -> Compiler tab, and enter LUA_BUILD_AS_DLL LUA_LIB (or LUA_BUILD_AS_DLL LUA_CORE or LUA_BUILD_AS_DLL LUA_CORE LUA_LIB)under the Define preprocessor symbols line.

All the Lua API functions which you want exported are prefixed with LUA_API. As you noted, to define LUA_API correctly (for Visual Studio, GCC, et al.) you need only define LUA_BUILD_AS_DLL and LUA_LIB.
However, you define them in a header file, so those definitions only exist in the files that include that header — i.e. none of the Lua files include your header.
You need to define those symbols in your project-settings/makefile/whatever so they are defined during compilation of the Lua sources.