使用 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 等命令行开发工具,只要会使用图形界面开发工具即可。

posted @   健康平安快乐  阅读(1123)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示