14

配置Windows 10的C++开发环境

 2 years ago
source link: https://www.myfreax.com/configure-the-c-development-environment-of-windows-10/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Windows的C++开发环境,确实不如Linux配置C++开发环境容易,你需要编译一个Hello World都会给你扔一个错误。

在本教程中,配置Windows 10的C++开发环境并安装visual studio。安装C++组件,整合到任意终端。并使用vscode进行开发

安装visual studio

下载visual studio并安装,然后打开visual studio installer,选择使用“桌面C++开发”

Windows C++的hello world

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(){
  vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
  for(const string& word:msg){
    cout << word << " ";
  }
  cout << endl;
}

helloworld.cpp

编译HelloWorld

cl helloworld.exe helloworld.cpp

LINK : fatal error LNK1149: output filename matches input filename

你将会获得以下错误

Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30038.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

cl : Command line warning D9024 : unrecognized source file type 'helloworld.exe', object file assumed
helloworld.cpp
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\ostream(746): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
helloworld.cpp(10): note: see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char *)' being compiled
Microsoft (R) Incremental Linker Version 14.29.30038.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:helloworld.exe
helloworld.exe
helloworld.obj
LINK : fatal error LNK1149: output filename matches input filename 'E:\work\cplusplus\hello\helloworld.exe

这是Google一圈回来的结果,那有一个简单编译,就要加一堆参数的。

cl.exe /Zi /EHsc /Fe: helloworld.exe helloworld.cpp

你可能想问,cl.exe的参数是指的什么,现在你可能会找帮助文档。这对于我这个不懂CMD的人来说,先后以多种方式尝试执行cl.exe命令

cl.exe
cl.exe \help
cl.exe help
cl.exe --help
help cl

终于在help cl.exe 提示我该怎么看帮助文档了。

This command is not supported by the help utility.  Try "cl /?".

  • /EHs enable C++ EH (no SEH exceptions)  /EHa enable C++ EH (w/ SEH exceptions) 开启C++ EH
  • /Zi enable debugging information 开启调试信息
  • /Fe: <file> name executable file 指定可执行文件的名称

Fatal error C1034: windows.h: no include path set

解决`Fatal error C1034: windows.h: no include path set`

中文fatal error c1034:iostream: 不包括路径集

解决方法:从你开发命令行提示符中把LIB,INCLUDE,LIBPATH设置到系统级环境变量中

在Deveoper Command Prompt执行set命令,这将会打印所有环境变量,找到LIB,INCLUDE,LIBPATH这个三个环境变量

添加到系统级环境变量中

这样你就可以在任意终端编译,包括在wsl,powershell中,不用忍受垃圾cmd


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK