Is it possible to exclude a directory from all cmake? [duplicate] - makefile

I have Mingw64 GCC 6.3.0 (always in PATH) and Visual C++ compiler tools from Visual Studio 2017 RTM (not in PATH).
If I run cmake . -G "MinGW Makefiles", GCC 6.3.0 will be selected.
If I run cmake . -G "Ninja", GCC 6.3.0 will be selected.
My Visual C++ compiler tools is none standard, I only keep the parts I need and delete the rest (like MSBuild, IDE etc.). I use my own batch script to set up PATH, INCLUDE and LIB (works just fine).
If I run this batch script and run cmake ., MSVC will be selected and build with NMake.
With the same environment, cmake . -G "Ninja", GCC 6.3.0 is selected instead of MSVC.
So my question is, how to tell CMake I want to use MSVC + Ninja rather than GCC + Ninja when both are in PATH? Any environment variable I should set?

You can also use the inverted approach and exclude all compilers you don't want with CMAKE_IGNORE_PATH. It takes a list of paths to ignore, but be aware that it needs to be an exact string match. The advantage would be that you can declare those directly from the command line.
So what I did is to take the path from the compiler found but "not to be taken" into CMAKE_IGNORE_PATH.
And on my system there were actually three GCC compilers in my PATH (just make sure to start from an empty binary output directory with each try):
> cmake -G"Ninja" ..
...
-- Check for working C compiler: C:/MinGW/bin/cc.exe
...
> cmake -DCMAKE_IGNORE_PATH="C:/MinGW/bin" -G"Ninja" ..
...
-- Check for working C compiler: C:/Strawberry/c/bin/gcc.exe
...
> cmake -DCMAKE_IGNORE_PATH="C:/MinGW/bin;C:/Strawberry/c/bin" -G"Ninja" ..
...
-- Check for working C compiler: C:/Program Files (x86)/LLVM/bin/clang.exe
...
> cmake -DCMAKE_IGNORE_PATH="C:/MinGW/bin;C:/Strawberry/c/bin;C:/Program Files (x86)/LLVM/bin" -G"Ninja" ..
...
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe
...

use a toolchain file
set(CMAKE_C_COMPILER cl.exe)
set(CMAKE_CXX_COMPILER cl.exe)
then build your cmake project with with -DCMAKE_TOOLCHAIN_FILE=toolchainfile

Related

CMake toolset specification seems to be ignored

I'm trying to compile instant-ngp, but there are bugs building on Windows that require you to force MSVC to 14.25 (Documented in this GitHub issue.)
I'm trying to override this with -T version=14.25 or -DCMAKE_GENERATOR_TOOLSET=version=14.25, but they just don't work.
> cmake . -B build -T 'version=14.25'
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- The C compiler identification is MSVC 19.29.30146.0
-- The CXX compiler identification is MSVC 19.29.30146.0
...
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
...
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
...
And it definitely builds with the 14.29 compiler.
14.25 is installed, and I know CMake can see it because if I try a version like 14.24 I get an error message about that not being installed, but 14.25 works.
How do I make CMake respect this setting instead of instantly ignoring it?

How to configure CMakeLists to use Windows command line compiler instead of Visual Studio

