VSCode配置编译MSVC程序高级示例

VSCode配置编译MSVC程序高级示例

本文讲解了如何把一个VisualStudio C++(VC)项目转为VSCode(VSC)项目,并使用MSVC编译。MSVC编译一个程序分为编译(cl.exe)和链接(link.exe)的过程。有时候还需要生成静态库或动态库(lib.exe)。如果我们使用VC,这些过程是在背后运作的,如果使用VSC,则不得不了解这些命令行背后运作的机理。

本文的示例代码 fifoapi 可以在我的 github 上免费下载。这个代码演示了如何使用命名管道(named pipe 或 fifo)在进程间(客户端和服务端)直接传递消息。命名管道的细节我已经封装到 fifoapi 中,用户可以不用了解这些细节就能使用fifo。代码支持 Windows (提供了VSC工程)和 Linux (目前还未提供 VSC 工程)。

下载 fifoapi

fifo 示例代码本身就具有相当的实用价值,是我在2020年春节的期间窝在家里过年,百无聊赖时的作品:用于在一台机器的不同进程(无依赖关系)之间传递消息。你可以为任何目的使用它,前提是不改变
文件头部的版权声明部分:

Copyright © 2008-2080 pepstack.com, [email protected]

无论是 Windows还是Linux,无论服务端还是客户端,都使用了同一个头文件:fifo.h。它提供了简单易用的接口:

  • fifo.h

    const char * fifo_server_get_pipename (fifo_server server);
    int fifo_server_new (const char *pipename, int client_timeout, int connect_timeout, fifo_server *server);
    void fifo_server_runforever (fifo_server server, fifo_onpipemsg_cb pipemsgcb, void *argument);
    void fifo_server_free (fifo_server server);
    
    const char * fifo_client_get_pipename (fifo_client client);
    int fifo_client_new (const char *pipename, int wait_timeout, fifo_client *client);
    void fifo_client_free (fifo_client client);
    int fifo_client_write (fifo_client client, const fifo_pipemsg_t *msg);
    int fifo_client_read (fifo_client client, fifo_pipemsg_t *msg);
    

编译Windows的fifoapi需要 fifo-win.c,编译Linux的fifo需要 fifo.c。github的示例代码仅仅显示了如何使用VSCode来构建MSVC项目。以后我会加入mingw和Linux构建的部分。

我首先采用VSCode来构建fifo-win的静态库,然后又构建了客户端(fifoclient.exe)和服务端(fifoserver.exe)的测试程序。当然,构建测试程序不是必须的。工程目录下的 vscode-msvc-x64.bat 是构建 64 位程序用到的。双击 vscode-msvc-x64.bat,进入 cmd 窗口,在这个窗口里,进入到项目的目录下面,然后输入命令:

...\Workspace\fifoapi> code .

就打开了相应的(x86或x64)的VSCode。然后按 Ctrl+Shift+B,选择合适的 task 去构建。
在这里插入图片描述需要注意的是,如果是双击 vscode-msvc-x64.bat 打开的项目,不要去构建 x86 的 task。反之亦然。

具体编译参数我没有深入了解,这也是我用VSCode写的第一个实用的项目,初步体会是非常强大,非常技术,然而如果对项目构建的底层逻辑不了解,想用好还是有点学习曲线的。绝对不是开箱就能用的傻瓜式的。但是一旦掌握,你会觉得非常cool。我觉得仅仅cool这一点就足够了。

