Building a win32 app with CMake, Ninja, and MSVC - windows

I use CMake to build win32 apps by using its Visual Studio 16 2019 generator and passing the following options:
cmake -G "Visual Studio 16 2019" -A Win32
I'd like to switch the Visual Studio 16 2019 generator with Ninja to build the same win32 app. However, CMake's Ninja generator does not offer the same high-level functionality as CMake's Visual Studio generators.
So, does anyone know how to configure Ninja to build win32 apps with the MSVC compiler?

Related

Why `msvc_x86` toolset in Visual Studio CMake Settings passes `-A x64` to CMake instead of `-A Win32`?

I'm using Visual Studio 2022 to build a project using CMake.
It builds successfully for x86_64 architecture.
However, when I did the following:
Added CMake configuration x86-Release , from CMake Settings
Selected Release in Configuration Type
Left msvc_x86 selected as a Toolset
Selected Visual Studio 17 2022 Win64
Ran from the menu: Project -> Configure Cache
I got the configuration done for x86_64 instead of x86:
Command line: "C:\WINDOWS\system32\cmd.exe" /c "%SYSTEMROOT%\System32\chcp.com 65001 >NUL && "C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe" -G "Visual Studio 17 2022" -A x64 -DCMAKE_CONFIGURATION_TYPES:STRING="Release" -DCMAKE_INSTALL_PREFIX:PATH="F:\Work\conan\crono_pci_windows\tools\out\install\x86-Release" "F:\Work\conan\crono_pci_windows\tools" 2>&1"1>
While I expected the command line to have -A Win32 instead.
Any idea how to set it to build for x86 (-A Win32)?
Thanks,
Bassem
I found a solution, it's by selecting a 32-bit CMake generator: from the list of available generators, e.g. Visual Studio 17 2022 instead of Visual Studio 17 2022 Win64.
Selecting x86-Release doesn't seem to be enough.

CMake microsoft visual studio + gcc

I'm using cmake to build the project.
There is a CMakeLists where is used to build the project.
So, when I create a build folder, and use
cmake ..
the project solution is created, but with MVSC c++ compiler.
Is it possible to generate a solution with GCC compiler but in a Visual studio solution?

How to detect Visual Studio LLVM toolset in CMakeLists?

How can I detect in CMakeLists.txt, that Visual Studio Clang toolset is used, as opposed to clang on Linux? When Visual Studio Clang toolset is used, I need to specify extra flags specific to Visual Studio headers (such as _CRT_SECURE_NO_WARNINGS).
Further info:
The toolset is Visual Studio Community 2019 Preview, version 16.10.0 Preview 3.0 (with C++ Clang Tools for Windows 10.0.0).
To build I am using Visual Studio Codium 1.56.2 with plugins CMake 0.0.17 and CMake Tools 1.7.2.
The VSCode CMake plugin autodetects Visual Studio Clang toolset, and when I select it, it builds with following command:
D:\dev_tools\cmake\bin\cmake.EXE --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE "-DCMAKE_C_COMPILER:FILEPATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\Llvm\x64\bin\clang.exe" "-DCMAKE_CXX_COMPILER:FILEPATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\Llvm\x64\bin\clang.exe" -Hd:/dev/exemine -Bd:/dev/exemine/build -G Ninja
The build works correctly. I just need to detect when this toolset is used from CMakeLists.txt and specify few extra options.
Note: As a workaround, I am using following code:
if(MSVC OR (CMAKE_CXX_COMPILER MATCHES "clang.exe$"))
add_compile_definitions(WIN32)
...

CMake: how to specify the version of Visual C++ to work with?

I have multiple versions of Visual Studio installed (2010, 2012, 2015 trial).
How can I force CMake to generate the makefiles for a specific VS version? By default it generates for VS2015.
First you can check what generators your CMake version does support (and how they are named):
> cmake.exe --help
...
The following generators are available on this platform:
...
Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
Optional [arch] can be "Win64" or "ARM".
...
Then you can give the generator with
cmake.exe -G "Visual Studio 11" .. (short name)
cmake.exe -G "Visual Studio 11 2012" .. (full name)
I prefer the later, because of its clarity. And I normally have this call in a build script wrapper:
#ECHO off
IF NOT EXIST "BuildDir\*.sln" (
cmake -H"." -B"BuildDir" -G"Visual Studio 11 2012"
)
cmake --build "BuildDir" --target "ALL_BUILD" --config "Release"
The full name is transferred to an internal cached CMake variable name CMAKE_GENERATOR. So the above calls are equivalent to
cmake -DCMAKE_GENERATOR="Visual Studio 11 2012" ..
This gives us an interesting possibility. If you place a file called PreLoad.cmake parallel to your main CMakeLists.txt file you can force the default (if available) to take for your project there
cmake.exe ..
PreLoad.cmake
if (NOT "$ENV{VS110COMNTOOLS}" STREQUAL "")
set(CMAKE_GENERATOR "Visual Studio 11 2012" CACHE INTERNAL "Name of generator.")
endif()
Sometimes you may need to add also -T <toolset-name> or -A <platform-name> option:
cmake.exe -G "Visual Studio 10" -T "v90" ..
And last but not least if you are really only interested in the compiler
"\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat"
cmake.exe -G "NMake Makefiles" ..
References
CMake command line
What is the default generator for CMake in Windows?
CMake: In which Order are Files parsed (Cache, Toolchain, …)?
How can I generate a Visual Studio 2012 project targeting Windows XP with CMake?
cmake -G "Visual Studio 12" ..\MyProject

Compiling Qt using visual studio command prompt

if I follow this link
and build qt libraries using visual studio command prompt does the output binaries is using mingw or microsoft c++ compiler
The resulting binaries should be used with the visual studio compiler.
This process seems unnecessary since Qt has posted built binaries for the latest versions of Qt built for VS 2010:
http://qt-project.org/downloads
Qt libraries 4.8.3 for Windows (VS 2010, 235 MB)

Resources