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 工程)。
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" } ] }