VSCode配置编译MSVC程序
本文来自于以下链接的实践:
Configure VS Code for Microsoft C++
前言
自从盘古开天辟地以来,乱世纷争,群雄并起。在编程IDE领域,各路豪杰争霸,互不相让。直是乱花渐欲迷人眼,何处是归途?所谓天下大势,分久必合。微软乃上顺天意,下应民心,兼相爱,交相利。于是,VSCode横空出世,利刃出鞘,魑魅魍魉,片甲不留。一时间,各路豪杰避之唯恐不及。其构思精妙绝伦,色泽圆润无比。30多万行脚本代码,垒出盖世神宫,不可谓不是神来之笔。从此以后,秦时明月,汉时古道,小桥流水,暖玉生烟,乾坤朗朗,天下太平。
Electron 可以让你使用纯 JavaScript 调用丰富的原生 APIs 来创造桌面应用
VSCode团队负责人:Erich Gamma . JUnit 作者之一,《设计模式》作者之一, Eclipse 架构师。2011 加入微软,在瑞士苏黎世组建团队开发基于 web 技术的编辑器,也就是后来的 monaco-editor。VSCode 开发团队从 10 来个人开始,早期成员大多有 Eclipse 开发团队的背景。
配置编译MSVC程序
- 确定已经安装了Microsoft Visual Studio(我用的是2015)。点此直达Visual Studio下载页面。安装位置在C盘默认位置。为此C盘至少要有500GB空间,这样才能一劳永逸。
- 确定已经安装了VSCode.点此直达VSCode下载页面.如果选择了zip包,需要手工配置Path环境变量。
- 编辑2个批处理文件,一个用于编译32位程序(vscode-x86.bat),一个用于64位(vscode-x64.bat)。都放在桌面上备用。内容一句话(根据Visual Studio安装位置决定):
- vscode-x86.bat
cmd /K "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86
- vscode-x64.bat
cmd /K "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
检查载入的环境:
cl.exe /?
- 双击 vscode-x64.bat 打开一个cmd窗口,在此窗口中输入命令以切换到工程父目录。新建helloworld工程。
cd ..
mkdir Projects
cd Projects
mkdir helloworld
code .
- 从此 helloworld 项目目录 “${workspaceFolder}” 就是你的衣食父母。
code . 命令在${workspaceFolder}目录下创建了一个文件夹.vscode,里面未来将会有3个文件:
(1) tasks.json (构建设定)
[VSCode菜单] Terminal > Configure Default Build Task
选中 C/C++: cl.exe build active file,新建或打开 tasks.json
(2) launch.json (调试设定)
[VSCode菜单] Debug > Add Configuration…
选中 C++ (Windows),新建launch.json
(3) c_cpp_properties.json (编译路径和智能提示设定)
Ctrl+Shift+P
输入: C/C++: Edit Configurations (UI 或 JSON) - 给你的项目添加源代码文件
- Ctrl+Shift+B 编译
- F5 调试
调试的快捷键用法和VisualStudio一样。
helloworld示例工程
- 项目结构
helloworld/
|
+-- .gitignore
+-- Makefile
+-- README.md
+-- VERSION
+-- AUTHOR
+-- .git/
+-- .vscode/
|
+-- tasks.json
+-- launch.json
+-- c_cpp_properties.json
+-- src/
|
+-- helloapi.h
+-- helloapi.c
+-- helloworld.c
- 部分文件内容
- 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": "cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${workspaceFolder}\\build\\helloworld.exe",
"${workspaceFolder}\\src\\helloworld.c",
"${workspaceFolder}\\src\\helloapi.c"
],
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
- 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": "cl.exe build and debug active file",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/build/helloworld.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}/build",
"environment": [],
"externalConsole": false,
"preLaunchTask": "cl.exe build active file"
}
]
}
- c_cpp_properties.json
{
"configurations": [
{
"name": "Win32", // Win32, Linux, Mac 这3名字是针对特定平台的保留字,系统会自动匹配。其他名字任意
"includePath": [
"${workspaceFolder}/src/**",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.10240.0/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.10240.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
- helloapi.h
/**
* helloapi.h
*/
#ifndef HELLOAPI_H
#define HELLOAPI_H
#if defined(__cplusplus)
extern "C"
{
#endif
extern const char * get_appname (void);
extern const char * get_version (void);
#ifdef __cplusplus
}
#endif
#endif /* HELLOAPI_H */
- helloapi.c
#include "helloapi.h"
const char * get_appname (void)
{
return "helloworld";
}
const char * get_version (void)
{
return "0.0.1";
}
- helloworld.c
#include <stdio.h>
#include "helloapi.h"
int main (int argc, const char *argv[])
{
printf("%s-%s sample with vscode!\n", get_appname(), get_version());
return 0;
}
- Makefile (build for gcc)
PREFIX=.
BUILDDIR=$(PREFIX)/build
SRCDIR=$(PREFIX)/src
APPINCLUDE=-I$(SRCDIR)
# cygwin or linux
CC=gcc
# build for release
CFLAGS=-D_GNU_SOURCE -DNDEBUG -O2
# build for valgrind memory check:
# $ valgrind --leak-check=full --show-leak-kinds=all ./logapp -n10000000
#CFLAGS=-D_GNU_SOURCE -g
# application binary
APPBIN=helloworld
# objects for static lib
APPOBJS=helloapi.o
all: $(APPBIN)
help:
@echo "make"
helloapi.o: $(SRCDIR)/helloapi.c
$(CC) $(CFLAGS) -c $(SRCDIR)/helloapi.c -o $@
# target for building application
$(APPBIN): $(APPOBJS) $(SRCDIR)/helloworld.c
$(CC) $(CFLAGS) $(SRCDIR)/helloworld.c $(APPINCLUDE) -o $@ \
$(APPOBJS) \
-lpthread -lrt -lm
clean:
-rm -f $(BUILDDIR)/*
.PHONY: all clean