使用 Visual Studio Code 開發Linux C 程序


使用 Visual Studio Code 開發Linux C 程序

背景

目前,使用 Windows SubSystem Linux + Visual Studio Code 可以高效地開發 Linux C/C+ 程序。這一套開發環境可謂是目前(2020年5月)最方便的開發工具。

步驟

step 1,打開 VS Code,點擊左下角綠色按鈕連接 WSL 子系統。

step 2,創建 launch.json 文件。

step 3,創建 task.json 文件。

step 4,創建源文件,編寫源代碼。

step 5,點擊 Run -> Run Without Debugging,或者 Ctrl + F5

文件內容

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],  //命令行參數
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "gcc build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.json

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "shell",
			"label": "gcc build active file",
			"command": "/usr/bin/gcc",
			"args": [
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}",
				"-pthread"	//加入gcc編譯時的參數
			],
			"options": {
				"cwd": "/usr/bin"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build"
		}
	]
}

源文件

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread_foo(void *id) {
    printf("%d\n", (int)id);
    pthread_exit(NULL);  //線程退出
}

int main() {
    int thread_num = 40;

    pthread_t *pt = (pthread_t*)malloc(thread_num * sizeof(pthread_t));
    
    for (int i = 0; i < thread_num; i++)
        pthread_create(&pt[i], NULL, thread_foo, (void *)i);  //注冊線程
    
    for (int i = 0; i < thread_num; i++) 
        pthread_join(pt[i], NULL);  //線程執行
    
    return 0;
}

研究意義

從此以后,word2vec變體的研究就更加方便了,不需要再學習 gdb、vim 等命令行開發工具,只要會使用圖形界面開發工具即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM