3

This is the first question I have found myself not being able to get to the bottom of using my normal googling/stack overflowing/youtubing routine.

I am trying to compile a minimal Lua program inside of a C++ environment just to ensure my environment is ready to development. The Lua language will be later used for User Interface programming for my C++ game.

First some basic information on my environment:

Windows 7 64-bit

Visual studio 2010

Lua for Windows 5.1 (latest build I could download from google code)

Here is the code I am trying to compile:

// UserInt.cpp : Defines the entry point for the console application.
//
#pragma comment(lib,"lua5.1.dll")
#include "stdafx.h"
#ifndef __LUA_INC_H__
#define __LUA_INC_H__

extern "C"
{
   #include "lua.h"
   #include "lauxlib.h"
   #include "lualib.h"
}



int _tmain(int argc, _TCHAR* argv[])
{
    lua_State * ls = luaL_newstate();
    return 0;
}

#endif // __LUA_INC_H__

Here is the Error I am getting:

1>UserInt.obj : error LNK2019: unresolved external symbol _luaL_newstate referenced in function _wmain 1>c:\users\deank\documents\visual studio 2010\Projects\UserInt\Debug\UserInt.exe : fatal error LNK1120: 1 unresolved externals

Things I have tried:

I have read about lua_open()(and several other functions) no longer being used so I tried the newstate function instead. I get the same error. This was more of a sanity check than anything. I am using 5.1 and not 5.2 so I do not think this really matters.

I have also read this thread Cannot link a minimal Lua program but it does not seem to help me because I am not running the same environment as that OP. I am on a simple windows 7 and visual studio environment.

The top pragma comment line was something I saw in yet another thread. I get the same error with or without it.

I have gone into my visual studio C++ directories area and added the lua include to the includes and the lua lib to the libraries.

So it seems like my program is seeing the .h and seeing the symbol. But for some reason it is not getting the .cpp implementation for the functions. This is why I was hoping including that .dll directly would help fix the problem, but it hasn't.

So, I feel like I have exhausted all of my options solving this on my own. I hope someone is able to help me move forward here. Lua looks like an awesome language to script in and I would like to get my environment squared away for development.

I hope it is just some silly error on my part. I believe I have provided as much information as I can. If you need more specifics I will update with info if I can provide it.

Edit1

Tried the solution in this Can't build lua a project with lua in VS2010, library issue suspected

That did not work either.

Community
  • 1
  • 1
Dean Knight
  • 660
  • 6
  • 17

1 Answers1

9

You'll need to have the library (.LIB) file and add that to VS. Use Project > Properties and go to Linker > Input and add the full .lib filename to the "Additional Dependencies" line. Note that the .LIB is different from the .DLL.

Personally, I prefer adding the source code to my project, over referencing the dynamic link library. The following procedure will let you do as such.

  1. Download the source code ( http://www.lua.org/ftp/ ), uncompress it.

  2. In Visual Studio, choose File > New > Project and choose Visual C++, Win32, "Win32 Console Application".

  3. In your project in Visual Studio, add all the source code, except luac.c. Also delete the main() function out of the file that VS created for you. This is usually given the name of the project you specified with the .cpp file extension. You could just remove this file all-together from the project.

  4. Build and Run.

This is the Lua console

PaulPerry
  • 906
  • 5
  • 14
  • 1
    Well, you get my vote. Adding the .lib did the trick. But I am now more curious about your steps to add the actual source code. This seems like a better solution. I could just add all of the .h's and .c's (except luac.c) to my project and it should be equal to depending on the library right? I should be able to keep my application intact without changing anything and just add a "Lua" filter to hide all of those files under... if I am understanding correctly. I just want to be able to execute lua scripts C++-side for UI loading. Also, I would upvote you if I could. Not enough rep to do so. – Dean Knight Aug 06 '12 at 22:14
  • 2
    Yes, pretty much what you wrote, with slight modification. To just embed Lua in your application, you will NOT add lua.c (in addition to luac.c already mentioned) to your project. The lua.c file has it's own main() which handles the console (get a line of text and display the result) which you probably don't want in your own application. – PaulPerry Aug 06 '12 at 22:34
  • That is exactly what I want. Thanks! 110% satisfied with the answer. – Dean Knight Aug 06 '12 at 23:10
  • Finally had enough Reputation to upvote your answer. It deserved one. Thanks again – Dean Knight Aug 23 '12 at 15:15