lua/c

trebsirk

Registered
Hello. My first post here. I am using OS X 10.5.6. I am trying to compile a simple C program in Terminal using g++.

//File: a.c
#include <stdio.h>
extern "C"
{
#include <lua.h>
}

int main(int argc, char* argv[ ])
{
lua_State* luaVM = lua_open(0);

if (NULL == luaVM)
{
printf("Error Initializing lua\n");
return -1;
}

return 0;
}

I type the line "g++ a.c"
Compiler errors abound because g++ cannot find lua.h, which resides in /opt/local/include. My PATH is set to the following:
/opt/local/bin:/opt/local/include:/stow/bin:/stow/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

lua.h is in the second entry of PATH, but g++ cannot find it. If anyone can shed some light on this, I would be much appreciative.

-Kris
 
Last edited:
Hi Kris -

You need to tell the compiler where the header file resides (the compiler doesn't look in your PATH).

Code:
g++ -I/opt/local/include a.c

In addition you'll need to link against the lua library (liblua.a) so you need to provide the library name minus the "lib" prefix and the ".a" extension (-llua) and you also provide the directory where the library resides. So the final command line looks like this:

Code:
g++ -I/opt/local/include a.c -L/opt/local/lib -llua
 
Back
Top