1、简介
Premake是一个命令行实用程序,它读取软件项目的脚本定义,最常见的是使用它为Visual Studio,Xcode或GNU Make等工具集生成项目文件。
2、测试
2.1 测试1:入门例子
- 新建文件夹test001:
mkdir test001
cd test001
- 新建代码文件hello.c:
/* hello.c */
#include <stdio.h>
int main(void) {
puts("Hello, world! 爱看书的小沐!");
return 0;
}
- 新建构建脚本文件premake5.lua:
-- premake5.lua
workspace "XiaoMuProject"
configurations { "Debug", "Release" }
project "XiaoMu001"
kind "ConsoleApp"
language "C"
targetdir "bin/%{cfg.buildcfg}"
files { "**.h", "**.c" }
filter "configurations:Debug"
defines { "DEBUG" }
symbols "On"
filter "configurations:Release"
defines { "NDEBUG" }
optimize "On"
- 执行构建命令如下:
# premake5 --file=MyProjectScript.lua vs2013
premake5 vs2017
结果如下: 用vs2017打开上面生成的工程文件:
2.2 测试2:入门例子2
- premake5.lua
workspace "XiaMuTest002" -- 解决方案
startproject "Test" -- 开始项目
configurations
{
"Debug",
"Release"
}
platforms
{
"Win32",
"Win64"
}
filter "platforms:Win32"
system "Windows"
architecture "x32"
filter "platforms:Win64"
system "Windows"
architecture "x86_64"
outputdir = "%{cfg.platform}/%{cfg.buildcfg}/%{prj.name}"
project "XiaMuTest002"
kind "ConsoleApp"
language "C++"
files
{
"./**.cpp",
"*.c"
}
targetdir("../bin/" .. outputdir)
objdir("../obj/" .. outputdir)
2.3 测试3:glfw例子
2.3.1 准备第三方库glfw
https://github.com/glfw/glfw/releases 下载完毕之后,解压到文件夹如下:
2.3.2 新建封装库项目ExampleDll
- ExampleDll.h
#ifndef EXAMPLE_DLL_HPP
#define EXAMPLE_DLL_HPP 1
#include <string>
#include <memory>
struct GLFWwindow;
namespace ExDLL
{
class _declspec(dllexport) Window
{
public:
Window(int width, int height, const std::string& title);
~Window();
bool shouldClose() const noexcept;
void pollEvents() const noexcept;
void swapBuffers() const noexcept;
std::pair<int, int> getWindowSize() const noexcept;
private:
GLFWwindow* wnd;
};
}
#endif
- ExampleDll.cpp
#include "ExampleDll.h"
#include <GLFW/glfw3.h>
namespace ExDLL
{
Window::Window(int width, int height, const std::string& title)
{
glfwInit();
wnd = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
glfwMakeContextCurrent(wnd);
}
Window::~Window()
{
glfwDestroyWindow(wnd);
glfwTerminate();
}
bool Window::shouldClose() const noexcept
{
return glfwWindowShouldClose(wnd) != 0;
}
void Window::pollEvents() const noexcept
{
glfwPollEvents();
}
void Window::swapBuffers() const noexcept
{
glfwSwapBuffers(wnd);
}
std::pair<int, int> Window::getWindowSize() const noexcept
{
std::pair<int, int> sz{};
glfwGetWindowSize(wnd, &sz.first, &sz.second);
return sz;
}
}
2.3.3 新建测试项目ExampleTest
- main.cpp
#include <ExampleDll.h>
#if defined _WIN32
#include <Windows.h>
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
#endif
#include <gl/GL.h>
//导入ExampleDll中的Window类
class _declspec(dllimport) ExDLL::Window;
int main()
{
ExDLL::Window window{ 1000, 600, "Hello World! 爱看书的小沐,2023" };
while (!window.shouldClose())
{
// 事件更新
window.pollEvents();
// 绘图
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.5f, -0.5f);
glColor3f(1.0, 1.0, 0.0);
glVertex2f(0.5f, -0.5f);
glColor3f(1.0, 0.0, 1.0);
glVertex2f(0, 0.5f);
glEnd();
// 渲染更新
window.swapBuffers();
}
return 0;
}
2.3.4 新建构建脚本
- premake5.lua
workspace "XiaoMuTest003"
startproject "ExampleTest" -- 开始项目
location "vs"
language "C++"
architecture "x64"
configurations {"Debug","Release"}
filter {"configurations:Debug"}
symbols "On"
filter {"configurations:Release"}
optimize "On"
-- 重置过滤器的其他设定
filter {}
targetdir ("build/target/%{prj.name}/%{cfg.longname}")
objdir ("build/obj/%{prj.name}/%{cfg.longname}")
postbuildcommands{
("{COPY} %{cfg.buildtarget.relpath} \"../bin/\"")
}
-- 定义函数,包含glfw三方库头文件,可被其他工程调用
function includeGLFW()
includedirs "../3rd/glfw-3.3.8.bin.WIN64/include"
end
-- 定义函数,链接glfw三方库
function linkGLFW()
libdirs "../3rd/glfw-3.3.8.bin.WIN64/lib-vc2017"
links "glfw3dll"
end
-- ExampleDll项目
project "ExampleDll"
kind "SharedLib"
files "src/ExampleDll/**"
includeGLFW()
linkGLFW()
-- 定义函数,链接ExampleDll动态库
function useExampleDLL()
includedirs "src/ExampleDll"
links "ExampleDll"
end
-- App应用程序
project "ExampleTest"
kind "ConsoleApp"
files "src/ExampleTest/**"
useExampleDLL()
filter "system:windows"
links {"OpenGL32"}
2.3.5 执行构建命令
最后构建的文件夹和里面存放的文件组织如下:
premake5 vs2017
vs2017打开生成的工程文件如下: 编译和运行后:
结语
如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;
╮( ̄▽ ̄)╭
如果您感觉方法或代码不咋地
//(ㄒoㄒ)//,就在评论处留言,作者继续改进;
o_O???
如果您需要相关功能的代码定制化开发,可以留言私信作者;
(✿◡‿◡)
感谢各位大佬童鞋们的支持!
( ´ ▽´ )ノ ( ´ ▽´)っ!!!