0

I'm attempting to execute a Lua script out of a C++ program tha I want to build with CMake. While I can import lua.h, I get a linker error as soon as I try to compile my code when it contains the call of a lua_State. This is my C++ code:

#include <iostream>

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

int main(int argc, char* argv[]) {

    lua_State* L = luaL_newstate();

    // luaL_openlibs(L);
    // luaL_dofile(L, "scripts/lol.lua");


    lua_close(L);

    return 0;
}

My CMakeLists.txt is

cmake_minimum_required(VERSION 3.16 FATAL_ERROR)

project(Lua_cpp)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(MARCH "-march=broadwell") # set(MARCH "-march=native")
set(MTUNE "-mtune=broadwell") # set(MTUNE "-mtune=native")

include_directories(${PROJECT_SOURCE_DIR}/include)

find_package(Lua REQUIRED)
include_directories(/usr/local/include ${LUA_INCLUDE_DIR})

add_executable(execute main.cc)

target_link_libraries(execute ${LUA_LIBRARIES})

Upon build I receive the following message:

/usr/local/Cellar/cmake/3.25.1/bin/cmake -S/Users/leo/coding/Lua/Lua_cpp -B/Users/leo/coding/Lua/Lua_cpp/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.25.1/bin/cmake -E cmake_progress_start /Users/leo/coding/Lua/Lua_cpp/build/CMakeFiles /Users/leo/coding/Lua/Lua_cpp/build//CMakeFiles/progress.marks
/Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/Makefile2 all
/Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/execute.dir/build.make CMakeFiles/execute.dir/depend
cd /Users/leo/coding/Lua/Lua_cpp/build && /usr/local/Cellar/cmake/3.25.1/bin/cmake -E cmake_depends "Unix Makefiles" /Users/leo/coding/Lua/Lua_cpp /Users/leo/coding/Lua/Lua_cpp /Users/leo/coding/Lua/Lua_cpp/build /Users/leo/coding/Lua/Lua_cpp/build /Users/leo/coding/Lua/Lua_cpp/build/CMakeFiles/execute.dir/DependInfo.cmake --color=
/Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/execute.dir/build.make CMakeFiles/execute.dir/build
[ 50%] Building CXX object CMakeFiles/execute.dir/main.cc.o
/usr/bin/g++  -I/Users/leo/coding/Lua/Lua_cpp/include -I/usr/local/include -I/usr/local/include/lua -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk -mmacosx-version-min=11.6 -MD -MT CMakeFiles/execute.dir/main.cc.o -MF CMakeFiles/execute.dir/main.cc.o.d -o CMakeFiles/execute.dir/main.cc.o -c /Users/leo/coding/Lua/Lua_cpp/main.cc
[100%] Linking CXX executable execute
/usr/local/Cellar/cmake/3.25.1/bin/cmake -E cmake_link_script CMakeFiles/execute.dir/link.txt --verbose=1
/usr/bin/g++ -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk -mmacosx-version-min=11.6 -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/execute.dir/main.cc.o -o execute  /usr/local/lib/liblua5.4.dylib
Undefined symbols for architecture x86_64:
  "luaL_newstate()", referenced from:
      _main in main.cc.o
  "lua_close(lua_State*)", referenced from:
      _main in main.cc.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [execute] Error 1
make[1]: *** [CMakeFiles/execute.dir/all] Error 2
make: *** [all] Error 2

I find this confusing as the header "lua.h" is found - otherwise main.cc could not have been built. Also the cmake command returns -- Found Lua: /usr/local/lib/liblua5.4.dylib (found version "5.4.4"). I suspect that it is this lib-file that is referred to here that can - unlike the rest of the Lua source code like lua.h - not be properly linked to the executeable but I can't validate that or find out why that is.

I have tried to have CMake download the Lua source code like this and I receive exactly the same linker error.

  • 1
    In C++ code you need to wrap inclusion of Lua headers with `extern "C"`. – Tsyvarev Feb 19 '23 at 18:13
  • An alternative method, which I actually recommend, is to *compile Lua source as C++* instead of C. This is officially supported. This has the advantage that Lua will then use C++ exceptions instead of `longjmp` to handle errors. This mode better plays together with C++ hosting application. – prapin Feb 20 '23 at 20:16

0 Answers0