Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
[原文发表地址] 用Visual Studio Code 来生成您的C++应用程序
[原文发表时间] 2016/10/24
在过去的几个月里,我们收到了很多反馈关于希望可以在Visual Studio Code 中允许开发人员生成他们的C++应用程序。在Visual Studio Code中这个任务可以扩展为使生成,打包,测试和配置这些任务可以自动化实现。这篇文章将会说明如何在Visual Studio Code中使用任务扩展性,您可以通过下面各部分的帮助来调用编译,生成系统和其他外部的任务:
- 安装C/C++生成工具
- 生成一个简单的Visual Studio Code任务
- 在Visual Studio Code任务中直接调用GCC和Clang
- 使用Visual Studio Code任务扩展来调用Makefiles
- 使用Visual Studio Code任务扩展来调用MSbuild
- 使用Visual Studio Code扩展来调用CMake
安装C/C++生成工具
为了生成您的C++代码您首先应该确认在您的机器中已经安装了C/C++生成工具(编译器,连接器和生成系统)。如果您已经在Visual Studio Code外安装了这些工具并可以在外部生成工程,您可以直接跳到下一部分。
您可以在Visual C++ 生成工具中下载这些工具然后安装在机器里。这些工具默认的安装路径是 'C:\Program Files (x86)\Microsoft Visual C++ Build Tools'。如果您还没有安装Visual studio, 您只需要做这一步就好了。如果您已经安装了Visual Studio, 那么您需要的一切已经准备就绪了。
如果您工作在支持apt-get的Linux平台,您可以运行下面的命令来确保您是否安装了正确的可以生成C/C++代码的工具。
sudo apt-get install g++
sudo apt-get install clang
在OS X上,安装C++生成工具最简单的方法就是安装Xcode命令行工具。您可以参考苹果开发者论坛上的这篇文章。我比较建议安装这个而不是直接在他们的clang 工具箱的版本上安装clang作为Apple的附加品。一旦您安装好之后,您可以在终端窗口上运行这些命令来确认您将编译和生成的工具安装的位置。
xcodebuild -find make
xcodebuild -find gcc
xcodebuild -find g++
xcodebuild -find clang
xcodebuild -find clang++
为生成C/C++代码创建一个简单的Visual Studio Code任务
为了更好的学习这一部分您可以先去下载helloworld 的C++源文件夹。如果您运行过程出现了问题可以去下载相同的已经预先设置了任务的C++源文件夹。
如果您想学习C++并要理解被涉及到的执行一个简单生成的不同组件,您可以查看这个指南。
在Visual Studio Code中任务被定义为一个工作空间,Visual Studio Code已经预安装了一个通用的任务运行列表。在命令面板中(Ctrl+Shift+P (Win, Linux), ⇧⌘P (Mac))您可以键入task然后可以看到全部的和task相关的命令。
在命令面板可执行的 ‘Configure Task Runner’选项中您将会看到预先安装的任务列表如下图所示。以后我们将会把这种任务通道的列表发展成为更受欢迎的系统,但是目前只能在这个列表中获取其他的模板。
这样将会在您的.vscode 文件夹中生成一个tasks.json的文件,文件内容如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "echo",
"isShellCommand": true,
"args": ["Hello World"],
"showOutput": "always"
}
设置窗口
在Windows上,为C/C++生成应用程序而安装Visual Studio Code最简单的方法就是用下面的命令生成一个名为build.bat的batch文件”:
@echo off
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64
set compilerflags=/Od /Zi /EHsc
set linkerflags=/OUT:hello.exe
cl.exe %compilerflags% helloworld.cpp /link %linkerflags%
请注意为生成应用程序设置合适环境的vcvarsall.bat文件的路径在每个人的电脑上可能是不同的。如果您正在使用Visual C++ build SKU,您则需要调用下面的命令:
call “C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat” x64
一旦生成脚本准备好了,然后您就可以修改您的task.json。通过做下列的改变直接调用batch文件来自动生成的task.json文件。
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"windows": {
"command": "build.bat",
"isShellCommand": true,
"showOutput": "always"
}
通过再次打开命令面板并运行‘run build task’这条命令来开始一个应用程序的生成。
这是C++应用程序开始在生成,,您可以在输出窗口中查看这个过程。
尽管这些是在Windows上的例子,但您也可以使用相同的步骤在其他平台上去调用一个生成应用程序的脚本。
为生成 C/C++代码而在visual studio code 任务中直接调用GCC和Clang
接下来让我们看看如何不调用外部的batch文件来生成C/C++应用程序,这时我们可以直接使用像GCC和Clang这样受欢迎的成套工具而不用一个生成系统。
为了更好的学习这一部分您可以先去下载helloworld 的C++源文件夹。如果您运行过程出现了问题可以去下载相同的已经预先设置了任务的C++源文件夹。
Tasks.json文件允许用户指定修饰符,比如下面的‘OS X’。这些类似的修饰符将允许您为不同的构建目标不同的平台生成特定的生成配置,就如本例所示。
"OS X": {
"command": "clang++",
"args": [
"-Wall",
"helloWorld.cpp",
"-v"
],
"isShellCommand": true,
"showOutput": "always",
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
在这一节需要强调的另一件事是 “Problem Matcher”部分。Visual Studio Code提供了一些很普遍的问题匹配可以立即使用,而大多数的编译器和其它的工具都会定义他们自己的Errors 和Warnings风格。您不必担心,您可以在Visual Studio Code上自定义您的问题匹配。这个网站帮助您在线测正则表达式的时候应该可以派得上用场。
这里的匹配模式对Clang和GCC工具套来说可以正常工作,所以您可以直接使用它们。下图显示了当您在Visual Studio Code中启动显示问题的命令时对他们的影响。(Cntrl+Shift+M (Win, Linux), ⇧⌘M (Mac))。
使用Visual Studio Code任务扩展来调用Makefiles
类似于您如何配置tasks.json文件来调用编译器的方法,您可以对makefiles做相同的操作。请看下面这个关于tasks.json文件的例子,在这个tasks.json文件中有一个新的概念就是关于任务的嵌套。在makefile文件中‘hello’和‘clean’都是任务同时‘compile w/o makefile’是独立的任务。但是这个例子可以告诉您如何生成tasks.json文件来防止有多个生成系统在起作用。您可以在这里看到整个例子。
注意这是一个OSX,Linux的具体实例但是同样也试用于Windows, 您可以用cmd来替换bash, 用/c来替换args。
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"osx": {
"command": "bash",
"args": ["-c"],
"isShellCommand": true,
"showOutput": "always",
"suppressTaskName": true,
"options": {
"cwd": "${workspaceRoot}"
},
"tasks": [
{
"taskName": "hello",
"args": [
"make hello"
],
"isBuildCommand": true
},
{
"taskName": "clean",
"args": [
"make clean"
]
},
{
"taskName": "compile w/o makefile",
"args": [
"clang++ -Wall -g helloworld.cpp -o hello"
],
"echoCommand": true
}
]
}
}
还有需要注意的是在Visual Studio Code不管您把那个任务与‘isBuildCommand’结合在一起,这个都将会成为您的默认生成任务。既然这样,那么‘hello’这个任务将会成为默认生成任务。如果您想要运行其他的任务您可以启动命令面板然后选择‘run task’这个选项。
然后可以去选择独立的任务来运行,例如选择‘clean’这个任务项。或者您也可以将生成的任务设置为密钥绑定。可以通过file->preferences->keyboard shortcuts来打开快捷键,然后将下面的密钥绑定添加到您的任务中。装帧绑定现在只在生成和测试任务中存在,但将在十月份发布的版本中将会有一个修改,同时也将允许对单个的任务进行绑定。
[
{
"key": "f7",
"command": "workbench.action.tasks.build"
}
]
使用Visual Studio Code任务扩展来调用MSbuild
MSbuild是随着Visual Studio Code的安装已经被预安装的任务。您可以启动命令面板然后选择MSbuild,将会生成下面的task.json文件,这时在“args”部分添加您的MSbuild解决方案和项目名称是比较容易的。
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "msbuild",
"args": [
// Ask msbuild to generate full paths for file names.
"/property:GenerateFullPaths=true"
],
"taskSelector": "/t:",
"showOutput": "silent",
"tasks": [
{
"taskName": "build",
// Show the output window only if unrecognized errors occur.
"showOutput": "silent",
// Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile"
}
]
}
使用Visual Studio Code扩展来调用CMake
现阶段在Visual Studio Code的应用市场中有两个Visual Studio Code的扩展。第一个扩展提供了CMake的语言服务支持,第二个扩展允许可以生成您自己的CMake目标文件。在Visual Studio Code中想要有更好的CMake体验您可以把两个扩展都安装上。
一旦配置好后您就可以生成特定的CMake目标文件并可以做其他的CMake操作如下图所示。
总结
这篇文章给您提供了指南关于如何使用Visual Studio Code任务扩展来生成您的C/C++应用程序。如果您想要我们提供更多关于Visual Studio Code任何其他方面的指南请务必通过在Github page上填写问题来和我们保持联系,并希望您继续尝试这方面的体验。如果您想要在未来也为这个扩展的改变出一份力的话,请加入我们的cross platform C++ Insiders group,在这里您可以和我们直接联系并可以帮助我们一起使这个产品更好地满足您的需求。