CMake: Generating batch file calling cl.exe - c++

CMake is a build system generator supporting different build systems and environments like make, visual studio, code-blocks, eclipse-cdt, etc...
To generate a project, you can use the -G option to specify the project generator.
Is there an option, are there any projects, or is it possible to write a generator that creates batch files calling cl.exe so I don't have to open visual studio every time?

CMake is able to invoke the build tool (MSBuild in this case) by itself, using the --build option. See this question for more details.

Related

Maintaining Makefiles and CMakeLists.txt

When you're working to very big projects with a large amount of people, maintaining both Makefiles and CMakeLists.txt could be very difficult. I know that CMakeLists.txt could generate Makefiles, but suppose that I want to compile my code by using make and in the same time, use an IDE to have a kind of intellisense. How can I achieve it?
In the scenario that I'm describing, CMakeLists.txt files will disappear, so I won't have the chance to generate solutions with them.
CMake is not a build manager!
It is a generator of files for other build managers!
So you can generate project for IDEs (Visual Studio, Xcode, CodeBlocks, ..) or generate MakeFile, or whatever you prefer and CMake supports. So I do not understand why do you maintain CMakeLists.txt and Makefiles at the same time.
Just maintain CMakeLists.txt and then generate respective Makefiles from it.
For details see CMake Documentation - generators:
Command-Line Build Tool Generators
These generators support command-line build tools. In order to use them, one must launch CMake from a command-line prompt whose environment is already configured for the chosen compiler and build tool.
Borland Makefiles
MSYS Makefiles
MinGW Makefiles
NMake Makefiles
NMake Makefiles JOM
Ninja
Unix Makefiles
Watcom WMake
Bottom line you should be able to generate files for IDE or Makefile for you favorite build manager without any complications.
In the scenario that I'm describing, CMakeLists.txt files will disappear, so I won't have the chance to generate solutions with them.
WAT? You are doing something very strange and most probably wrong and you didn't provide any details abut that.
Concerning intellisense I would say:
Visual Studio 2017 as built-in support for CMake, see announcement
CMake can generate a json file with all commands see CMAKE_EXPORT_COMPILE_COMMANDS
so you can use it to have smart clang based completion in vim
QtCreator as built-in support for CMake so you'll have completion too.
Extra tip at first: Do not try to mix both handmade make files and CMake over the whole time of the project.
The normal way is to create the make files using cmake. The extra of cmake is the cross platform and in your case the nativ support by some IDEs, eg. QtCreator, CLion(awaik). For a wide range of other IDE cmake can generate the project files.
Example Workflow using QtCreator:
Create a simple CMakeLists.txt, at least with the name of the project
Load this into QtCreator
Add source files, update the CMakeLists.txt from within QtCreator
Build from within QtCreator, repeat
But the same CMakeLists.txt will be used to create the make file on command line. Or on your build server or CI system.

Generate Embarcadero project files from CMake files

I have a C++ code base with a large number of projects. Usually I can use CMake to generate the required project files for QtCreator, XCode, Visual Studio, etc. Now I need to compile for Embarcadero, but it does not seem that CMake supports it.
Does CMake support Embarcadero? If not, is there maybe some set of projects that I could generate with CMake and then import into Embarcadero.
For a toy example it may work with the bcc and bcc32c, bcc64 is not supported at all.
Maybe the new "free" compiler will help to change that - the press release seems to imply this.
Personally, I would not put my hopes to high. So far, everything I have seen from the company Embarcadero was just a let down.
You can use CMake's "Borland Makefiles" generator and then use Embarcadero's clang based bcc32c to compile the project. Here's their blog post about it: https://community.embarcadero.com/blogs/entry/using-cmake-with-the-free-embarcadero-c-compiler
I didn't find a way to generate project files for their IDE yet, but I'm wondering if one could use one of the "Visual Studio" generators, then manually change the file extension (considering Embarcadero is using MSBuild). But that's just a wild guess.
According to the current CMake documentation on generators, no CMake does not produce Embarcadero project files. Instead, you could use one of the Makefile generators, and set your IDE to run cmake and make when you execute its "build" command.

