Search code examples
c++visual-c++dllluavisual-c++-2010

How to link Lua in Microsoft Visual C++.2010 Express?


I am trying to write C++ classes/functions which can be accessed from Lua.

I need to export the C++ library as dll files.

I'm using Microsoft Visual C++.2010 Express

So I create a project and add the require .cpp files and .h files Now when I try to build, the following error occurs.

mylib.obj : error LNK2019: unresolved external symbol _lua_settop referenced in function "void __cdecl g_initializePlugin(struct lua_State *)" (?g_initializePlugin@@YAXPAUlua_State@@@Z)

Similar errors occur for all lua functions. When Googling I learnt that I had to link LUA.

How can that be done in Visual C++.

P.S I found a solution which directed me to give

#pragma comment( lib, "lua5.1" ) 

For this solution, it requires a lua5.1 file. From where should I download that file and where should it go (in the project folder?) ?


Solution

  • OK! I figured out how!

    To link Lua, the following has to be done after the C++ project has been created.

    • Copy C:\Program Files\Lua\5.1\lib\lua5.1.dll to your project folder
    • Copy C:\Program Files\Lua\5.1\lib\lua5.1.lib to your project folder
    • Right Click on Project --> Properties --> Configuration Properties --> Linker --> Input.
      • Add lua5.1.lib to Additional Dependencies
    • Right Click on Project --> Properties --> Configuration Properties --> VC++ Directories.
      • Add C:\Program Files\Lua\5.1\include to Include Directories

    NOTE : my lua installation is in C:\Program Files\Lua\5.1

    Now on building the project, dll file is created!