Compiling tolua++ 1.0.93 with lua 5.1.4 and Visual Studio 2010

While one person hesitates because he feels inferior, the other is busy making mistakes and becoming superior. — Henry C. Link



Not long ago I decided it would be wise to extend my programming horizon and get to know C++ better. I kind of feel like a newb all over – again. Anyway, it seems that successes come in at a faster pace as they used to appear when learning other languages – which kind of makes learning C++ (even) more fun, despite the occasional and unavoidable mental blockade. Now that I have a bit more time, I apparently also decided to finally continue to work on my blog. Both things combined, and the need to compile tolua++ for a new project of mine, lead to this post. Note, that the instructions here are derived from the lua-users.org wiki article Compiling Toluapp Without Scons. The original author did already explain on how to compile it on VS2003 and VS2005, but left a small pitfall when compiling the actual executable. I tried to make this a version for dummies – if I succeeded, do not feel offended: it is also supposed to be a reference for myself :)

Index

  1. Needed tools
  2. Compiling tolua++.dll
  3. Compiling tolua++.exe

Needed tools

Extract the tolua++ archive to a directory of your choice. For the sake of this post I assume the directory is C:\LUA\tolua++\. Also install LuaForWindows. Here I assume the target directory to be C:\LUA\Lua4Win\. Make sure to select Lua Module Development files (libs and headers).

Compiling tolua++.dll

  • Create a new Microsoft Visual Studio 2010 C++ Win32-Project and name it tolua++.
  • In the Application-Wizard that will appear, select the option DLL and press the Finish button to create the project.
  • In the solution explorer, right click the source files folder, select Add->Existing Item and add all .c and .h files of the folder C:\LUA\tolua++\src\lib\ to the project.
  • Move the tolua_event.h file from the solution explorer source files folder via drag-and-drop into the solution explorer header files folder (not absolutely necessary, but looks nicer, eh).
  • In the solution explorer, right click the tolua++ project and select Properties.

Note: You might want to do the following for debug and release configurations. Just select the type of configuration from the drop-down list on the top left of the properties dialog.

  • C/C++ -> General: Additional include directories – here add the location of your LUA and your tolua++ include files (C:\LUA\tolua++\include and C:\LUA\Lua4Win\include).
  • C/C++ -> Preprocessor: Preprocessor definitions – add a new line with TOLUA_API=extern __declspec(dllexport). (without the period at the end … just the gray stuff o.o)
  • C/C++ -> Precompiled headers: Precompiled headers – select Do not use precompiled headers from the drop-down list.
  • Linker -> General: Additional library directories – add the LUA directory where your LUA library files are located (C:\LUA\Lua4Win\lib).
  • Linker -> Input: Additional dependencies – add lua51.lib on a new line (for other lua versions see the note below).
  • Accept those settings, close the properties dialog and press F7 to compile. If all went well you should find the resulting tolua++.dll and the tolua++.lib in either the debug or release directory of your VS project.

Note on lua-versions: If you happen to use a lua version different from 5.1, make sure to use the stub library. That is the one without the dot (.) between the major and minor version – or .lib with the same name as the smaller .dll file. So for lua versions 5.1.x this is lua51.lib (lua51.dll is the – smaller – stub dll).

Compiling tolua++.exe

This is almost the same process as above – just some minor differences. Important is to choose a different Visual Studio project type (console project), to use a different preprocessor definition (exchange dllexport for dllimport) and to also add your tolua++.lib file, which was created while compiling the tolua++.dll, as additional dependency. For the sake of completion, and to make sure no errors slip in, the whole procedure in detail:

  • Create a new Microsoft Visual Studio 2010 C++ Win32-Console-Project and name it toluac++ (note the different name – if your projects reside in the same directory they obviously can not have the same name).
  • In the Application-Wizard that will appear, untick the checkbox for precompiled headers and press the Finish button to create the project.
  • In the solution explorer, right click the source files folder, select Add->Existing Item and add tolua.c and toluabind.c files from the folder C:\LUA\tolua++\src\bin\ to the project.
  • In the solution explorer, right click the toluac++ project and select Properties.

