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