I need to build my CMake based project under MSVC 2013 and MSVC 2019.
With MSVC 2019 using Ninja generator I build it successfully with following commands:
cmake -S . -B build -GNinja "-DCMAKE_BUILD_TYPE:STRING=Release"
cmake --build build --target all
On MSVC 2013 I have no Ninja available, so I tried the following:
cmake -S . -B build -DCMAKE_BUILD_TYPE:STRING=Release
cmake --build build --target all
Anyway I am getting following error and nothing is built:
MSBUILD : error MSB1009: Project file does not exist.
Switch: all.vcxproj
Any idea how to build it without ninja? (I cannot install it, since I am building it on a build server.)
In contrast to other generators (like Makefiles or Ninja) CMake does not generate an all target for Visual Studio solution but an ALL_BUILD target.
So cmake --build build --target ALL_BUILD --config Release should succeed.
Related
One way to get cmake to build x86 on Windows with Visual Studio is like so:
Start Visual Studio Command prompt for x86
Run cmake: cmake -G "NMake Makefiles" \path_to_source\
nmake
One way to get cmake to build x64 on Windows with Visual Studio is like so:
Start Visual Studio Command prompt for x64
Run cmake: cmake -G "NMake Makefiles" \path_to_source\
nmake
Using Cmake, how do I compile either or both architectures? (like how Visual Studio does it from in the IDE)
This cannot be done with CMake. You have to generate two separate build folders. One for the x86 NMake build and one for the x64 NMake build. You cannot generate a single Visual Studio project covering both architectures with CMake, either.
To build Visual Studio projects from the command line for both 32-bit and 64-bit without starting a Visual Studio command prompt, use the regular Visual Studio generators.
For CMake 3.13 or newer, run the following commands:
cmake -G "Visual Studio 17 2022" -A Win32 -S \path_to_source\ -B "build32"
cmake -G "Visual Studio 17 2022" -A x64 -S \path_to_source\ -B "build64"
cmake --build build32 --config Release
cmake --build build64 --config Release
For earlier versions of CMake, run the following commands:
mkdir build32 & pushd build32
cmake -G "Visual Studio 15 2017" \path_to_source\
popd
mkdir build64 & pushd build64
cmake -G "Visual Studio 15 2017 Win64" \path_to_source\
popd
cmake --build build32 --config Release
cmake --build build64 --config Release
CMake generated projects that use one of the Visual Studio generators can be built from the command line with using the option --build followed by the build directory. The --config option specifies the build configuration.
try use CMAKE_GENERATOR_PLATFORM
e.g.
// x86
cmake -DCMAKE_GENERATOR_PLATFORM=x86 .
// x64
cmake -DCMAKE_GENERATOR_PLATFORM=x64 .
Besides CMAKE_GENERATOR_PLATFORM variable, there is also the -A switch
cmake -G "Visual Studio 16 2019" -A Win32
cmake -G "Visual Studio 16 2019" -A x64
https://cmake.org/cmake/help/v3.16/generator/Visual%20Studio%2016%202019.html#platform-selection
-A <platform-name> = Specify platform name if supported by
generator.
I work on Windows 10. I use cmake with Visual Studio 15 2017 generator. CMakeLists.txt requires CMAKE_BUILD_TYPE to be defined because it is used by Conan command called from cmake (execute_process). Here is the cmake command:
cmake -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 15 2017" ..
When I run build command this way:
cmake --build .
I get the following error:
error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MTd_StaticDebug' in Tests.obj
Conan installs a release library (MT_StaticRelease) but CMAKE_BUILD_TYPE is ignored during build. Building works when I use the following command:
cmake --build . --config Release
How does it work ? When I have to define --config buildType ? What with other generators, for example Ninja ?
Visual Studio is a multi-configuration system. It means that the build type is selected at build time, lets say when you are using the IDE, you can select at that time the Release or Debug configuration and build. So when you are generating the Visual Studio solution (project generation or configure step), with cmake -G "Visual Studio ...", CMake doesnt have a value for CMAKE_BUILD_TYPE.
So the CMake invocations need to be different depending on the system:
In multi-configuration environments like Visual Studio, you use just 1 build folder:
$ mkdir build && cd build # assume we are in the folder containing CMakeLists.txt
$ cmake .. -G "Visual Studio 15 2017 Win64" # Unless building for 32 bits, add the Win64
$ cmake --build . --config Release # Or open IDE, change config to Release and build
$ cmake --build . --config Debug
In single-configuration environments like gcc with Makefiles, you need to use 1 folder for every configuration
$ mkdir build_release && cd build_release # assume we are in the folder containing CMakeLists.txt
$ cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
$ cmake --build .
$ cd .. && mkdir build_debug && cd build_debug
$ cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug
$ cmake --build .
A different issue is the definition of the MT (static) Visual Studio runtime. It is possible that you have defined in your Conan profile compiler.runtime=MT, or it is being set by your CMakeLists.txt directly. Conan doesn't install such static MT_StaticRelease library, it is part of Visual Studio. If you are trying to statically link the visual studio environment, your used profiles should look like:
# for debug
$ conan install .. -s build_type=Debug -s compiler.runtime=MTd
# for release
$ conan install .. -s build_type=Release -s compiler.runtime=MT
I'm trying to build this
https://github.com/patrikhuber/eos
but I'm having troubles.
The instructions are pretty simple, as it says on gitHub
To build:
git clone --recursive https://github.com/patrikhuber/eos.git
mkdir build && cd build # creates a build directory next to the 'eos' folder
cmake -G "<your favourite generator>" ../eos -DCMAKE_INSTALL_PREFIX=../install/
make && make install # or open the project file and build in an IDE like Visual Studio
I'm using "Ninja" as generator and it looks like the cmake part goes through successfully as I get
-- Configuring done
-- Generating done
-- Build files have been written to: /home/francesco/eos/build
That's where things stop "working" for me, or where I fail to understand what's next. Following the instructions, I type
make && make install
and I get this message
make: *** No targets specified and no makefile found. Stop.
I looked around for solutions but I don't really understand what I am supposed to do: I tried
./configure
but I'm getting
bash: ./configure: No such file or directory
Anyone can please help?
Thanks
It always depends on your CMake "Generator". The 'make' is linux/mingw tool/command. For VisualStudio you can use nmake or sln/proj generated stuff.
More reliable could be utilize CMake for building i.e. for "NMake Makefiles" generator:
cmake --build <build folder> --target install
or
cmake --build <build folder> --config release --target install
for VisualStudio generator
I had the same problem, and solved by tinkering with locations for cmake and make. Here's what I used:
cmake -DCMAKE_INSTALL_PREFIX=/usr/local
make
I believe /usr/local is the default location (see here)
I just cant figure out why I am getting this error, would anyone mind giving me some help?
I have added Cmake to the Environment variables, but am still getting an error. I'm currently a University Student trying to get SDL to work.
My machine is x64, I have Visual Studio 2017 with all C++ elements installed.
Here is the CMD error:
CMD Error
Here is the GUI error:
GUI Error
Copy - Paste CMD Version:
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:1 (PROJECT):
No CMAKE_C_COMPILER could be found.
CMake Error at CMakeLists.txt:1 (PROJECT):
No CMAKE_CXX_COMPILER could be found.
-- Configuring incomplete, errors occurred!
See also "C:/Users/Chris Ross/.conan/data/zlib/1.2.8/lasote/stable/build/6cc50b139b9c3d27b3e9042d5f5372d327b3a9f7/_build/CMakeFiles/CMakeOutput.log".
See also "C:/Users/Chris Ross/.conan/data/zlib/1.2.8/lasote/stable/build/6cc50b139b9c3d27b3e9042d5f5372d327b3a9f7/_build/CMakeFiles/CMakeError.log".
zlib/1.2.8#lasote/stable:
zlib/1.2.8#lasote/stable: ERROR: Package '6cc50b139b9c3d27b3e9042d5f5372d327b3a9f7' build failed
zlib/1.2.8#lasote/stable: WARN: Build folder C:\Users\Chris Ross\.conan\data\zlib\1.2.8\lasote\stable\build\6cc50b139b9c3d27b3e9042d5f5372d327b3a9f7
ERROR: zlib/1.2.8#lasote/stable: Error in build() method, line 60
self.run('%s && cmake .. %s' % (cd_build, cmake.command_line))
ConanException: Error 1 while executing cd _build && cmake .. -G "Visual Studio 15 2017 Win64" -DCONAN_LINK_RUNTIME="/MD" -DCONAN_EXPORTED="1" -DCONAN_COMPILER="Visual Studio" -DCONAN_COMPILER_VERSION="15" -DCONAN_CXX_FLAGS="/MP4" -DCONAN_C_FLAGS="/MP4" -Wno-dev
INFO: Conan job finished.
INFO: Starting cmake
CMake Error: Error: generator : Visual Studio 14 2015 Win64
Does not match the generator used previously: Visual Studio 15 2017 Win64
Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.
INFO: CMake job finished.
Copy - Paste GUI Version:
CMAKE_CONFIGURATION_TYPES Debug;Release;MinSizeRel;RelWithDebInfo
CMAKE_INSTALL_PREFIX C:/Program Files (x86)/lab1
Configuring incomplete, errors occurred!
See also "C:/Users/Chris Ross/Desktop/lab2template/bin/CMakeFiles/CMakeOutput.log".
See also "C:/Users/Chris Ross/Desktop/lab2template/bin/CMakeFiles/CMakeError.log".
My conanfile.txt:
[requires]
SDL2/2.0.5#dotfloat/stable
SDL2_image/2.0.1#lasote/stable
SDL2_mixer/2.0.1#a_teammate/testing
SDL2_ttf/2.0.14#hilborn/stable
[options]
SDL2:shared=True
SDL2_image:shared=False
SDL2_mixer:shared=True
SDL2_ttf:shared=False
[generators]
cmake
[imports]
bin, *.dll -> ./bin # Copies all dll files from packages bin folder to my "bin" folder
lib, *.dylib* -> ./bin # Cop
Using this .bat file:
#echo off
:start
echo INFO: I am running from '%cd%'
if exist src goto changetobin
:conan
echo INFO: Starting conan
conan install --build missing
echo INFO: Conan job finished.
goto cmake
:cmake
echo INFO: Starting cmake
cmake .. -G "Visual Studio 14 2015 Win64"
echo INFO: CMake job finished.
goto end
:changetobin
echo ERROR: I'm in the wrong directory.. moving into bin
cd bin
echo INFO: Retrying..
goto start
:end
if exist bin goto quit
cd ..
goto quit
:quit
echo.
echo.
echo INFO: Conan and CMake ran successfully. Open up your solution file (listed below) to open your project.
echo.
dir /b *.sln
(I have also attempted a manual build (conan install --build missing))
"My machine is x64, I have Visual Studio 2017 with all C++ elements installed."
You're generating a solution for the wrong version of Visual Studio..
cmake .. -G "Visual Studio 14 2015 Win64"
Change that line in your batch script to:
cmake .. -G "Visual Studio 15 2017 Win64"
Then you must remove all generated files (CMakeCache.txt', 'CMakeFiles dir, etc`) and try again. Please also double check that your conan profile settings match the compiler you are using.
I have a C++ project that builds using CMake. I usually build on OSX but now I am trying to get a Windows version working too. I would like to use Clang on Windows for compatibility reasons.
I installed the pre-compiled Clang 3.8 binary from LLVM:
C:\Program Files\LLVM\bin\clang.exe
C:\Program Files\LLVM\bin\clang++.exe
It is also installed on my PATH:
>clang++
clang++.exe: error: no input files
I have two questions:
How do I tell CMake to use clang++ when I call cmake --build?
How can I check before building which compiler CMake is configured with?
You also need - in addition to the Clang compilers itself - an build/link environment for Windows.
The latest CMake 3.6 builds do have several integrated supported Clang build environments on Windows (e.g. Visual Studio, Cygwin; see Release Notes).
I've just run a successful test with
LLVM-3.9.0-r273898-win32.exe from http://llvm.org/builds/
cmake-3.6.0-rc4-win64-x64.msi from https://cmake.org/download/
Microsoft VS2015 Community Edition Version 14.0.23107.0
All installed to their standard paths with their bin directories in the global PATH environment.
The part you need to know is setting the right toolset with the CMake -T"LLVM-vs2014" command line option. During the configuration process CMake will let you know which compiler it has found/taken.
CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(HelloWorld)
file(
WRITE main.cpp
"#include <iostream>\n"
"int main() { std::cout << \"Hello World!\" << std::endl; return 0; }"
)
add_executable(${PROJECT_NAME} main.cpp)
Windows Console
...> mkdir VS2015
...> cd VS2015
...\VS2015> cmake -G"Visual Studio 14 2015" -T"LLVM-vs2014" ..
-- The C compiler identification is Clang 3.9.0
-- The CXX compiler identification is Clang 3.9.0
-- Check for working C compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: .../VS2015
...\VS2015> cmake --build .
Microsoft (R)-Buildmodul, Version 14.0.23107.0
[...]
...\VS2015> Debug\HelloWorld.exe
Hello World!
Installation Hints
Please note that I have added LLVM to my search paths during setup:
And you can crosscheck the available "Platform Toolsets" in any VS project's property page:
References
What is the -D define to tell Cmake where to find nmake?
Linker for Clang?
Switching between GCC and Clang/LLVM using CMake
As an update for Visual Studio 2019 and 2022, you can install the clang-cl toolchain via the Visual Studio Installer and use this to generate a .sln file:
> mkdir build && cd build
> cmake .. -G "Visual Studio 16 2019" -T ClangCL -A x64
or
> mkdir build && cd build
> cmake .. -G "Visual Studio 17 2022" -T ClangCL -A x64
However a much better workflow is to simply open the folder containing the CMakeLists.txt file directly in VS and it will recognise it as a CMake project and provide the ability to set the compiler etc. directly.
The only drawback I could find to this approach is that the Visual Studio Graphics Debugger workflow doesn't work with it, which will affect you if you are developing with DirectX etc.
Follow these instructions:
Install choco if you don't have it: https://chocolatey.org/install
choco install ninja -y
choco install cmake -y
choco install llvm -y
Reset your shell so environment variables are set properly (you can check if bin folders for each are added to your Path).
Using Ninja
From PowerShell
$env:CC="C:\Program Files\LLVM\bin\clang.exe"
$env:CXX="C:\Program Files\LLVM\bin\clang++.exe"
cmake -S ./ -B ./build -G "Ninja-Multi-Config"
cmake --build ./build --config Release
From GUI
Go to your project and run:
cmake-gui .
From the upper menu select Tools/Configure and follow these settings:
Choose "Ninja Multi-Config" and Specify native compilers:
Give the path to the compilers:
Finally, run
cmake --build ./build --config Release
Using Visual Studio
Using GUI
In some folder install llvm-utils:
git clone https://github.com/zufuliu/llvm-utils.git
cd llvm-utils/VS2017
.\install.bat
Go to your project and run:
cmake-gui .
From the upper menu select Tools/Configure and follow these settings:
Choose Visual Studio 2019 and 2nd option (specify native compilers). Set
LLVM_v142 for Visual Studio 2019 and above. See here for older versions for others.
Give the path to the compilers:
Finally, run
cmake --build ./build --config Release