在 Windows 上使用 cmake for c++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9878225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:21:50  来源:igfitidea点击:

Using cmake on windows for c++

c++cmake

提问by Yaman Baron

for around 5 consecutive days i have been trying to set up my computer with the c++ environment for programming with libraries such as sdl,glm,opengl. its important for us to be able to run it on unix machines on presentations so im running with cmake. i finally got it to work with the cmake-gui, i wont even bother trying anymore with any IDE.

连续大约 5 天,我一直在尝试使用 C++ 环境设置我的计算机,以便使用 sdl、glm、opengl 等库进行编程。能够在 unix 机器上在演示文稿上运行它对我们来说很重要,所以我用 cmake 运行。我终于让它与 cmake-gui 一起工作了,我什至不会再尝试使用任何 IDE。

i specified my folder project and where to build the binaries, i got a folder "CMakeFiles" along with a txt "CMakeCache", a CMAKE file "cmake_install.cmake" and a file "Makefile". also in my folder "CMakeFiles" there are lots of other folders such as "CMakeTmp", "CompilerIdC", "CompilerIdCXX etc" and in both folders "Compiler*" has each an .exe which doesnt work! so where is my wanted executable?

我指定了我的文件夹项目以及构建二进制文件的位置,我得到了一个文件夹“CMakeFiles”以及一个 txt“CMakeCache”、一个 CMAKE 文件“cmake_install.cmake”和一个文件“Makefile”。同样在我的文件夹“CMakeFiles”中,还有很多其他文件夹,例如“CMakeTmp”、“CompilerIdC”、“CompilerIdCXX 等”,并且在两个文件夹中,“Compiler*”都有一个 .exe,但它不起作用!那么我想要的可执行文件在哪里?

i opened cmd and navigated to my folder and tried to write "make" as we are supposed to do according to the intruction. alas, it didnt work very well. hoping you could share your wisdom and help a newbie like me!

我打开 cmd 并导航到我的文件夹并尝试按照说明编写“make”。唉,它没有很好地工作。希望你能分享你的智慧并帮助像我这样的新手!

so what exactly is needed for compiling projects containing additional libraries? so far i have a compiler, Mingw32, the latest CMake and using the cmake-gui for extracting the binaries but gets makefiles.

那么编译包含附加库的项目到底需要什么?到目前为止,我有一个编译器,Mingw32,最新的 CMake,并使用 cmake-gui 来提取二进制文件,但会获取 makefile。

EDIT: hrrm. is it only me who gets these kind of problems? i can add that i have look thorough about 10 tutorials and 90% of the steps are similar (if compiling with VS which i tried at first):

编辑:hrrm。只有我会遇到这些问题吗?我可以补充一点,我已经仔细阅读了大约 10 个教程,其中 90% 的步骤是相似的(如果使用我最初尝试的 VS 进行编译):

  • Download latest SDL
  • Make a folder on e.g C:\SDL with two folders, include and lib
  • Copy the libs and includes from the downloaded SDL
  • Make new VS project, open VC++ directories and add lib/incl folder on e.g C:\SDL
  • Add to linker SDL.lib and SDLmain.lib (i made sure they got linked, no problem here)
  • Change system to WINDOWS (optional if you dont want two windows)
  • Added include to "additional libraries"
  • Put the SDL.dll file (which i got from the latest SDL) in my C:\windows\system32(64SysWoW) and also in my project file.
  • 下载最新的 SDL
  • 在例如 C:\SDL 上创建一个文件夹,其中包含两个文件夹,包括和 lib
  • 从下载的 SDL 复制库和包含
  • 创建新的 VS 项目,打开 VC++ 目录并在例如 C:\SDL 上添加 lib/incl 文件夹
  • 添加到链接器 SDL.lib 和 SDLmain.lib(我确保它们已链接,这里没问题)
  • 将系统更改为 WINDOWS(如果您不想要两个窗口,则可选)
  • 将包含添加到“附加库”
  • 将 SDL.dll 文件(我从最新的 SDL 中获得)放在我的 C:\windows\system32(64SysWoW) 和我的项目文件中。

so what i am actually looking for is gettning the CMake to work, since it generates and builds sources successfully (with the gui) and i feel im closing in. do i need to add any additional libraries from sdl to my compiler mingw32 and/or cmake?

所以我实际上正在寻找的是让 CMake 工作,因为它成功生成和构建源代码(使用 gui),我觉得我正在接近我是否需要从 sdl 添加任何额外的库到我的编译器 mingw32 和/或cmake?

采纳答案by Bob

You were almost there with Visual Studio. Select Visual Studio as target. Open the generated project in Visual Studio, build it. (just like you alread did). Then, instead of trying to run BUILD_ALL, run a real project that creates an executable, it should also be in that list. Just right click it and 'play' it.

