So, I’m relatively new to C++ and I’ve been trying to run Lua files in my C++ project. To start off things, I ran this simple code:
#include <iostream> #include <lua.h> #include <lualib.h> #include <lauxlib.h> #include <stdlib.h> #include <stdio.h> int main() { std::cout << "Lua Console"; lua_State *L; L = luaL_newstate(); char ch; std::cin >> ch; return 0; }
And the terminal command looks like this:
g++ main.cpp -L lib/liblua.a
Edit: Corrected to this
g++ main.cpp -Llib -llua
Lua libraries are installed on my system (Linux), but the linker can’t find the definition for luaL_newstate(), despite the fact that I included the library in the command line:
main.cpp:(.text+0x2b): undefined reference to `luaL_newstate()'
And that happens with any Lua function. I don’t really know what went wrong, importing libraries in Linux was pretty straightforward up until now. This seems to be a problem with me not familiar with using libraries in my projects, but I couldn’t find very useful documentation. Thank you in advance.
Advertisement
Answer
-L
option designates a folder where library files are kept; try g++ main.cpp -Llib -llua
Also wrap #include
for Lua headers into extern "C"
:
extern "C" { #include <lualib.h> #include <lauxlib.h> #include <lua.h> }