[IDE]VSCode+Premake5打造(c++)跨平台开发调试工具(4): Premake5

不想看废话的话,直接上github:https://github.com/zhangping312/VSCode-Premake5-Example

    在正式引用第3方库SFML之前,先开始premake5,因为现在cpp文件只有一个Main.cpp,还好管理。现在我们就按照正常开发C++的步骤来。PS:CMake是不会去学的,打死都不会的。
    现在Source目录下创建新的文件夹Engine,创建一个Application.h,和Application.cpp文件。
    Application.h:

#include <iostream>
#include <exception>

class Application
{
public:
    Application(const std::string& title, unsigned int width, unsigned int height);
    Application(const Application& other) = delete;
    Application& operator=(const Application& other) = delete;
    virtual ~Application();

    void Run();
};

    Application.cpp:

#include "Application.h"

Application::Application(const std::string& title, unsigned int width, unsigned int height)
{
    std::cout << "Application constructor!" << std::endl;
}

Application::~Application()
{
    std::cout << "Application destructor!" << std::endl;
}

void Application::Run()
{
    std::cout << "Application run!" << std::endl;
}

    接着修改Main.cpp文件

#include "Engine/Application.h"

#define APP_NAME "SFML"
#define APP_WIDTH 1280
#define APP_HEIGHT 720

int main(int args, char* argv[])
{
    try
    {
        Application app(APP_NAME, APP_WIDTH, APP_HEIGHT);
        app.Run();
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }

    return 0;
}

    基础的框架搭好了,也有1个h文件,2个cpp文件了,相比之前的Main.cpp,已经很复杂了。是时候轮到premake5大显身手了,在Game目录下创建premake5.lua文件, 再接着在Game/SFML目录下创建premake5.lua文件,一个是工程的,一个是项目的。不然后期项目多了,全部写一个premake5.lua文件中,就有点庞大了。
    Game/premake5.lua:

-- Custom variables
g_WorkspaceFolder = os.getcwd()
g_BuildFolder = g_WorkspaceFolder .. "/../build"
print("g_WorkspaceFolder: " .. g_WorkspaceFolder)
print("g_BuildFolder: " .. g_BuildFolder)

-- Workspace configuration:
workspace "Game"
    -- Place where build files will be generated
    location "../build"
    -- Available configurations and platforms
    configurations { "Debug", "Release" }
    filter "system:windows"
        platforms { "x86", "x64" }
    filter "system:not windows"
        platforms { "x64" }

    filter "system:windows"
        systemversion "latest"
        
    -- Setup platforms
    filter "platforms:x86"
        architecture "x86"
    filter "platforms:x64"
        architecture "x86_64"
    
    -- Setup configurations
    filter "configurations:Debug"
        symbols "On"
    filter "configurations:Release"
        optimize "On"

    -- Add projects
    include("SFML/premake5.lua")

    我看了下,没啥好说的,单词都蛮好懂的,关注2处吧,第一次是location这里,这边是将premake5生成的工程文件放在../build文件夹中,就是跟Game文件夹同级,这目录架构在第2篇就已将讲过了,接下来就看项目的设置。
    Game/SFML/premake5.lua:

project "SFML"
    kind "ConsoleApp"
    language "C++"
    cppdialect "C++17"
    
    -- Place where build files will be generated
    location (g_BuildFolder .. "/%{prj.name}")
    -- Place where compiled binary target
    targetdir (g_WorkspaceFolder .. "/../bin/%{prj.name}")
    -- Place where object and other intermediate files
    objdir (g_WorkspaceFolder .. "/../bin-int/%{prj.name}")

    -- Specify script files for the project
    files
    {
        g_WorkspaceFolder .. "/%{prj.name}/Source/**.h",
        g_WorkspaceFolder .. "/%{prj.name}/Source/**.cpp",
    }

    -- Window configuration
    filter "system:windows"
        includedirs { g_WorkspaceFolder .. "/%{prj.name}/Source/" }-- Specify the include file search path

    -- Mac configuration
    filter "system:macosx"
    systemversion "10.14"   -- 我的Mac版本是10.14.5,低于development target的10.15, 为了避免手动修改,改成10.14
        includedirs     { g_WorkspaceFolder .. "/%{prj.name}/Source/" }--Xcode: User Header Search Paths

    接着在Game目录创建GenerateProjects.bat和GenerateProjects.sh文件。 
    GenerateProjects.bat

