简体   繁体   English

如何在Windows上编写,编写和编译C ++ 14

[英]How to cmake, make and compile C++14 on Windows

I'm trying to compile, on Windows 10, a C++ project that is using C++14 features such as std::make_unique<> and things like that. 我正在尝试在Windows 10上编译一个使用C ++ 14功能的C ++项目,例如std::make_unique<>等等。

I'd like to be able to compile easily in command line, using CMake and make and then be able to run my program from any command line or just by clicking it's executable. 我希望能够在命令行中轻松编译,使用CMake和make,然后能够从任何命令行运行我的程序,或者只需单击它的可执行文件。

Compiling using Cygwin, even if it's indeed working (I used this tutorial ), is not an option since the resulting executable won't be usable outside of the Cygwin environment due to missing DLLs. 使用Cygwin编译,即使它确实有效(我使用本教程 ),也不是一个选项,因为由于缺少DLL,生成的可执行文件将无法在Cygwin环境之外使用。

I've read about MinGW-w64, but it seems like the latest available version for Windows corresponds to GCC 4.8.3 . 我读过有关MinGW-w64的内容,但似乎最新的Windows版本与GCC 4.8.3相对应。

Same goes for the MinGW installer mingw-get-setup.exe available here , wich only allows me to install the 4.8.1-4 version. 同样适用于此处提供的MinGW安装程序mingw-get-setup.exe ,它只允许我安装4.8.1-4版本。

So I'd like to have a procedure on how to compile a C++14 project using CMake, and that will allow me to launch the executable in the default Windows environment. 所以我想有一个关于如何使用CMake编译C ++ 14项目的程序,这将允许我在默认的Windows环境中启动可执行文件。

Thanks. 谢谢。

Update : Chris Drew commented that I could use the latest Visual Studio version to build my project using the Visual C++ compiler instead of GCC. 更新: Chris Drew评论说我可以使用最新的Visual Studio版本来使用Visual C ++编译器而不是GCC来构建我的项目。 I detailed the workflow in my answer, but I'm still looking for a "GNU-style" way to compile it. 我在答案中详细说明了工作流程,但我仍然在寻找一种“GNU风格”的编译方式。

"GNU-style" : Use the link provided in Tive's comment to install a working GCC 5.1 environment and use the normal cmake . -G"Unix Makefiles" && make “GNU-style”:使用Tive评论中提供的链接安装正常工作的GCC 5.1环境并使用普通的cmake . -G"Unix Makefiles" && make cmake . -G"Unix Makefiles" && make commands. cmake . -G"Unix Makefiles" && make命令。

See this answer for more details on both solutions. 有关两种解决方案的更多详细信


Using Visual Studio compiler 使用Visual Studio编译器

Following Chris Drew comment , here's what I did in order to compile using CMake to generate a Visual Studio 2015 solution and compile this solution in command line. Chris Drew评论之后 ,这就是我为了使用CMake编译生成Visual Studio 2015解决方案并在命令行中编译此解决方案所做的工作。

Note that this is not using the GNU toolchain as we won't be using gcc / make but the Visual C++ Compiler. 请注意,这不是使用GNU工具链,因为我们不会使用gcc / make而是使用Visual C ++编译器。

Creating and moving to a build subdirectory (it's recommended since it will generate a lot of files, instead of just a standard Makefile) : 创建并移动到构建子目录(建议使用它,因为它会生成大量文件,而不仅仅是标准的Makefile):

mkdir build && cd build

Generating the Visual Studio 2015 solution : 生成Visual Studio 2015解决方案:

cmake . -G"Visual Studio 14 2015"

This will generate several project files including YourProjectName.sln that we will use to build the project using the following command : 这将生成几个项目文件,包括我们将使用以下命令构建项目的YourProjectName.sln

msbuild YourProjectName.sln

Note that msbuild was not in my PATH, so I had to provide the complete path to the executable wich was, in my case, C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\MSBuild.exe . 请注意, msbuild不在我的PATH中,因此我必须提供可执行文件的完整路径,在我的例子中, C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\MSBuild.exe


Using an updated MinGW installer 使用更新的MinGW安装程序

Tive provided a link to a bundled installer that will automatically install GCC 5.1 on your system, this way you can use the normal GNU toolchain by generating an Unix Makefile, and using the make command. Tive提供了一个链接到捆绑安装程序链接,该安装程序将自动在您的系统上安装GCC 5.1,这样您就可以通过生成Unix Makefile并使用make命令来使用普通的GNU工具链。

Note that you will need to edit your PATH manually as the installer is not doing it for you. 请注意,您需要手动编辑PATH,因为安装程序不会为您执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM