CMake构建Lua的简单C-API项目

CMake版本: 3.16

Lua版本: 5.1

系统: Win10

一个简单的C-API程序, 建立一个lua虚拟机, 并关闭虚拟机.

 1 #include <iostream>
 2 #include "lua.hpp"  // 注意这里, 因为使用C++编译器, 所以包含lua.hpp, 这样就不需要自己写extern "C" 语句.
 3 
 4 int main(void)
 5 {
 6    lua_State *L = luaL_newstate();  // 新建一个Lua虚拟机
 7    luaL_openlibs(L);  // 打开库
 8 
 9   lua_close(L);  // 关闭虚拟机
10   return 0;   
11 }

编写CMakeLists.txt

 1 cmake_minimum_required(VERESION 3.10)
 2 
 3 project(Demo)
 4 
 5 # find lua 5.1
 6 find_package(Lua51)
 7 
 8 set(lua_src main.cpp)
 9 
10 include_libraries(${LUA_INCLUDE_DIR})
11 
12 add_executable(LuaDemo ${lua_src})
13 
14 target_link_libraries(LuaDemo ${LUA_LIBRARIES})

命令行进行CMake构建, 源代码目录为当前目录, 构建目录为build

>> cmake -G "Visual Studio 14 2015" -S ./ -B ./build

使用VS2015进行编译

>>devenv Demo.sln /Build "Debug|win32"

      ----------------------- 勿在浮沙筑高台

猜你喜欢

转载自www.cnblogs.com/068XS228/p/12161434.html