Using cmake on windows for c++

2020-05-19 02:19发布

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.

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?

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!

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.

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):

  • 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.

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?

标签: c++ cmake
4条回答
迷人小祖宗
2楼-- · 2020-05-19 02:46

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.

  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 make or nmake
查看更多
▲ chillily
3楼-- · 2020-05-19 02:47

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.

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.

查看更多
Anthone
4楼-- · 2020-05-19 02:49

Quoting from "CMake support in Visual Studio":

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.

查看更多
Rolldiameter
5楼-- · 2020-05-19 02:55

if you run cmake by command:

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

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

msbuild Project.sln

either if you run cmake:

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

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

nmake
查看更多
登录 后发表回答