#### Archived

This topic is now archived and is closed to further replies.

# Lua article

This topic is 5370 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I just started reading the article on embedding Lua into games. However, in the article, the author does not show how to create a static library from the Lua source. Does anyone know how I can create the static library with Visual C++ 6 or Dev-c++(I use dev-cpp at school because we use a non-ansi 99 compliant compiler, Borland C++ 5.02 - NOT Builder). I am asking for this, because I want to know how I create a static library for myself, and it gives me the feeling I learn more then just using someone else''s stuff(I don''t bother using other peoples stuff, but I want to know about the static lib). Also, how do I use it? In VC++ 6 I just add it to the workspace or #pragma comment(lib, "X:\\lua.lib")? Toolmaker
-Earth is 98% full. Please delete anybody you can.

##### Share on other sites
It''s fairly simple to build Lua as a static library. Make a static library project, add in all the .cpp files in lua/src and lua/src/lib, and add in all the headers in lua/src, lua/include, and lua/src/lib.

Building luac with the static library is a bit trickier. Hint: You can set the project options to predefine the preprocessor symbol "LUA_OPCODES".

##### Share on other sites
quote:
Original post by Sneftel
Building luac with the static library is a bit trickier. Hint: You can set the project options to predefine the preprocessor symbol "LUA_OPCODES".

Uhm, I don''t think I understand that one. Do you mean, to include lua as static lib(The one I built), I need to include the .lib file AND #define LUA_OPCODES? In that case, I would just make a second header looking like this:

  #pragma comment(lib, "mylualib.lib")#include <lua.h>...#define LUA_OPCODES

-Earth is 98% full. Please delete anybody you can.

##### Share on other sites
LUA_OPCODES only needs to be defined when you''re building the lua compiler, it isn''t needed for normal use.

Creating a static library for Lua with DevC++ is quite easy:
1) Make a new project, with the static library template, set to C not C++, and add all the lua files from the src folder of the windows source distribution - this doesn''t include the files in the sub-folders, which are for the lua interpretter, the lua compiler and the lua standard libraries.
2) Make sure you''ve copied the standard lua include files (lua.h, lauxlib.h and lualib.h) to your compiler''s include directory, these files are needed to use lua, and to build the static library.
3) Turn on optimisation (assuming you want it) and build the static library.
4) Copy it to your compiler''s library directory, and rename it to liblua.a
5) Build the lua standard libraries in the same way (the source files for them are in src/lib of the lua source distribution.

Now, in order to use Lua in C++ programs, it will be necessary to slightly alter lua.h to prevent the C++ function name mangling from preventing correct linking.
What you need to do is add:
#ifdef __cplusplus
extern "C" {
#endif
to a point between the standard #includes and the beginning of the code.
#ifdef __cplusplus
}
#endif
to the bottom of lua.h

You should do this to lualib.h as well.

Unfortunately, being poor and unwilling to steal, I don''t have a copy of VC++, so I can''t help you with that bit.

John B

##### Share on other sites
Find the Lua users site from the links on Lua''s website. You may be able to find a pre-built Lua library there. I may be wrong though.

##### Share on other sites
Ok, I have been trying to build the compiler. I get some error code about opcodes. What do I do now? How do I fix it so I can compile my luac.exe.

EDIT: Never mind, I got the luac.exe to compile. I read some documentation and found out how to compile it ^_^.

Toolmaker

-Earth is 98% full. Please delete anybody you can.

[edited by - toolmaker on May 8, 2003 7:54:04 PM]