CMake include lua library

Answered

I'm trying to run example from https://www.lua.org/pil/24.1.html

cmake_minimum_required(VERSION 3.8)

project(test)

set(CMAKE_C_STANDARD 11)
set(SOURCE_FILES main.c)

include_directories(/usr/local/include)

add_executable(test ${SOURCE_FILES})


 

#include <stdio.h>
#include <string.h>

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

int main(void) {
puts("lua interpretor:");
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;
}

 

This is the error that I receive:

Undefined symbols for architecture x86_64:
"_luaL_loadbufferx", referenced from:
_main in main.c.o
"_luaL_newstate", referenced from:
_main in main.c.o
"_luaL_openlibs", referenced from:
_main in main.c.o
"_lua_close", referenced from:
_main in main.c.o
"_lua_pcallk", referenced from:
_main in main.c.o
"_lua_settop", referenced from:
_main in main.c.o
"_lua_tolstring", referenced from:
_main in main.c.o
ld: symbol(s) not found for architecture x86_64

 

However if I run it "by hands" it works! How to include Lua library with linker properly w/ CMake?

gcc -W -Wall -g -o main main.c -I/usr/local/include -L/usr/local/lib/lua/5.1 -llua
0
1 comment

Hi!

As I can see, you've already got the answer on stackoverflow. By the way, we have a quick CMake tutorial in our web help, it contains some tips, for example, about including libraries. Hope it will be helpful.

0

Please sign in to leave a comment.