I would like to convert my project from a Visual Studio solution to build with CMake and compile it with Makefiles.
This is a 2-part question.
Right now the CMakeLists.txt is:
cmake_minimum_required(VERSION 3.13.0)
project(Project2015 CXX)
add_executable(Project Source/main.cpp)
When I run cmake .. out of the build directory, it generates *.vcxproj and *.sln files, but there is no Makefile. How can I change the CMakeLists file to generate a Makefile?
What is the command line equivalent compiler to gcc for windows? And how do I set this compiler as the target for CMake and the generated Makefile?
Reading about the build tools https://learn.microsoft.com/en-us/cpp/build/walkthrough-compile-a-c-program-on-the-command-line?view=vs-2019
Do I need to target the cl.exe compiler? Would this work with CMake and Makefiles?
I'm reading online that these command line flags will set the compiler, how can I add these to the CMakeLists.txt to be used automatically?
DCMAKE_C_COMPILER=cl
DCMAKE_C_COMPILER_FORCED=ON
DCMAKE_CXX_COMPILER=cl
DCMAKE_CXX_COMPILER_FORCED=ON
DCMAKE_BUILD_TYPE=Debug
DCMAKE_INSTALL_PREFIX=%CFITSIO_DIR%
G"NMake Makefiles"
You should use the build tool mode of CMake for builds from the command line.
After configuring your project for a 64bit build using Visual Studio 2019 e.g. with
cmake -S <sourcedir> -B <builddir> -G "Visual Studio 16 2019" -A x64
you would run
cmake --build <builddir> --target ALL_BUILD --config Release
For further options see here for an almost quiet build from the command line see here.
As suggested by #vre, you can run everything from the command line, while still using the Visual Studio generator. Just use CMake's command line build tools:
cmake ..
cmake --build . --config Release
This way, you don't have to open Visual Studio at all to build your libraries/executables.
Another option is to use Microsoft's nmake utility, which will generate NMake Makefiles. You can tell CMake to use this generator instead using this:
cmake -G"NMake Makefiles" ..
The full list of CMake generators you can choose from is listed here.
If you don't want to manually set the CMake generator in the command line, you can set it at the top of your CMakeLists.txt file:
set (CMAKE_GENERATOR "NMake Makefiles" CACHE INTERNAL "" FORCE)
It will be used on the second CMake configuration in this case, as the first run will use the system default generator. If you want CMake to use it on the first configuration, you can utilize the Preload.cmake procedure outlined in this answer.

cmake fails to pick up clang++ and always use visual studio instead on Windows