Changing current cmake generator

How can I change current cmake generator without using Cmake GUI (I want to change standart generator to Visual Studio 12 2013 Win64)?
For those seeking the CMake GUI answer.
Using the GUI menu
Go to File->Delete Cache and then click Configure again.
A scenario where changing the generator is needed is that you are keeping the CMake GUI open and reusing the same directory (source and CMakeList.txt) to do simple examples or tutorials and deleting the build files before starting again, e.g. with out-of-source build it would be the entire build directory. This is not the standard use case for CMake but one that would be common among beginners.
You cannot reliably change the generator used for an output (= binary) directory once generation has already happened there once. You should start in a fresh output directory.
Once there, simply use CMake's -G command-line option:
cmake -G "Visual Studio 12 2013 Win64"

Using cmake on windows for c++

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?
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
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.
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.
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.
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.
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.
change directory to your build directory (ie. the one where you called cmake, it contains the CMakeCache file) and run make or nmake
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.

How do I use CMake?

I am trying to use CMake in order to compile opencv.
I am reading the tutorial but can't understand what is CMakeLists files and how is it connected to the gui of CMake?
Also couldn't understand what are makefiles, are they the same is CMakeLists?
And which file is it which I in the end open with visual-studio?
I don't know about Windows (never used it), but on a Linux system you just have to create a build directory (in the top source directory)
mkdir build-dir
go inside it
cd build-dir
then run cmake and point to the parent directory
cmake ..
and finally run make
make
Notice that make and cmake are different programs. cmake is a Makefile generator, and the make utility is governed by a Makefile textual file. See cmake & make wikipedia pages.
NB: On Windows, cmake might operate so could need to be used differently. You'll need to read the documentation (like I did for Linux)
CMake takes a CMakeList file, and outputs it to a platform-specific build format, e.g. a Makefile, Visual Studio, etc.
You run CMake on the CMakeList first. If you're on Visual Studio, you can then load the output project/solution.
Yes, cmake and make are different programs. cmake is (on Linux) a Makefile generator (and Makefile-s are the files driving the make utility). There are other Makefile generators (in particular configure and autoconf etc...). And you can find other build automation programs (e.g. ninja).
CMake (Cross platform make) is a build system generator. It doesn't build your source, instead, generates what a build system needs: the build scripts. Doing so you don't need to write or maintain platform specific build files. CMake uses relatively high level CMake language which usually written in CMakeLists.txt files. Your general workflow when consuming third party libraries usually boils down the following commands:
cmake -S thelibrary -B build
cmake --build build
cmake --install build
The first line known as configuration step, this generates the build files on your system. -S(ource) is the library source, and -B(uild) folder. CMake falls back to generate build according to your system. it will be MSBuild on Windows, GNU Makefiles on Linux. You can specify the build using -G(enerator) paramater, like:
cmake -G Ninja -S libSource -B build
end of the this step, generates build scripts, like Makefile, *.sln files etc. on build directory.
The second line invokes the actual build command, it's like invoking make on the build folder.
The third line install the library. If you're on Windows, you can quickly open generated project by, cmake --open build.
Now you can use the installed library on your project with configured by CMake, writing your own CMakeLists.txt file. To do so, you'll need to create a your target and find the package you installed using find_package command, which will export the library target names, and link them against your own target.
Cmake from Windows terminal:
mkdir build
cd build/
cmake ..
cmake --build . --config Release
./Release/main.exe
Regarding CMake 3.13.3, platform Windows, and IDE Visual Studio 2017, I suggest this guide. In brief I suggest:
1. Download cmake > unzip it > execute it.
2. As example download GLFW > unzip it > create inside folder Build.
3. In cmake Browse "Source" > Browse "Build" > Configure and Generate.
4. In Visual Studio 2017 Build your Solution.
5. Get the binaries.
Regards.