这里提供一下 tasks.json 全文,供快速参考。

  • tasks.json

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558 
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "type": "shell",
                "label": "win-all-debug-obj",
                "problemMatcher": ["$msCompile"],
                "command": "cl.exe",
                "args": [
                    "/MTd", "/D", "_DEBUG",
                    "/Fo:", "${workspaceFolder}\\build\\fifo-win.obj",
                    "/c", "${workspaceFolder}\\src\\fifo-win.c"
                ],
                "detail": "> compile Windows x86(32bits) debug objects: fifo-win.obj"
            },
            {
                "type": "shell",
                "label": "win-all-rels-obj",
                "problemMatcher": ["$msCompile"],
                "command": "cl.exe",
                "args": [
                    "/MT", "/D", "NDEBUG",
                    "/Fo:", "${workspaceFolder}\\build\\fifo-win.obj",
                    "/c", "${workspaceFolder}\\src\\fifo-win.c"
                ],
                "detail": "> compile Windows x86(32bits) release objects: fifo-win.obj"
            },
    
            {
                "type": "shell",
                "label": "win-x86-debug-staticlib",
                "dependsOn": [
                    "win-all-debug-obj",
                ],
                "problemMatcher": ["$msCompile"],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "command": "lib.exe",
                "args": [
                    "/MACHINE:X86",
                    "/OUT:${workspaceFolder}\\build\\fifo-win_x86d.lib",
                    "${workspaceFolder}\\build\\fifo-win.obj"
                ],
                "detail": "> build Windows x86(32bits) debug staticlib: fifo-win_x86d.lib"
            },
            {
                "type": "shell",
                "label": "win-x86-rels-staticlib",
                "dependsOn": [
                    "win-all-rels-obj",
                ],
                "problemMatcher": ["$msCompile"],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "command": "lib.exe",
                "args": [
                    "/MACHINE:X86",
                    "/OUT:${workspaceFolder}\\build\\fifo-win_x86.lib",
                    "${workspaceFolder}\\build\\fifo-win.obj"
                ],
                "detail": "> build windows x86(32bits) release staticlib: fifo-win_x86.lib"
            },
            {
                "type": "shell",
                "label": "win-x64-debug-staticlib",
                "dependsOn": [
                    "win-all-debug-obj",
                ],
                "problemMatcher": ["$msCompile"],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "command": "lib.exe",
                "args": [
                    "/MACHINE:X64",
                    "/OUT:${workspaceFolder}\\build\\fifo-win_x64d.lib",
                    "${workspaceFolder}\\build\\fifo-win.obj"
                ],
                "detail": "> build Windows x64 debug staticlib: fifo-win_x64d.lib"
            },
            {
                "type": "shell",
                "label": "win-x64-rels-staticlib",
                "dependsOn": [
                    "win-all-rels-obj",
                ],
                "problemMatcher": ["$msCompile"],
                "command": "lib.exe",
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "args": [
                    "/MACHINE:X64",
                    "/OUT:${workspaceFolder}\\build\\fifo-win_x64.lib",
                    "${workspaceFolder}\\build\\fifo-win.obj"
                ],
                "detail": "> build Windows x64 release staticlib: fifo-win_x64.lib"
            },
            {
                "type": "shell",
                "label": "win-all-debug-examples-obj",
                "problemMatcher": ["$msCompile"],
                "command": "cl.exe",
                "args": [
                    "/Zi", "/MTd", "/D", "_DEBUG", "/EHsc",
                    "/c",
                    "${workspaceFolder}\\examples\\fifoclient.c",
                    "${workspaceFolder}\\examples\\fifoserver.c"
                ],
                "detail": "> compile Windows x64 debug examples objects"
            },
            {
                "type": "shell",
                "label": "win-all-rels-examples-obj",
                "problemMatcher": ["$msCompile"],
                "command": "cl.exe",
                "args": [
                    "/Zi", "/MT", "/D", "NDEBUG", "/EHsc",
                    "/c",
                    "${workspaceFolder}\\examples\\fifoclient.c",
                    "${workspaceFolder}\\examples\\fifoserver.c"
                ],
                "detail": "> compile Windows x64 release examples objects"
            },
            {
                "label": "win-x64-debug-fifoclient",
                "dependsOn": [
                    "win-x64-debug-staticlib",
                    "win-all-debug-examples-obj"
                ],
                "dependsOrder": "sequence",
                "problemMatcher": ["$msCompile"],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "command": "link.exe",
                "args": [
                    "${workspaceFolder}\\build\\fifo-win_x64d.lib",
                    "${workspaceFolder}\\fifoclient.obj",
                    "/out:${workspaceFolder}\\build\\fifoclient.exe"
                ],
                "detail": "> build Windows x64 debug app: fifoclient.exe"
            },
            {
                "label": "win-x64-debug-fifoserver",
                "dependsOn": [
                    "win-x64-debug-staticlib",
                    "win-all-debug-examples-obj"
                ],
                "dependsOrder": "sequence",
                "problemMatcher": ["$msCompile"],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "command": "link.exe",
                "args": [
                    "${workspaceFolder}\\build\\fifo-win_x64d.lib",
                    "${workspaceFolder}\\fifoserver.obj",
                    "/out:${workspaceFolder}\\build\\fifoserver.exe"
                ],
                "detail": "> build Windows x64 debug app: fifoserver.exe"
            },
            {
                "label": "win-x64-rels-fifoclient",
                "dependsOn": [
                    "win-x64-rels-staticlib",
                    "win-all-rels-examples-obj"
                ],
                "dependsOrder": "sequence",
                "problemMatcher": ["$msCompile"],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "command": "link.exe",
                "args": [
                    "${workspaceFolder}\\build\\fifo-win_x64.lib",
                    "${workspaceFolder}\\fifoclient.obj",
                    "/out:${workspaceFolder}\\build\\fifoclient.exe"
                ],
                "detail": "> build Windows x64 release app: fifoclient.exe"
            },
            {
                "label": "win-x64-rels-fifoserver",
                "dependsOn": [
                    "win-x64-rels-staticlib",
                    "win-all-rels-examples-obj"
                ],
                "dependsOrder": "sequence",
                "problemMatcher": ["$msCompile"],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "command": "link.exe",
                "args": [
                    "${workspaceFolder}\\build\\fifo-win_x64.lib",
                    "${workspaceFolder}\\fifoserver.obj",
                    "/out:${workspaceFolder}\\build\\fifoserver.exe"
                ],
                "detail": "> build Windows x64 release app: fifoserver.exe"
            },
            {
                "label": "win-x86-debug-fifoclient",
                "dependsOn": [
                    "win-x86-debug-staticlib",
                    "win-all-debug-examples-obj"
                ],
                "dependsOrder": "sequence",
                "problemMatcher": ["$msCompile"],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "command": "link.exe",
                "args": [
                    "${workspaceFolder}\\build\\fifo-win_x86d.lib",
                    "${workspaceFolder}\\fifoclient.obj",
                    "/out:${workspaceFolder}\\build\\fifoclient.exe"
                ],
                "detail": "> build Windows x86 debug app: fifoclient.exe"
            },
            {
                "label": "win-x86-debug-fifoserver",
                "dependsOn": [
                    "win-x86-debug-staticlib",
                    "win-all-debug-examples-obj"
                ],
                "dependsOrder": "sequence",
                "problemMatcher": ["$msCompile"],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "command": "link.exe",
                "args": [
                    "${workspaceFolder}\\build\\fifo-win_x86d.lib",
                    "${workspaceFolder}\\fifoserver.obj",
                    "/out:${workspaceFolder}\\build\\fifoserver.exe"
                ],
                "detail": "> build Windows x86 debug app: fifoserver.exe"
            },
            {
                "label": "win-x86-rels-fifoclient",
                "dependsOn": [
                    "win-x86-rels-staticlib",
                    "win-all-rels-examples-obj"
                ],
                "dependsOrder": "sequence",
                "problemMatcher": ["$msCompile"],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "command": "link.exe",
                "args": [
                    "${workspaceFolder}\\build\\fifo-win_x86.lib",
                    "${workspaceFolder}\\fifoclient.obj",
                    "/out:${workspaceFolder}\\build\\fifoclient.exe"
                ],
                "detail": "> build Windows x86 release app: fifoclient.exe"
            },
            {
                "label": "win-x86-rels-fifoserver",
                "dependsOn": [
                    "win-x86-rels-staticlib",
                    "win-all-rels-examples-obj"
                ],
                "dependsOrder": "sequence",
                "problemMatcher": ["$msCompile"],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "command": "link.exe",
                "args": [
                    "${workspaceFolder}\\build\\fifo-win_x86.lib",
                    "${workspaceFolder}\\fifoserver.obj",
                    "/out:${workspaceFolder}\\build\\fifoserver.exe"
                ],
                "detail": "> build Windows x86 release app: fifoserver.exe"
            }
        ]
    }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章