I have visual studio 2017 and clang both installed in a 64-bit Windows 10 PC.
clang version 6.0.0 (tags/RELEASE_600/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
I have tried running cmake with clang using:
-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
but cmake always pick up visual studio instead of clang as the c++ compiler.
-- Building for: Visual Studio 15 2017
-- The C compiler identification is MSVC 19.13.26131.1
-- The CXX compiler identification is MSVC 19.13.26131.
Is there a way to ONLY use clang as my c++ compiler in cmake without other tools like visual studio?
If you wish to use VisualStudio 201x with clang/c2 compiler, forgot about linux/mac approach (it works for mingw/msys etc.). There is no need to use these CMAKE_C??_COMPILER variables.
Try e.g.
cd build
cmake -G"Visual Studio 15 2017" -T "v141_clang_c2" <SRC_DIR>
I list of available toolsets can be observed when you open any C++ project and then look to Propertiese menu -> General: "Platform toolsets" combobox-list
Or using LLVM snapshot build the page define toolset names to be used in above command e.g. LLVM-vs2013.
sample output:
C:\work\build>cmake -G"Visual Studio 15 2017" -T"v141_clang_c2" <SRC>
-- The C compiler identification is Clang 3.8.0
-- The CXX compiler identification is Clang 3.8.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/ClangC2/14.10.25903/bin/HostX86/clang.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/ClangC2/14.10.25903/bin/HostX86/clang.exe -- works
-- Detecting C compiler ABI info
....

Can't compile restbed on Windows 7

I'm a the end with 16 hours of configure, installing, deleting, modifying and hit my keyboard many times....
I want to use restbed for plattform independent C++ programming, but I can't get build this stuff. I have Cygwin (download at 2017/04/24) installed (think for git or whatever creepy things), Code::Blocks with MinGW (16.01) and at least a seperate MinGW (also downloaded 2017/04/24) installation. Also I've Visual Studio 2012 Pro, 2015 and 2017 (long story) on a Windows 7 x64 Pro.
This is what I try and the results:
cloning recursivly with git in empty directory and following the instructions.
cmake -DBUILD_TESTS=YES -DBUILD_EXAMPLES=YES -DBUILD_SSL=NO -DBUILD_SHARED=YES ..
Uhh.. success. I seems it using the gcc/g++ from Cygwin.
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
:
:
-- Configuring done
-- Generating done
-- Build files have been written to: /cygdrive/d/Entwicklung/C++/restbed/restbed/build
Nice! Now follows:
make -j install
After 7% working, it gives a bunch of warnings and failed:
/cygdrive/{my_path_to}/restbed/restbed/dependency/asio/asio/include/asio/detail/config.hpp:755:5: warning: #warning Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. [-Wcpp]
# warning Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately.
^
/cygdrive/{my_path_to}/restbed/restbed/dependency/asio/asio/include/asio/detail/config.hpp:756:5: warning: #warning For example, add -D_WIN32_WINNT=0x0501 to the compiler command line. [-Wcpp]
# warning For example, add -D_WIN32_WINNT=0x0501 to the compiler command line.
^
/cygdrive/{my_path_to}/restbed/restbed/dependency/asio/asio/include/asio/detail/config.hpp:757:5: warning: #warning Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target). [-Wcpp]
# warning Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).
^
/cygdrive/{my_path_to}/restbed/restbed/dependency/asio/asio/include/asio/detail/config.hpp:781:5: error: #error You must add -D__USE_W32_SOCKETS to your compiler options.
I modifing config.hpp and add
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
The 'define bla' warnings dissapear, but now I should set '-D__USE_W32_SOCKETS'.
BUT WHERE?
After researching google I try to set a enviroment variable CPPFLAGS=-D__USE_W32_SOCKETS, but this change nothing!
OK, now I want give MinGW a chance, but how get I rid of this penetrant Cygwin, without deinstalling and get mess with other software?
Cool, should I set some enviroment variables....
Clean up the build-Directory and set CC and CXX as mentioned in cmake output.
SET CC=D:/MinGW/bin/gcc.exe
SET CXX=D:/MinGW/bin/g++.exe
Checking my PATH variable and adding ";D:\MinGW\mysys\1.0\bin;D:\MinGW\bin"
Now use cmake bla.. again...but... but... WHAT?
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is GNU 5.3.0
CMake Error at CMakeLists.txt:4 (project):
The CMAKE_C_COMPILER:
D:/MinGW/bin/gcc.exe
is not a full path and was not found in the PATH.
Tell CMake where to find the compiler by setting either the environment
variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
the compiler, or to the compiler name if it is in the PATH.
CMake Error at CMakeLists.txt:4 (project):
The CMAKE_CXX_COMPILER:
D:/MinGW/bin/g++.exe
is not a full path and was not found in the PATH.
Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
-- Configuring incomplete, errors occurred!
See also "/cygdrive/{my_path_to}/restbed/restbed/build/CMakeFiles/CMakeOutput.log".
With Cygwin cmake says, I use gcc/g++ in version GNU 5.4.0. On the other hand, it detects the correct MinGW version as 5.3.0, but can't find it? Hu?
What can I do to get this library work under Windows 7?
EDIT BEGIN
To build makefiles for Code::Blocks, the -G parameter doesn't work. I try some combinations, but always say:
cmake -DBUILD_TESTS=YES -DBUILD_EXAMPLES=YES -DBUILD_SSL=NO -DBUILD_SHARED=YES -G "CodeBlocks" ..
CMake Error: Could not create named generator CodeBlocks
Generators
Unix Makefiles = Generates standard UNIX makefiles.
Ninja = Generates build.ninja files.
CodeBlocks - Ninja = Generates CodeBlocks project files.
CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
:
:
cmake -DBUILD_TESTS=YES -DBUILD_EXAMPLES=YES -DBUILD_SSL=NO -DBUILD_SHARED=YES -G "CodeBlocks - Ninja" ..
CMake Error: CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
Now this (many censored words) thing want to know the cmake-make?
My fault.... don't think about, what I've installed.
"CodeBlocks - Unix Makefiles" works as expected for generating project file, but can't compile under Code::Blocks. I changed compiler for project in 'Build options' to Cygwin, but says:
Execution of '/usr/bin/make.exe -j8 -f "/cygdrive/d/{my_path_to}/restbed/restbed/build/Makefile" VERBOSE=1 all' in 'D:\{my_path_to}\restbed\restbed\build' failed.
...and nothing more. Think it has to do with "Unix Makefile" under Windows.
EDIT END
I'm out of knowledge what to do, what to set, to delete, to modify, configure, aaaargh.
My last hope is, that someone has the big hint, a good idea or a workaround.
Thanks in advance.
Restbed can build with Visual Studio 2015/2017, why not use the native toolchain for that platform?

Unable to specify the compiler with CMake

I have a problem with this CMakeLists.txt file:
cmake_minimum_required(VERSION 2.6)
SET(CMAKE_C_COMPILER C:/MinGW/bin/gcc)
SET(CMAKE_CXX_COMPILER C:/MinGW/bin/g++)
project(cmake_test)
add_executable(a.exe test.cpp)
Calling cmake with: cmake -G "MinGW Makefiles" , it fails with the following output:
c:\Users\pietro.mele\projects\tests\buildSystem_test\cmake_test>cmake -G "MinGW Makefiles" .
-- The C compiler identification is GNU 4.6.1
-- The CXX compiler identification is GNU 4.6.1
-- Check for working C compiler: C:/MinGW/bin/gcc
CMake Error: your C compiler: "C:/MinGW/bin/gcc" was not found. Please set CMAKE_C_COMPILER to a valid compiler path or name.
CMake Error: Internal CMake error, TryCompile configure of cmake failed
-- Check for working C compiler: C:/MinGW/bin/gcc -- broken
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:52 (MESSAGE):
The C compiler "C:/MinGW/bin/gcc" is not able to compile a simple test
program.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:10 (project)
CMake Error: your C compiler: "C:/MinGW/bin/gcc" was not found. Please set CMAKE_C_COMPILER to a valid compiler path or name.
CMake Error: your CXX compiler: "C:/MinGW/bin/g++" was not found. Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
-- Configuring incomplete, errors occurred!
However the gcc compiler is in C:/MinGW/bin/ and it works.
Any idea?
Platform:
Windows 7
MinGW/GCC 4.6
Never try to set the compiler in the CMakeLists.txt file.
See the CMake FAQ about how to use a different compiler:
https://gitlab.kitware.com/cmake/community/wikis/FAQ#how-do-i-use-a-different-compiler
(Note that you are attempting method #3 and the FAQ says "(avoid)"...)
We recommend avoiding the "in the CMakeLists" technique because there are problems with it when a different compiler was used for a first configure, and then the CMakeLists file changes to try setting a different compiler... And because the intent of a CMakeLists file should be to work with multiple compilers, according to the preference of the developer running CMake.
The best method is to set the environment variables CC and CXX before calling CMake for the very first time in a build tree.
After CMake detects what compilers to use, it saves them in the CMakeCache.txt file so that it can still generate proper build systems even if those variables disappear from the environment...
If you ever need to change compilers, you need to start with a fresh build tree.
I had similar problem as Pietro,
I am on Window 10 and using "Git Bash".
I tried to execute >>cmake -G "MinGW Makefiles", but I got the same error as Pietro.
Then, I tried >>cmake -G "MSYS Makefiles", but realized that I need to set my environment correctly.
Make sure set a path to C:\MinGW\msys\1.0\bin and check if you have gcc.exe there. If gcc.exe is not there then you have to run C:/MinGW/bin/mingw-get.exe and install gcc from MSYS.
After that it works fine for me
Using with FILEPATH option might work:
set(CMAKE_CXX_COMPILER:FILEPATH C:/MinGW/bin/gcc.exe)
I had the same issue. And in my case the fix was pretty simple. The trick is to simply add the ".exe" to your compilers path. So, instead of :
SET(CMAKE_C_COMPILER C:/MinGW/bin/gcc)
It should be
SET(CMAKE_C_COMPILER C:/MinGW/bin/gcc.exe)
The same applies for g++.

Resources