嵌入式Lua 5.4.2链接错误 [英] Embedded Lua 5.4.2 Link Errors
问题描述
我试图通过CLion IDE和CMake使用嵌入在C中的Lua,但是遇到链接器错误.
I'm trying to use Lua embedded in C using the CLion IDE and CMake, but I'm running into linker errors.
运行该程序时,出现未定义符号:_luaL_newstate
:
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main()
{
lua_State *L = luaL_newstate();
return 0;
}
这是我的CMake文件,它使用CMake在FindLua.cmake中内置的文件包.我正在使用CMake选项 -DCMAKE_CXX_FLAGS =-Wall -Wextra"-DCMAKE_C_FLAGS =-墙-Wextra"
.
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"
.
由 CMake 生成的失败的链接器命令是 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
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})
Lua目录包含lua54.dll和lua54.lib文件,以及包含所有标头的include目录.
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!
推荐答案
可以编译吗?...
(wait.c)
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;
}
在Linux上,Makefile如下:
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...
-
require('wait')
...或... -
package.loadlib('wait.so','wait')()
... *.so是您当然需要的linux库:* .dll
(我在Windows上没有C的IDE ;-))
这篇关于嵌入式Lua 5.4.2链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!