你几乎已经有了 Visual Studio。选择 Visual Studio 作为目标。在 Visual Studio 中打开生成的项目,构建它。(就像你已经做的那样)。然后,不要尝试运行 BUILD_ALL,而是运行一个创建可执行文件的真实项目,它也应该在该列表中。只需右键单击它并“播放”它。

If you still get errors, post them in detail including what you did before the error. Note: a carefully configured cross platform CMake project (aka the CMakeLists.txt) should not require any fiddling with VC++ directories. It should work automagically, especially with well known libs such as SDL.

如果您仍然遇到错误,请详细发布它们,包括您在错误之前所做的事情。注意:精心配置的跨平台 CMake 项目(又名 CMakeLists.txt)不应该需要对 VC++ 目录进行任何摆弄。它应该自动工作,尤其是使用 SDL 等众所周知的库。

回答by Protoss

if you run cmake by command:

如果您通过命令运行 cmake:

cmake -G "Visual Studio 14 Win64" path\to\source\dir

you need to run this command to continue(in Visual Studio Command Prompt):

您需要运行此命令才能继续(在Visual Studio 命令提示符中):

msbuild Project.sln

either if you run cmake:

如果您运行 cmake:

cmake -G "NMake Makefiles" path\to\source\dir

you need to run this cmd to continue(in Visual Studio Command Prompt):

您需要运行此 cmd 才能继续(在Visual Studio 命令提示符中):

nmake

回答by stativ

If I understood it correctly you want to use CMake in your project. I'm using CMake in all my projects. I won't give you exact step-by-step howto, since I use Arch Linux but I used it in Windows 7 too.

如果我理解正确,你想在你的项目中使用 CMake。我在所有项目中都使用 CMake。由于我使用 Arch Linux,但我在 Windows 7 中也使用过它,因此我不会为您提供确切的分步操作方法。

  1. To make CMake find the libraries, it is often needed to set up the CMAKE_PREFIX_PATH environment variable so it points to the directories where dependencies of your project are installed.
  2. Set you PATH environment varible so you can invoke you compiler and make just by calling by calling eg. make.I think you need to do than manually for Mingw32, for Visual Studio you can use the "Visual Studio Command Propt" which has these variables already set.
  3. Run CMake with desired generator. To select the generator from command line use the -G switch. You will probably use one of the following (the ... means other options you want to pass to cmake)

    For GNU make used in MinGW use cmake -G "MinGW Makefiles" ...

    For NMake from visual studio use cmake -G "NMake Makefiles" ...

    It is also possible to create a Visual Studio project but I do not recommend it, since it quite difficult to set up automatic builds then. I also had some problems with dependencies when I tried to use VS project.

  4. change directory to your build directory (ie. the one where you called cmake, it contains the CMakeCache file) and run makeor nmake
  1. 为了让 CMake 找到库,通常需要设置 CMAKE_PREFIX_PATH 环境变量,以便它指向安装项目依赖项的目录。
  2. 设置您的 PATH 环境变量,以便您可以调用编译器并通过调用例如调用来进行编译。make.我认为您需要为 Mingw32 手动执行操作,对于 Visual Studio,您可以使用已经设置了这些变量的“Visual Studio Command Propt”。
  3. 使用所需的生成器运行 CMake。要从命令行选择生成器,请使用 -G 开关。您可能会使用以下其中一种(... 表示您想传递给 cmake 的其他选项)

    对于在 MinGW 中使用的 GNU make 使用 cmake -G "MinGW Makefiles" ...

    对于来自 Visual Studio 的 NMake 使用 cmake -G "NMake Makefiles" ...

    也可以创建 Visual Studio 项目,但我不推荐它,因为那时设置自动构建非常困难。当我尝试使用 VS 项目时,我也遇到了一些依赖问题。

  4. 将目录更改为您的构建目录(即您调用的目录cmake,它包含 CMakeCache 文件)并运行makenmake

回答by Vimieiro

Quoting from "CMake support in Visual Studio":

引用“ Visual Studio 中的 CMake 支持”:

Visual Studio 2017 introduces built-in support for handling CMake projects. This makes it a lot simpler to develop C++ projects built with CMake without the need to generate VS projects and solutions from the command line. This post gives you an overview of the CMake support, how to easily get started and stay productive in Visual Studio.

Visual Studio 2017 引入了对处理 CMake 项目的内置支持。这使得开发使用 CMake 构建的 C++ 项目变得更加简单,而无需从命令行生成 VS 项目和解决方案。这篇博文概述了 CMake 支持,以及如何在 Visual Studio 中轻松入门并保持高效。