Embedding Lua in Visual C++ 2008

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

User avatar
Cole S.
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 59
Joined: Sat Dec 06, 2008 2:49 pm

Embedding Lua in Visual C++ 2008

Post by Cole S. »

Well, I am making a game engine for Windows with C++, Win32 API, DirectX, and Lua. So far everything has been working fine except for Lua and it's really getting annoying... Anyways, I have downloaded the Lua source code, built it, and i can run any script i put in the lua.exe application (the console app) such as: print("hello"). Here's where my problem is, I have added the src folder in the MSVC++ include directories, and in the library directories. When I try to compile this program:

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

int main()
{

lua_State *L = lua_open();

// load the libs
luaL_openlibs(L);

printf("Hello World");

lua_close(L);

return 0;
}

I get the following build log...

1>------ Build started: Project: luatest, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>Linking...
1>main.obj : error LNK2019: unresolved external symbol _lua_close referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _luaL_openlibs referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _luaL_newstate referenced in function _main
1>C:\Users\Cole\Documents\Visual Studio 2008\Projects\luatest\Debug\luatest.exe : fatal error LNK1120: 3 unresolved externals
1>Build log was saved at "file://c:\Users\Cole\Documents\Visual Studio 2008\Projects\luatest\luatest\Debug\BuildLog.htm"
1>luatest - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

WHAT THE HECK IS GOING ON!!! I have looked all over the internet and I cannot find what is wrong!!! Please reply soon.

----------------------------------------------------------------------------------------------------------------------------------------------

Edit- I can now compile it but when i try to run it it says "This application has failed to start because lua5.1.4.dll was not found. Re-installing the application may fix this problem."
Last edited by Cole S. on Sun Dec 07, 2008 9:56 am, edited 1 time in total.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Embedding Lua in Visual C++ 2008

Post by Falco Girgis »

Did you add -lualib5.1 or whatever your .lib is named to your link line?
User avatar
Cole S.
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 59
Joined: Sat Dec 06, 2008 2:49 pm

Re: Embedding Lua in Visual C++ 2008

Post by Cole S. »

What do you mean Link Line...
User avatar
Cole S.
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 59
Joined: Sat Dec 06, 2008 2:49 pm

Re: Embedding Lua in Visual C++ 2008

Post by Cole S. »

Nvm stupid question... but how would i add it... thorugh the project properties or global settings?
User avatar
Cole S.
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 59
Joined: Sat Dec 06, 2008 2:49 pm

Re: Embedding Lua in Visual C++ 2008

Post by Cole S. »

Wait... I don't have .lib files i have .obj files?????
User avatar
cypher1554R
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1124
Joined: Sun Jun 22, 2008 5:06 pm

Re: Embedding Lua in Visual C++ 2008

Post by cypher1554R »

Make sure you have the official library package. If you do have it, then check for .cpp or .c files that correspond with your .h files and include them in your project.

If you do find the .lib file then copy/move it to your compiler/platform folder into the "lib" directory. In the solution explorer right click on the project file->Properties->Configuration Properties->Linker->Input, and in the Additional Dependencies write down the lib's name (lualib.lib - for example)
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Embedding Lua in Visual C++ 2008

Post by Falco Girgis »

Just compile the .c and .h files into your own library and link against it.
Fear
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 9
Joined: Sun Nov 09, 2008 3:04 pm

Re: Embedding Lua in Visual C++ 2008

Post by Fear »

GyroVorbis wrote:Did you add -lualib5.1 or whatever your .lib is named to your link line?
I'm having the same problem of Cole S. and i added -lualib5.1 to the link line and i am still receving the same error. What's wrong?
User avatar
cypher1554R
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1124
Joined: Sun Jun 22, 2008 5:06 pm

Re: Embedding Lua in Visual C++ 2008

Post by cypher1554R »

Fear wrote:
GyroVorbis wrote:Did you add -lualib5.1 or whatever your .lib is named to your link line?
I'm having the same problem of Cole S. and i added -lualib5.1 to the link line and i am still receving the same error. What's wrong?
Did you move the lualib5.1.lib file to your compiler's lib directory? cause the compiler won't tell you if it's not there..
User avatar
Cole S.
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 59
Joined: Sat Dec 06, 2008 2:49 pm

Re: Embedding Lua in Visual C++ 2008

Post by Cole S. »

I have the source package... the one with nothing compiled, and I followed this guide to build it http://media.wiley.com/product_data/exc ... 069171.pdf , and I have .obj files for every c file... so should i just include everything into a project file and compile it like that, or do i have to build it another way to get a .lib file???
Fear
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 9
Joined: Sun Nov 09, 2008 3:04 pm

Re: Embedding Lua in Visual C++ 2008

Post by Fear »

cypher1554R wrote:
Fear wrote:
GyroVorbis wrote:Did you add -lualib5.1 or whatever your .lib is named to your link line?
I'm having the same problem of Cole S. and i added -lualib5.1 to the link line and i am still receving the same error. What's wrong?
Did you move the lualib5.1.lib file to your compiler's lib directory? cause the compiler won't tell you if it's not there..
Yes, i moved it to the compiler directory and the error doesn't go away
User avatar
Cole S.
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 59
Joined: Sat Dec 06, 2008 2:49 pm

Re: Embedding Lua in Visual C++ 2008

Post by Cole S. »

Okay I re-built it and found that i had lua5.1.4.lib and lua5.1.4-static the dependency thing worked. It compiles but when i run it it says "This application has failed to start because lua5.1.4.dll was not found. Re-installing the application may fix this problem." But i know my dll is fine... Do i copy it into the project file??
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Embedding Lua in Visual C++ 2008

Post by avansc »

yeas. or you can put it in System32 folder.

but that could be dagerouse.
just put it in the exe directory.

not that if you compile from vs2008 it does not run the program formt he debug/erlease forder/

you would have to put the dll in the project root. ie, where all the source is. thats where VS executes the program when you run it from the IDE.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Cole S.
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 59
Joined: Sat Dec 06, 2008 2:49 pm

Re: Embedding Lua in Visual C++ 2008

Post by Cole S. »

Where is the project root??
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Embedding Lua in Visual C++ 2008

Post by Falco Girgis »

Cole S. wrote:I have the source package... the one with nothing compiled, and I followed this guide to build it http://media.wiley.com/product_data/exc ... 069171.pdf , and I have .obj files for every c file... so should i just include everything into a project file and compile it like that, or do i have to build it another way to get a .lib file???
There's a special project setting called "Static Linked Library" which lets you compile it all to a library. The .o files are for if you were to include it directly into your project (which you shouldn't).
Post Reply