I am trying to use Lua in one of my C++ projects, but I get errors:

**** Build of configuration Debug for project MacroLua ****

**** Internal Builder is used for build ****
g++ -LD:\Program Files\Lua\5.1\lib -oMacroLua.exe MainLuaTest.o
MainLuaTest.o: In function `main':
C:/Users/s/MacroLua/MacroLua/Debug/../MainLuaTest.cpp:11: undefined reference to `luaL_newstate'
C:/Users/s/MacroLua/MacroLua/Debug/../MainLuaTest.cpp:14: undefined reference to `luaL_openlibs'
C:/Users/s/MacroLua/MacroLua/Debug/../MainLuaTest.cpp:17: undefined reference to `luaL_loadfile'
C:/Users/s/MacroLua/MacroLua/Debug/../MainLuaTest.cpp:17: undefined reference to `lua_pcall'
C:/Users/s/MacroLua/MacroLua/Debug/../MainLuaTest.cpp:21: undefined reference to `lua_close'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 864 ms.

I just copied the code from another site:

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

int main()
{
    int s=0;

    lua_State *L = lua_open();

    // load the libs
    luaL_openlibs(L);

    //run a Lua scrip here
    luaL_dofile(L,"foo.lua");

    printf("\nI am done with Lua in C++.\n");

    lua_close(L);

    return 0;
}

I have Lua for Windows installed... What is the problem? :?:

Thanks in advance,
Nick Guletskii.

Recommended Answers

All 4 Replies

> g++ -LD:\Program Files\Lua\5.1\lib -oMacroLua.exe MainLuaTest.o
You told the linker where to look, with the -L option.

But you also need to say which library you want, with the -l option (that's lower case L).

If the library is liblua.a for example, you would add -llua to the command line.

Now:

**** Build of configuration Debug for project MacroLua ****

**** Internal Builder is used for build ****
g++ -LD:\Program Files\Lua\5.1\lib -oMacroLua.exe MainLuaTest.o -llua5.1.lib -llua51.lib
D:\Program Files\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot find -llua5.1.lib
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 548 ms.

Do I remove the extension?

Thanks, it works now! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.