1

I'm trying to use Lua embedded in C using the CLion IDE and CMake, but I'm running into linker errors.

When running this program, I get undefined symbol: _luaL_newstate:

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

int main()
{
    lua_State *L = luaL_newstate();
    return 0;
}

This is my CMake file, it uses CMake's built in FindLua.cmake to find the package. I'm using the CMake options -DCMAKE_CXX_FLAGS="-Wall -Wextra" -DCMAKE_C_FLAGS="-Wall -Wextra".

Edit: The linker command generated by CMake that's failing is lld-link.exe /nologo @CMakeFiles\LuaProjk.dir\objects1.rsp /out:C:\Users\b\CLionProjects\LuaProjk\bin\LuaProjk.exe /implib:LuaProjk.lib /pdb:C:\Users\b\CLionProjects\LuaProjk\bin\LuaProjk.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console C:\Lua\lua54.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\LuaProjk.dir/intermediate.manifest CMakeFiles\LuaProjk.dir/manifest.res

cmake_minimum_required(VERSION 3.17)
project(LuaProjk C)

set(LUA_DIR /Lua)
set(LUA_INCLUDE_DIR /Lua/include)
find_package(Lua REQUIRED)
include_directories(${LUA_INCLUDE_DIR})

set(CMAKE_C_STANDARD 11)
set(SOURCE_FILES main.c)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ../../bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ../../lib)

add_executable(LuaProjk ${SOURCE_FILES})
target_link_libraries(LuaProjk ${LUA_LIBRARIES})

The Lua directory contains the lua54.dll and lua54.lib files, as well as the include directory with all the headers.

Am I missing something? Thanks for any help!

OCDkirby
  • 190
  • 9
  • Include [lua.hpp](https://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-870.1.10/tools/hidScript/lua/src/lua.hpp.auto.html) instead – Egor Skriptunoff Apr 15 '21 at 17:34
  • @EgorSkriptunoff this is entirely in C11, and C does not recognize `extern "C"` statements. The .hpp naming convention is meant to specify that the header is only for C++ programs, and likely doesn't compile in C. – OCDkirby Apr 15 '21 at 18:07
  • Would you mind to [edit] your question and post the executed linker command? -- Oh, and does `-DCMAKE_CXX_FLAGS` work for C builds? I mean, "CXX" commonly denotes C++, but my Cmake experiences are quite low. – the busybee Apr 15 '21 at 18:57
  • @thebusybee I wouldn't know about the option actually (new to C but not C++), but I will post the full linker command. Thanks! – OCDkirby Apr 15 '21 at 19:31
  • Are you trying to compile a 32-bit or 64-bit program? Do your Lua libraries match this? – Joseph Sible-Reinstate Monica Apr 15 '21 at 23:50
  • @JosephSible-ReinstateMonica 64-bit debug VS2019 profile, with 64-bit Lua binaries compiled on vc16 (VS2019) toolchain as well. EDIT: Switching my target from amd64_x86 to amd64 solved the issue! Thank you, please add an answer so I can mark it as correct! – OCDkirby Apr 15 '21 at 23:57

1 Answers1

0

Can you compile this? ...
(wait.c)

/* This is a wait function for Lua
   It uses unistd.h > usleep()
   so wait(1000000) is a second
*/
#include <unistd.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

static int wait_c(lua_State *L){
 long msecs=lua_tointeger(L,-1);
 usleep(msecs);
 return 0;
}

int wait(lua_State *L){
 lua_register(L,"wait",wait_c);  
 return 0;
}

On linux the Makefile looks like:

all: wait.so

wait.so: wait.c
    gcc -shared -fPIC -o wait.so wait.c

clean:
    rm -v wait.so

...it can be loaded in two ways...

  1. require('wait')
    ...or...
  2. package.loadlib('wait.so','wait')()
    ...the *.so is a linux library you need of course: *.dll
    ( I dont have an IDE for C on Windows ;-) )
koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15
  • Nope, all of the Lua symbols are undefined. – OCDkirby Apr 15 '21 at 20:37
  • Argh yes - You need for windows: https://stackoverflow.com/questions/3379139/sleep-function-in-windows-using-c – koyaanisqatsi Apr 15 '21 at 21:15
  • 1
    I already replaced that part with its windows equivalent because it's not part of the problem at hand, the program still does not compile due to its use of undefined Lua symbols (which is a problem at link-time, as the headers work perfectly). – OCDkirby Apr 15 '21 at 21:54