使用CMake链接Lua与C++程序时遇链接错误,求解决方案
问题
我正在尝试构建一个通过CMake编译、可执行Lua脚本的C++程序。虽然成功导入lua.h,但当代码中调用lua_State相关函数时,编译阶段出现链接错误。
C++代码
#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; }
CMakeLists.txt内容
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})
构建错误信息
/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
CMake已找到Lua库:-- Found Lua: /usr/local/lib/liblua5.4.dylib (found version "5.4.4"),尝试过自动下载Lua源码构建,仍出现相同链接错误。
解决方案
1. 修复C++与C库的链接兼容性
C编译器会对函数名进行名称修饰(name mangling),而Lua是C语言编写的库,未做C兼容声明,导致链接时找不到对应符号。修改C++代码,用extern "C"包裹Lua头文件:
#include <iostream> extern "C" { #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; }
2. 优化CMake配置(可选但推荐)
改用现代CMake的目标级配置,替代全局包含和链接方式,提升兼容性:
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") # 添加编译选项 add_compile_options(${MARCH} ${MTUNE}) find_package(Lua REQUIRED) add_executable(execute main.cc) # 目标级别包含目录 target_include_directories(execute PRIVATE ${PROJECT_SOURCE_DIR}/include ${LUA_INCLUDE_DIR} ) # 使用官方推荐的目标链接 target_link_libraries(execute PRIVATE Lua::Lua)
3. 验证库架构匹配(排查步骤)
如果上述方法无效,检查Lua库的架构是否与编译目标一致:
lipo -info /usr/local/lib/liblua5.4.dylib
确保输出包含x86_64,若库为arm64而编译环境是x86_64,需重新安装对应架构的Lua库。
内容的提问来源于stack exchange,提问作者radioactiveman
相关产品推荐
相关产品推荐

