1

Background

I'm currently dabbling in the world of game programming, and following an online guide from bell0bytes. Right now I'm working on this tutorial: https://bell0bytes.eu/lua-and-game-settings/

Therein, the writer suggests "Avoiding DLL Hell" by simply placing the Lua DLL in the local project directory, but that's sloppy, IMO. I've recently started a career as a software engineer (primarily C++) and my team uses CMake and VS to manage our product. I'm confident in my C++ skills and in using VS, but I'm far from mastering CMake, and I'm trying to improve my skills across the board by maintaining a similar dev environment to my work environment for this personal project, meaning that putting the DLL into my driver/project directory and calling it a day isn't sufficient here.

So indeed, we're off to DLL Hell.

System & Versioning Information

OS: Microsoft Windows 10 Pro (10.0.18362 Build 18362)
Sys: x64 Desktop
VS: Microsoft Visual Studio Community 2019 (16.5.4)
CMake: CMake (3.17.1) (Windows x64 Executables for Attempt 1, Source Code and Makefiles for Attempt 2)
Lua: Lua (5.3.5)
Relevant Directories: ../dev/<myprojectdirectory>/, ../dev/resources/

Goal

I want to write the line #include <lua.h> or #include <lua.hpp> or something to that effect in my driver (.cpp / WinMain file) such that I can access the data structures and methods within that Lua header, with the stipulation that this dependency needs to be managed by CMake external to my project, meaning no Lua files end up copied into my working project directory (i.e. Not in here: ../dev/<myprojectdirectory>/ , or any of its subdirectories (excluding maybe <myprojectdirectory>/build/ (CMake generated bin) directory... assuming someone can explain why that's acceptable, from a professional / software engineering standpoint)).

Attempt 1

Originally I grabbed the x64 binaries for Lua from here: http://luabinaries.sourceforge.net/download.html

and after extracting the compressed content, I put the DLL here: ../dev/resources/Lua/lua53.dll

which I'm good with. A solution in which CMake grabs that DLL from my resources directory, and links it into such that IntelliSense can actually find the lua header, such that I can write the following code (from the guide I referenced earlier):

// BEGIN This is my driver 

#include <windows.h>
#include <lua.hpp>
#pragma comment(lib, "liblua53.a") // Fairly sure I wont need this line if Lua is linked correctly

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    lua_State *state = luaL_newstate();
    lua_close(state);
    return 0;
} 

// END my driver

and successfully compile my bare bones project, would be perfect.

Attempt 2:

Alternatively a Statically linked solution that works something like the following would be fine too:

I searched stackoverflow and found this, which made me hopeful at first:

Download and build Lua with modern CMake

After adjusting the following code, provided by the OP from the above thread:

cmake_minimum_required(VERSION 3.10)
include(ExternalProject)

ExternalProject_Add(lua
   URL "https://www.lua.org/ftp/lua-5.3.5.tar.gz"
   CONFIGURE_COMMAND ""
   BUILD_COMMAND make generic
   BUILD_ALWAYS true
   BUILD_IN_SOURCE true
   INSTALL_COMMAND ""
)
add_library(liblua STATIC IMPORTED)
ExternalProject_Get_property(lua SOURCE_DIR)
message("liblua will be found at \'${SOURCE_DIR}/src\'")

to my needs, my top level, bare bones CMakeLists.txt file ../dev/<myprojectdirectory>/CMakeLists.txt looks like this:

# BEGIN This is my CMakeLists.txt file

cmake_minimum_required(VERSION 3.2.3)

project(<myprojectname>)

cmake_minimum_required(VERSION 3.10)
include(ExternalProject)

ExternalProject_Add(lua
   URL "https://www.lua.org/ftp/lua-5.3.5.tar.gz"
   CONFIGURE_COMMAND ""
   BUILD_COMMAND make generic
   BUILD_ALWAYS true
   BUILD_IN_SOURCE true
   INSTALL_COMMAND ""
)
add_library(liblua STATIC IMPORTED)
ExternalProject_Get_property(lua SOURCE_DIR)
message("liblua will be found at \'${SOURCE_DIR}/src\'")

add_executable(${PROJECT_NAME} WIN32 <projectsource>)

# END my CMakeLists.txt file

I configured CMake(no errors/warnings) + generated, and finally opened the newly created VS project, thinking that I could #include <lua.h> / #include <lua.hpp> (statically, I think?) with no further trouble, but that didn't work, VS/IntelliSense didn't find the header.

Closing Remarks:

Eventually, I couldn't find anything else to try, or any more clues (I looked through documentation for Lua and CMake on their respective websites for instructions as well). So, stuck in Hell for the first time in quite a while, I decided it was about time I start an account with stack, and query the community.

The solution I'm looking for (of course) gets me to the point where I can use Lua cleanly (preferably dynamically, but statically would be Ok, without manually adjusting any project properties from VS) (If possible please include full code/files for CMakeLists.txt, or C++ drivers, not just snippets, with explanations).

Finally (and importantly), I'm much more interested in an explanation surrounding DLLs, static libraries, CMake, correctly establishing VS project dependencies & linking via CMake, and hopefully, an explanation of where/why I fell short on these two attempts.

Thanks!

Other Stack Exchange Resources I've Reviewed In My Quest For the Answer

Building 64bit application with makefiles using cmake-gui
How do I set the path to a DLL file in Visual Studio?
How to use external DLLs in CMake project
Ideal way to include headers from a DLL?
Unable to find Lua headers with find_package in cmake
Download and build Lua with modern CMake

~ I'm not stupid, but you're welcome to explain it to me like I'm 3. I want to understand, not just get the answer.

  • You have a problem in including header files, but in your `CMakeLists.txt` I see no `include_directories`, `target_include_directories` or other commands which adds directories for search header files. Note, that `ExternalProject_Add` doesn't perform this work for you. – Tsyvarev May 14 '20 at 15:42
  • *Lua* generally comes built with *Nix* cross tools, which are not very compatible with *VStudio*. If you add its source to *CMake*, you'll have to write the build spec yourself, as it doesn't magically build itself. As an alternative, check https://github.com/CristiFati/Prebuilt-Binaries/tree/master/Lua/v5.3.5. – CristiFati May 16 '20 at 01:55

0 Answers0