Note: You might want to do the following for debug and release configurations. Just select the type of configuration from the drop-down list on the top left of the properties dialog.

  • C/C++ -> General: Additional include directories – here add the location of your LUA and your tolua++ include files (C:\LUA\tolua++\include and C:\LUA\Lua4Win\include).
  • C/C++ -> Preprocessor: Preprocessor definitions – do not add a definition here! TOLUA_API is already defined as “extern” in tolua++.h and that works fine for this build.
  • C/C++ -> Precompiled headers: Precompiled headers – select Do not use precompiled headers from the drop-down list. If you unticked the checkbox for precompiled headers in the application-wizard earlier, that should be taken care of.
  • Linker -> General: Additional library directories – add the LUA directory where your LUA library files are located (C:\LUA\Lua4Win\lib) and this time also add the location of the tolua++.lib file which got created when compiling the tolua++.dll. It should be located in either the release or debug subdirectory of the tolua++.dll Visual Studio project. You might want to copy it to a more suitable location – for example the directory C:\LUA\tolua++\lib\ which should so far be empty. Obviously then you should add this directory to the additional library directories list.
  • Linker -> Input: Additional dependencies – add lua51.lib and this time also tolua++.lib on new lines (for other lua versions see the note above).
  • Accept those settings, close the properties dialog and press F7 to compile. If all went well you should find the resulting toluac++.exe in either the debug or release directory of your VS project.

Both projects do compile with some warnings. Feel free to fix them – they do work for me. Also, if you do find some errors or encounter some problems, let me know – at this time of the night I tend to be less concentrated :P

VN:F [1.9.18_1163]
Rating: 5.0/5 (3 votes cast)
Compiling tolua++ 1.0.93 with lua 5.1.4 and Visual Studio 2010, 5.0 out of 5 based on 3 ratings
Sunday, February 26th, 2012 at 04:20
136 visits
  • Regnarock
    Oct 16th, 2012 at 13:54 | #1

    Hello,

    I followed your tutorial step by step. But I’m having a big trouble :

    (I cut the repetitive part of these errors)
    - – - – - – - – - – - – - – -
    1>tolua.obj : error LNK2019: unresolved external symbol _luaL_getn referenced in function _add_extra
    [---]
    1>..\..\bin\tolua++.exe : fatal error LNK1120: 7 unresolved externals
    - – - – - – - – - – - – - – - – -

    I used Notepad++ search tool to check for this symbol in every files of the lua installation. And I wasn’t able to find them. It looks like it’s not possible with the lua for windows. Therefore I found every symbols needed in every other normal release of lua.

    Did I misunderstand something ? (I’m not English native). Or is there really something wrong here ?

    Thx by advance.

    VA:F [1.9.18_1163]
    Rating: 0.0/5 (0 votes cast)
  • Regnarock
    Oct 16th, 2012 at 14:07 | #2

    I made a better check. It appears that the symbol “luaL_getn” does not exist anymore after lua5.0.

    I found some lines in lua5.1 speaking about that :

    - – - – - – - – - -
    luaconf.h l.317 :
    /*
    @@ LUA_COMPAT_GETN controls compatibility with old getn behavior.
    ** CHANGE it (define it) if you want exact compatibility with the
    ** behavior of setn/getn in Lua 5.0.
    */
    #undef LUA_COMPAT_GETN
    - – - – - – - – - -

    Still, I don’t know what to do to use 5.1. I’m going to compile with 5.0 until you reply me.

    Thx for your time

    VA:F [1.9.18_1163]
    Rating: 0.0/5 (0 votes cast)
    • Oct 17th, 2012 at 11:20 | #3

      Well, I just rebuild both projects from scratch, using freshly downloaded sources from the links mentioned above (using the newest LuaForWindows_v5.1.4-46.exe and tolua++ 1.0.93). Following my own guide step by step everything compiles fine – no error messages whatsoever.

      To me it seems that you are not referencing the proper lua library file (i.e. lua51.lib) in your project. So make sure you set the proper library path in the general linker settings, and add the proper library in the linker input settings.

      Maybe double-check that you didnt mingle up your lua 5.0 project with this one. If you are using the versions I linked above you really should have the proper dependencies and no unresolved externals.

      regards, Sascha

      VN:F [1.9.18_1163]
      Rating: 5.0/5 (1 vote cast)

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>