Generating CMakeLists.txt from Visual Studio - c++

I have used several C++ libraries that ship with CMakeLists.txt for building them on various platforms. It works well in most cases. My primary development environment is Windows. However on Windows using Visual Studio, I want to build cross-platform C++ libraries that I can then package and ship for multiple platforms. Is there anyway (some tool etc.) using which I can generate CMake configuration file (CMakeList.txt) for my Visual Studio C++ Library project?

vcproj2cmake seems to be the answer: http://sourceforge.net/projects/vcproj2cmake/

Related

What to do in the absence of a .sln file?

I'm just starting in the world of C++. I was kind of forced into this because I want to study a Physics simulation. With a lot of effort, I found out that I needed to install Microsoft Visual Studio on my computer in order to edit and compile the source files that are available on GitHub.
However I noticed that some of the authors on GitHub do not include .sln files in their projects, and then I'm unable to open/edit/compile them with Microsoft Visual Studio. Conversely, those projects containing an .sln file and that are meant to run on DirectX are the ones that I was able to compile/execute after making my own modifications.
So... What is lacking in those projects that do not contain an .sln file or that are not meant for DirectX?
There are numerous build systems in the world, but for the Windows ecosystem there are a few dominate ones.
MSBuild (Microsoft Build) is the default build system for Visual Studio. .vcxproj files are the Visual C++ project files that use MSBuild, and the .sln files are meta-files that organize one or more .vcxproj files (or .csproj files for C#).
NMake (Microsoft Program Maintenance Utility) is the older 'makefile' build system from Microsoft. It's still around, but has not been updated in many years. Visual Studio can also support nmake-based .sln/.vcxproj files, but this has fallen out of favor over time and is all-in-all fairly clunky.
MinGW (Minimalist GNU for Windows) has its own 'makefile' build system.
CMake is a cross-platform 'meta-make' solution that is used by many platforms including Windows. It can target MSBuild, Ninja, MinGW Makefiles, etc. and a number of other build-systems which is why it's a popular open source and multi-platform solution. Visual Studio and Visual Code both support building with this build system, and you can of course use it from the command-line directly if you want. The presence of a CMakeList.txt file indicates this.
To provide some of the functionality of a '.sln' file for CMake, CMake 3.20 or later supports a CMakePresets.json file which makes it much easier to use within the Visual Studio and Visual Code IDEs.
See Microsoft Docs for more in Visual Studio build. For more on CMake with Visual Studio, see this article.
For general background on 'makefiles', see Wikipedia.
My list above is far from complete as there are numerous build systems out there (SharpMake, Meson, FASTbuild, etc.). For a good overview, see this blog post.
Microsoft uses .sln files as a way for Visual Studio to manage projects (files, compilation rules, dependencies, etc.), so it is Microsoft-specific.
Most of C++ projects are not written using Visual Studio however, but use other editors & build systems. Projects that do not have a .sln file, could have CMakeLists.txt files that can be compiled with CMake (which Visual Studio got support for in recent version), or meson.build files to be compiled with meson, Makefile for make, you get the point.
There are a lot of build systems & IDEs for C++ because it existed for very long time, and because it is a very popular language (for a good reason) so people created tools that suit their workflow. However, there should not be any differences in code you write (given it conforms to a standard version of C++) regardless of the build system or IDE you choose to use.

CMake generator in Visual Studio Code Windows 10 [duplicate]

I read the documentation.
It says:
A CMake Generator is responsible for writing the input files for a native build system.
What exactly does that mean?
If I have a set of C++ files in my project, are these the input files?
If I'm using Linux, what is my native build system by default? Make?
Why do the input files have to be written by the generator if they already exist?
What's a generator?
To understand what a generator is, we need to first look at what is a build system. CMake doesn't compile or link any source files. It used a generator to create configuration files for a build system. The build system uses those files to compile and link source code files.
So what's a build system?
A build system is a broad term that groups together a set of tools used to generally compile and link source code, but it can also include auxiliary tools used during a build process.
For example, in a multi-stage build system, one executable might be built to be used in the build process of another build.
Depending on the tool chain used on a system, CMake will generate multiple files and folders to allow the building of the source files referenced in the CMakeLists.txt and supporting .cmake files.
Sometimes multiple build systems may be installed on a computer, like for Windows you could have a Visual Studio and MinGW build system. CMake allows you to specify which if these build systems to generate configuration files for.
CMake includes a number of Command-Line, IDE, and Extra generators.
Command-Line Build Tool Generators
These generators are for command-line build tools, like Make and Ninja. The chosen tool chain must be configured prior to generating the build system with CMake.
The following are supported(**):
Makefile Generators
Borland Makefiles
MSYS Makefiles
MinGW Makefiles
NMake Makefiles
NMake Makefiles JOM
Unix Makefiles
Watcom WMake
Ninja Generators
Ninja
Ninja Multi-Config
IDE Build Tool Generators
These generators are for Integrated Development Environments that include their own compiler. Examples are Visual Studio and Xcode which include a compiler natively.
The following are supported(**):
Visual Studio 6
Visual Studio 7
Visual Studio 7 .NET 2003
Visual Studio 8 2005
Visual Studio 9 2008
Visual Studio 10 2010
Visual Studio 11 2012
Visual Studio 12 2013
Visual Studio 14 2015
Visual Studio 15 2017
Visual Studio 16 2019
Visual Studio 17 2022
Green Hills MULTI
Xcode
Extra Generators
These are generators that create a configuration to work with an alternative IDE tool and must be included with either an IDE or Command-Line generator.
The following are supported(**):
CodeBlocks
CodeLite
Eclipse CDT4
KDevelop3 (Unsupported after v3.10.3)
Kate
Sublime Text 2
If I have a set of C++ files in my project, are these the input files?
Yes, they are some of the input files. For a make build system you also have a MakeFile. For Visual Studio you have a solution file (.sln). With both systems there are additional files needed that CMake knows how to create given a proper CMakeLists.txt file.
If I'm using Linux, what is my native build system by default? Make?
Generally, yes, but other build systems could be setup like Ninja.
Why do the input files have to be written by the generator if they already exist?
Some source files may already exist, but CMake has the ability to generate header and source files. Also as mentioned above, there are configuration files that must be generated that depend on the source files supplied in the CMakeLists.txt file.
** According to the documentation for CMake Version 3.9 & 3.15 & 3.25
Maybe a picture is worth a thousand words.
A CMake Generator is responsible for writing the input files for a
native build system.
means that CMake prepares build scripts for a native build system when no generator is specified. In Linux the default build system is Make and its input file are makefiles, which are then interpreted and a build is executed accordingly. Before the first execution of CMake build scripts do not exist.
C++ source files (or any other source files) are not input files to a build system. Build system scripts specify how to handle source file in order to produce binary executables.
As far as I know, the standard native build system in Unix is GNU Make (gmake) known as "make".
The Google guys/gals also released a different tool called Ninja.

how to compile cmake on linux so that it can generate vsproj files?

I would like to generate visual studio project files on linux using cmake.
Unfortunately, the visual studio project file generator is not enabled by default on linux.
The cmake build instructions don't mention how to enable this feature on linux. It seems like the generators are enabled based on the host platform cmake is being build. Has anybody a patched cmake to enable the Visual Studio Project Generator on linux ?
We have tools that work on linux and windows for analyzing projects by parsing .sln/.vcxproj files. However generating .sln/.vcxproj files on windows is quite inconvenient as our main development is done on linux/osx.
You cannot generate Visual Studio Solutions under Linux. Nowadays you can generate Visual Studio solutions via ssh to a Windows docker container.

How to use g2o framework for Graph Optimization (SLAM) on windows?

I'm working on a graph slam project and i want to use g2o framework (https://github.com/RainerKuemmerle/g2o) de develop the application using visual studio 2010. This framework works primarly on linux but it can be used for windows as well.
Could any one tell me how to use with visual studio
Thank you,
Regards
You can use it in Windows because all requirements have a port to Visual Studio.
cmake http://www.cmake.org/
Eigen3 http://eigen.tuxfamily.org
suitesparse http://www.cise.ufl.edu/research/sparse/SuiteSparse/
Qt4 http://qt.nokia.com/
libQGLViewer http://www.libqglviewer.com/
Our primary development platform is Linux. Experimental support for
Mac OS X and Windows (MinGW or MSVC). We recommend a so-called out of
source build which can be achieved by the following command sequence.(1)
Use cmake to build it.
I remember having some issues with building g2o on Windows. But the CMake should work well. Just configure, generate, and you can open the solution with Visual Studio and build. If it says "Configuring Done", it should compile. Eigen3 is sometimes tough to find for CMake, as their find script is not officially distributed with CMake (yet).
You can run (assuming you are in g2o/trunk/build folder, hence ..):
cmake .. -DCMAKE_MODULE_PATH=/path/to/a/dir/containing_the_FindEigen3.cmake_file/
There are also other libraries that solve graph problems efficiently, such as SLAM++, iSAM or GTSAM, they compile on Windows with less problems.

Is it possible to create WinRT project with cmake?

To create WINRT project we need to have CMake >= 2.8.10 and:
Set project type to be Windows Store App(CMAKE: SET_TARGET_PROPERTIES(target PROPERTIES VS_WINRT_EXTENSIONS TRUE)
Add compiler flag /ZW for WinRT compilation(CMAKE: ADD_DEFINITIONS(-ZW))
Add App.xaml.h, App.xaml.cpp
Add App.xaml so VS will generate some additional files from it(CMAKE: HOW?)
Add Package.appxmanifest with description so VS will use it(CMAKE: HOW?)
Thank you.
UPDATE: CMake bug report referring to this question is here 0013749: Cannot target Windows 8 RT from CMake without workarounds.
CMake guys are working on it:
Bug report at cmake.org
This is perfectly doable. Things have considerably changed since this question was initially asked. The most significant changes are:
Visual Studio natively supports CMake since Visual Studio 2017 (see CMake support in Visual Studio). It's not longer required to separately download CMake, and have it generate .sln and .vcxproj files. CMake is part of the Visual Studio installation, and can be parsed by Visual Studio's project management infrastructure and build system.
C++/CX is no longer the only way to produce and consume Windows Runtime components. C++/WinRT provides a standard C++ language projection of the Windows Runtime API surface, with the ability to consume and produce Windows Runtime components.
With these changes you can use CMake to create a C++ project that builds a Windows Runtime application. C++/WinRT is standard C++, and Visual Studio 2017 provides built-in CMake support.