@echo off
call premake5 vs2019
pause

    GenerateProjects.sh

#!/bin/bash
set +v
premake5 xcode4

    Window上双击GenerateProjects.bat就可以生成.sln文件,直接用VS2019打开编译运行就OK,Mac上就需要修改GenerateProjects.sh权限,然后在终端上运行就可以生成XCode工程文件,再双击打开XCode工程,编译运行就OK。
    premake5生成VS2019工程和Xcode工程都没有任何问题,是时候加入到VSCode中来,修改tasks.json和launch.json文件了。先将name都修改一下,将Windows的都改成VS2019,Mac的都改成XCode,tasks.json中再添加2个task。

        /
        // # Project files generation(premake5)
        /

        // ## VS2019
        {
            "label": "Generate build files (VS2019)",
            "type": "shell",
            "group": "none",
            "command": "premake5 vs2019"
        },     
        
        // ## XCode
        {
           "label": "Generate build files (XCode)",
           "type": "shell",
           "group": "none",
           "command": "premake5 xcode4"
        },

    好了,现在我们可以在VSCode上通过Task: Run Task来生成vs2019工程和XCode工程了,只需要再加一点点,我们就可以编译链接生成可执行程序就了。还是继续修改tasks.json文件,在之前的Build (VS2019)(之前是Windows)和Build (XCode)(之前是Mac)上进行修改,改动其实不大,个人觉得。最大的区别也就是将cl.exe改成了msbuild,clang++改成了xcodebuild,如果之前也用过,估计应该没什么难度,但之前没学习过,那就不好意思了,xcodebuild我就没学习过,google了很久,踩的坑简直是一愣一愣的,后面还会再讲。现在已经可以通过VSCode生成VS2019或者XCode工程,并生成可执行程序,也可以调试。
    tasks.json文件改动比较大,就全部贴出来:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "dependsOn":[           // 任务依赖,在执行该任务前,需要先执行依赖任务
                "Create Bin folder"
            ],
            "label": "First task",  // 任务名称
            "type": "shell",        // 任务类型
            "group": "none",        // 组, 只有3个选项"build", "test", "none", 感觉功能就是用来简化选择用的
                                    //  build组: 通过Tasks: Run Build Task选择, 也可以通过Tasks: Run Task选择
                                    //  test组:  通过Tasks: Run Test Task选择, 也可以通过Tasks: Run Task选择
                                    //  none组:  通过Tasks: Run Task选择,就是显示全部任务
            "command": "echo",      // 命令
            "args": [               // 命令参数
                "Hello, World!"
            ]
        },

        /
		// # Creating
        /

        // ## Mac need to create bin folder
        {
            "label": "Create Bin folder",
            "type": "shell",
            "group": "none",
            "windows":{             // Window命令
                "command": "mkdir",
                "args": [
                    "-p",
                    "bin/${input:buildProject}",
                    ";", // PowerShell command, 不能用"&&"
                    "echo",
                    "Create BIN folder."
                ],
            },
			"osx": {                // Mac命令
                "command": "mkdir",
                "args": [
                    "-p",
                    "bin/${input:buildProject}",
                    "&&", 
                    "echo",
                    "Create BIN folder."
                ]
            },
            "options": {
                "cwd": "${workspaceFolder}/../"     // 指定当前的工作文件夹
            },
        },

        /
		// # Cleaning
        /

        // ## Clean build files
		{
            "label": "Clean Build folder",
            "type": "shell",
            "group": "none",
			"windows": {
                "command": "Remove-Item",
                "args": [
                    "build",
                    "-Recurse",
                    "-Force",
                    "-Confirm:$false",
                    ";", 
                    "echo",
                    "Cleaning Build folder."
                ]
			},
			"osx": {
				"command": "rm -f -r build && echo Cleaning Build folder."
			},
            "options": {
                "cwd": "${workspaceFolder}/../"
            },
        },
		// ## Clean bin files
		{
            "label": "Clean Bin folder",
            "type": "shell",
            "group": "none",
			"windows": {
                "command": "Remove-Item",
                "args": [
                    "bin*",
                    "-Recurse",
                    "-Force",
                    "-Confirm:$false",
                    ";", 
                    "echo",
                    "Cleaning BIN folder."
                ]
			},
			"osx": {
				"command": "rm -f -r bin* && echo Cleaning BIN folder."
			},
            "options": {
                "cwd": "${workspaceFolder}/../"
            },
        },

        /
		// # Project files generation(premake5)
        /

        // ## VS2019
        {
            "label": "Generate build files (VS2019)",
            "type": "shell",
            "group": "none",
            "command": "premake5 vs2019"
        },     
        
        // ## XCode
        {
           "label": "Generate build files (XCode)",
           "type": "shell",
           "group": "none",
           "command": "premake5 xcode4"
        },

        /
		// # Project files build
        /

        // ## VS2019
        {
            "dependsOn":[
                "Generate build files (VS2019)"
            ],

            "label": "Build (VS2019)",
            "type": "shell",
            "group": "build",
            "command": "msbuild", //需要通过Visual Studio命令行打开, 比如(x64_x86 Cross Tools Command Prompt for VS 2019)
            "args": [
                "${workspaceFolderBasename}.sln",
				"/m",
				"/property:Configuration=${input:buildConfigVS2019}",   // Configuration: Debug or Release
				"/property:Platform=${input:buildPlatformVS2019}",      // Platform: x86 or x64
				// Ask msbuild to generate full paths for file names.
				"/property:GenerateFullPaths=true",
                "/t:build"
            ],
            "options": {
                "cwd": "${workspaceFolder}/../build"
            },
            "problemMatcher": ["$msCompile"],//捕捉编译时编译器在终端里显示的报错信息
            "presentation": {
				"reveal": "silent" // Reveal the output only if unrecognized errors occur.
			}
        },
        // ## XCode
        {
            "dependsOn":[           // 任务依赖,在执行该任务前,需要先执行依赖任务
                "Create Bin folder",
                "Generate build files (XCode)"
            ],

            "label": "Build (XCode)",
            "type": "shell",
            "group": "build",
            "command": "xcodebuild",
            "args": [
                "-workspace",
                "${workspaceFolderBasename}.xcworkspace",
                "-scheme", 
                "${input:buildProject}",
                "-configuration",
                "${input:buildConfigXCode}",
                "-arch",
                "x86_64"
            ],
            "options": {
              "cwd": "${workspaceFolder}/../build"
            },
            "problemMatcher": ["$gcc"]
        }
    ],

    "inputs": [
        {
            "id": "buildProject",                       // 标识
            "description": "Select project to build:",  // UI上的描述
            "default": "SFML",                          // 默认值
            "type": "pickString",                       // 类型,只有3个选项"promptString", "pickString", "command"
            "options": ["SFML"]                 // 选项
        },
        {
            "id": "buildConfigVS2019",
            "description": "Select build config for VS2019:",
            "default": "Debug",
            "type": "pickString",
            "options": ["Debug", "Release"]
		},
		{
            "id": "buildPlatformVS2019",
            "description": "Select build platform for VS2019:",
            "default": "Win32",
            "type": "pickString",
            "options": ["Win32", "x64"]
        },
        {
            "id": "buildConfigXCode",
            "description": "Select build config for XCode:",
            "default": "Debug",
            "type": "pickString",
            "options": ["Debug", "Release"]
		}
    ]
}

    到现在为止,在不引用第3方库的情况下,已经完全可以编译和调试C++程序了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值