5

I have the following trivial Lua program which I copied from the book Programming In Lua

#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main (void) 
{
    char buff[256];
    int error;
    lua_State *L = luaL_newstate(); /* opens Lua */
    luaL_openlibs(L); /* opens the standard libraries */
    while (fgets(buff, sizeof(buff), stdin) != NULL) 
    {
        error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
        lua_pcall(L, 0, 0, 0);

        if (error) 
        {
            fprintf(stderr, "%s", lua_tostring(L, -1));
            lua_pop(L, 1); /* pop error message from the stack */
        }
    }
    lua_close(L);
    return 0;
}

my environment is cywin

my make file looks like this:

CC=gcc
INCLUDE='-I/home/xyz/c_drive/Program Files/Lua/5.1/include'
LINKFLAGS='-L/home/xyz/c_drive/Program Files/Lua/5.1/lib' -llua51 

li.o:li.c
    $(CC) $(INCLUDE)   -c li.c

main:li.o
    $(CC)  -o main  $(LINKFLAGS) li.o 

clean:
    rm *.o
    rm main

My /home/xyz/c_drive/Program Files/Lua/5.1/lib directory contains lua5.1.dll lua5.1.lib lua51.dll and lua51.lib

Trying to build my main target I am getting the following errors:

li.o:li.c:(.text+0x35): undefined reference to `_luaL_newstate'
li.o:li.c:(.text+0x49): undefined reference to `_luaL_openlibs'
li.o:li.c:(.text+0xaf): undefined reference to `_luaL_loadbuffer'
li.o:li.c:(.text+0xd9): undefined reference to `_lua_pcall'
li.o:li.c:(.text+0x120): undefined reference to `_lua_tolstring'
li.o:li.c:(.text+0x154): undefined reference to `_lua_settop'
li.o:li.c:(.text+0x167): undefined reference to `_lua_close'

Any ideas about what I might be doing wrong here?

JohnP
  • 677
  • 1
  • 9
  • 18
  • How did you build the lua51.lib? If the lib was compiled with C++ compiler then your program should also be compiled (and linked) with C++ compiler. – torus Nov 11 '10 at 00:35
  • I downloaded the bin version from lua's web site... The compiler I use is gcc (not g++)..... I will try to download the source code and compile it – JohnP Nov 11 '10 at 01:53
  • There are several different binary versions there, built with different compilers. What specific package did you get? – Mud Nov 11 '10 at 03:14
  • Originally I got the 5.1 windows binary.. When I switched to the source code and compile it all my problems disappeared and now I am on my way to become a .... lua .... expert (LOL) – JohnP Nov 12 '10 at 15:47

2 Answers2

9

The problem is that you have named the libraries on the link command line before the object files that require them. The linker loads modules from left to right on the command line. At the point on the line where you name -llua51, no undefined symbols that could be satisfied by that library are known. Then you name li.o, which does have unknown symbols.

Some Unix-like environments don't treat this as an error because part of the link process is deferred to the program load when reference to .so files are satisfied. But Cygwin, MinGW, and Windows in general must treat this as an error because DLLs work quite differently from .so files.

The solution is to put -llua51 after all the .o files on your link line.

Edit: Incidentally, it appears you are linking against the Lua for Windows distribution, but building with GCC under Cygwin. You will want to use Dependency Walker to make sure that your program does not depend on the Cygwin runtime, and that it does depend on the same C runtime as the lua51.dll from Lua for Windows. IIRC, that will be the runtime for the previous version of Visual Studio. It is possible to make GCC link against that, but you will need to be using the MinGW port (which you can use from Cygwin), and link against a couple of specific libraries to get that version. I'm away from my usual PC, or I'd quote an exact link line. (I believe you need -lmoldname -lmsvcr80 or something like that, as the last items on the link line.)

It will cause mysterious and very hard to diagnose problems if more than one C runtime library is in use. The easy answer is to use the same one as your preferred Lua DLL. Another alternative is that the Lua Binaries project has pre-compiled Lua DLLs for a wide array of C toolchains on Windows. If you need a Lua application that understands the Cygwin environment, you will want one that is built by GCC for Cygwin and not the Lua for Windows flavor. Lua Binaries will be your friend, or you can build Lua your self from source.

RBerteig
  • 41,948
  • 7
  • 88
  • 128
  • Thanks for the help... Yes, adding the -llua51 in the end of the link line solves the compilation issue. Because I still had some issues running the executable (permission denied) I just downloaded lua's source code and build it using make mingw. This seems to fix the problem... – JohnP Nov 11 '10 at 15:51
0

The names in the Lua API do not have those leading underscores. Try compiling with -fno-leading-underscore.

Mud
  • 28,277
  • 11
  • 59
  • 92
  • The leading underscores are part of the normal C name mangling, and are not the issue here. If they appear to be the issue, then it is likely you have the Lua DLL for Windows compiled for use with the wrong C compiler toolchain. – RBerteig Nov 11 '10 at 06:21