首页 > 编程语言 >VSCode配置编译MSVC程序

VSCode配置编译MSVC程序

时间:2023-01-27 11:32:20浏览次数:54  
标签:helloapi VSCode +-- helloworld 编译 json build MSVC


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程序

  1. 确定已经安装了Microsoft Visual Studio(我用的是2015)。​​点此直达Visual Studio下载页面​​。安装位置在C盘默认位置。为此C盘至少要有500GB空间,这样才能一劳永逸。
  2. 确定已经安装了VSCode.​​点此直达VSCode下载页面​​.如果选择了zip包,需要手工配置Path环境变量。
  3. 编辑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 /?
  1. 双击 vscode-x64.bat 打开一个cmd窗口,在此窗口中输入命令以切换到工程父目录。新建helloworld工程。
cd ..
mkdir Projects
cd Projects
mkdir helloworld
code .
  1. 从此 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)
  2. 给你的项目添加源代码文件
  3. Ctrl+Shift+B 编译
  4. F5 调试
    调试的快捷键用法和VisualStudio一样。

helloworld示例工程

  1. 项目结构
helloworld/
|
+-- .gitignore
+-- Makefile
+-- README.md
+-- VERSION
+-- AUTHOR
+-- .git/
+-- .vscode/
|
+-- tasks.json
+-- launch.json
+-- c_cpp_properties.json
+-- src/
|
+-- helloapi.h
+-- helloapi.c
+-- helloworld.c
  1. 部分文件内容
  • 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


标签:helloapi,VSCode,+--,helloworld,编译,json,build,MSVC
From: https://blog.51cto.com/mapaware/6024039

相关文章

  • Windows上使用VSCode结合mingw编译和调试C程序
    Windows上使用VSCode结合mingw编译和调试C程序目标是在Win7上,用VSCode使用mingw调试C程序.要设置终端为bash.exe.Windows7已经安装了VSCode(及相关插件),MSYS64,......
  • vscode bash
    目录截图步骤截图步骤左下角点击设置选择Settings输入"terminal.integrated.profiles.windows"点击"Editinsettings.json"输入"terminal.integrated.pr......
  • 编译原理分析器大作业之字幕分析器
            写这篇文章的主要目的呢是分享一下编译原理大作业——电影字幕分析器,分享一下我的做法,可能采用的做法不是特别好的用法,希望各位多多指点,觉得文章不错给点小......
  • chromium编译
     vs安装  cmd里 使用命里wInver  查看win版本  对应你电脑win10,11的sdk   ......
  • VScode中调试Unity【Debugger for Unity】
    我遇到的情况:在点击运行和调试时,出现中间栏让你选择调试器,我点击UnityDebugger没反应。单击创建Launch文件也无效,没有UnityDebugger的选项删除.vscode/launch.json如......
  • 2023 项目探秘:从零开始编译Asepirte
    前言Aseprite是收费软件,请大家尊重版权,尊重开发者的创作成果。Aseprite官网Asepirte简介Aseprite是一款用于像素作画的软件。可用于游戏精灵(Sprite)或者像素背景等......
  • Nginx1.10 编译安装
    安装环境系统:Centos6.8软件:Nginx1.10.2依赖软件:Pcre、Zlib、Openssl安装前准备安装编译环境yum-yinstallwgetyum-yinstallgccgcc-c++autoconfautomakemakey......
  • go mod init 之后 vscode报错gopls was not able to find modules in your workspace
    (27条消息)vscode报错packagexxxisnotinGOROOT(path)或者go:toaddmodulerequirementsandsums:gomodtidy_Kpdo的博客-CSDN博客上文是我在网上看了2天2夜,......
  • 使用VS2019编译EDK2的方法
    原先自己编译的EDK2的情况,有点旧,本次更新EDK2使用2019的编译器编译EDK2需要的工具链如下,自行下载哈:VS2019:Python3.8:​​https://www.python.org/downloads/release/python-......
  • lazarus 编译为Linux gtk2的应用使用TDateTimePIcker日历在tkDate模式日历下拉菜单不
    网友<安全生产监管>发现lazarus编译为Linux gtk2的应用使用TDateTimePIcker日历在tkDate模式,日历下拉菜单不响应鼠标点击,这个问题在windows和linuxqt下没问题。环境:1、L......