Changing compiler with cmake creates infinite loop - cmake

I try to change the compiler with cmake :
SET(CMAKE_C_COMPILER "/opt/rh/devtoolset-2/root/usr/bin/gcc")
SET(CMAKE_CXX_COMPILER "/opt/rh/devtoolset-2/root/usr/bin/g++")
I do that in the begining of my project just before calling the "PROJECT" command.
But I get an infinite loop when I call cmake, I have the following output:
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: /opt/rh/devtoolset-2/root/usr/bin/gcc
-- Check for working C compiler: /opt/rh/devtoolset-2/root/usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /opt/rh/devtoolset-2/root/usr/bin/g++
-- Check for working CXX compiler: /opt/rh/devtoolset-2/root/usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Doxygen: /usr/bin/doxygen (found version "1.6.1")
-- Looking for C++ include tut.h
-- Looking for C++ include tut.h - found
-- [STATUS] Found tut.h
-- Configuring done
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_CXX_COMPILER= /usr/bin/c++
CMAKE_CXX_COMPILER= /usr/bin/c++
So I have this message again and again in an infinite loop, even if I delete the cache before calling cmake...
EDIT : Solution is quite simple : You need to use cmake version 2.8.9 and the problem doesnt appear.

The reason may be that other cmake module changes the variable you set. So find the cmake code and put it before your command (SET(CMAKE_CXX_COMPILER "/opt/rh/devtoolset-2/root/usr/bin/g++")), it should be solved.
I come across the problem with pybind11 submodule as following
SET(CMAKE_C_COMPILER /usr/bin/gcc)
SET(CMAKE_CXX_COMPILER /usr/bin/g++)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -march=native ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -march=native")
add_subdirectory(pybind11)
The solution is changing it as:
add_subdirectory(pybind11)
SET(CMAKE_C_COMPILER /usr/bin/gcc)
SET(CMAKE_CXX_COMPILER /usr/bin/g++)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -march=native ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -march=native")

Since this was the first result on Google for me, I figured I'd offer an alternate solution to Quentin's.
If you call your cmake command with -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake, you can then put your CMAKE_C_COMPILER and CMAKE_CXX_COMPILER overrides in a new toolchain.cmake file and cmake will properly load the overrides without an infinite loop.
This solutions offers a fix for those who cannot easily upgrade their cmake (such as if you are a non-root and non-sudo user), those who simply want to avoid the effort involved in upgrading packages or managing additional packages, or those who do not want to bother their sysadmin. Additionally, the toolchain.cmake file can still execute logic to find the appropriate or latest devtoolset should you want it to dynamically choose it.
This solution will not work for those who will not or cannot add the -DCMAKE_TOOLCHAIN_FILE argument to the cmake call.

Solution is quite simple : You need to use cmake version 2.8.9 and the problem doesnt appear.

on cmake3.15 I found out that once you remove the CMakeCache file and redo cmake it is all good

Related

cmake cannot find an existing directory on mingw64 (msys2)

I am trying to compile a project under MSYS2 and CLANG64 environment.
I have previously compiled dependencies in /usr/local.
$ ls /usr/local/include
boost compat-5.3.c cryptopp lauxlib.h libmongoc-1.0 lua.hpp luajit.h mongocxx yaml-cpp
bsoncxx compat-5.3.h gtest libbson-1.0 lua.h luaconf.h lualib.h tsl
$ ls /usr/local/lib
cmake libboost_filesystem-mt-s-x64.a libbson-static-1.0.a libmongoc-1.0.dll.a
libboost_atomic-mt-s-x64.a libboost_program_options-mt-s-x64.a libbsoncxx-static.a libmongoc-static-1.0.a
libboost_atomic-mt-x64.a libboost_regex-mt-s-x64.a libcryptopp.a libmongocxx-static.a
libboost_chrono-mt-s-x64.a libboost_system-mt-s-x64.a libgtest.a libyaml-cpp.a
libboost_container-mt-s-x64.a libboost_thread-mt-s-x64.a libgtest_main.a pkgconfig
libboost_context-mt-s-x64.a libbson-1.0.dll.a liblua-compat.a
But when I create the project, I explicitly set the location of binaries with interface libraries as I don't want to rely on the find mechanism that has hurt me badly in the past - linking to unintended, old system libraries.
project(test)
cmake_minimum_required( VERSION 3.0 )
add_library( cryptopp STATIC IMPORTED GLOBAL )
set_target_properties( cryptopp PROPERTIES
IMPORTED_LOCATION "/usr/local/lib/libcryptopp.a"
INTERFACE_INCLUDE_DIRECTORIES "/usr/local/include"
INTERFACE_COMPILE_DEFINITIONS "HAVE_CRYPTOPP"
)
add_executable( test test.cpp )
target_link_libraries( test cryptopp )
This works perfect under all Linux distros - Redhat, Ubuntu, etc but fails in MSYS2.
However when I run cmake, I get an error stating that /usr/local/include does not exist.
$ cmake ..
-- Building for: Ninja
-- The C compiler identification is Clang 14.0.4
-- The CXX compiler identification is Clang 14.0.4
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: G:/msys64/clang64/bin/cc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: G:/msys64/clang64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
CMake Error in CMakeLists.txt:
Imported target "cryptopp" includes non-existent path
"/usr/local/include"
in its INTERFACE_INCLUDE_DIRECTORIES. Possible reasons include:
* The path was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and references files it does not
provide.
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
I just cannot figure out why this is happening. Any clues?
Maybe it's a Windows path issue. Try replacing /usr/local with the output of cygpath -m /usr/local.

Define compiler and linker in CMAKE

I would like to use CMAKE to compile a special piece of code in C99 with language extensions. Therefore I have to use a "custom" compiler and linker /your/path/to/compiler and /your/path/to/linker. How can I define the compiler and the linker command used by CMAKE?
EDIT:
I tried to define the compiler and linker as suggested by Equod:
set(CMAKE_C_COMPILER /your/path/to/compiler)
set(CMAKE_CUSTOM_LINKER /your/path/to/linker)
set(CMAKE_C_LINK_EXECUTABLE
"<CMAKE_CUSTOM_LINKER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
But CMAKE is still not taking it:
-- Building for: Visual Studio 15 2017
-- Selecting Windows SDK version 10.0.14393.0 to target Windows 10.0.18363.
-- The C compiler identification is MSVC 19.16.27040.0
-- The CXX compiler identification is MSVC 19.16.27040.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Programms/VisualStudio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Programms/VisualStudio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/your/path/to/source/build
What I forgot to mention before, I am working on a Windows machine and the executable of th compiler and linker is in the PATH.
EDIT:
Here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MyProject
VERSION 1.0
DESCRIPTION "This is MyProject"
LANGUAGES C
)
set(CMAKE_C_COMPILER my_compiler)
set(CMAKE_C_LINK_EXECUTABLE my_linker)
configure_file(include/myproject_config.h.in include/myproject_config.h)
set(HEADER_FILES include/main.h include/somefunc.h)
set(SOURCE_FILES src/main.c src/somefunc.c)
add_executable(MyProject ${HEADER_FILES} ${SOURCE_FILES})
target_include_directories(MyProject PUBLIC "${PROJECT_BINARY_DIR}" )
target_include_directories(MyProject PUBLIC "../include" )
P.S.: my_compiler and my_linker are in PATH of cmd and PowerShell.
EDIT:
I installed MinGW now. I have make in my PATH as well. I updated the CMakeLists.txt file above. The make command tries to compile the code now with:
my_compiler #CMakeFiles/MyProject.dir/includes_C.rsp -o CMakeFiles\MyProject.dir\src\main.c.obj -c "C:\mypath\main.c"
But this is not working because I need a command like:
my_compiler -I="../include" "C:\mypath\main.c"
What CMake commands do I need to configure such a behavior?
CMake as default C compiler uses CC environment variables. You can also specify a different compiler and linker setting CMake variables:
Compiler:
set(CMAKE_C_COMPILER /your/path/to/compiler)
Linker:
set(CMAKE_CUSTOM_LINKER /your/path/to/linker)
set(CMAKE_C_LINK_EXECUTABLE
"<CMAKE_CUSTOM_LINKER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
To better understand why linker settings are done in two steps I'd suggest taking a look at how those variables are managed internally by CMake:
CMake on github

Finding GLib with CMake: target_include_directories called with invalid arguments

I have this following (minimal) CMakeLists.txt supposed to find GLib via pkg-config and add the libs to the foo target:
cmake_minimum_required(VERSION 2.10 FATAL_ERROR)
project(foo)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLib REQUIRED glib-2.0)
add_executable(foo foo.cpp)
message(WARNING "libs:" ${GLIB_LIBRARIES})
message(WARNING "includes:" ${GLIB_INCLUDE_DIRS})
target_link_libraries(foo PUBLIC ${GLIB_LIBRARIES})
target_include_directories(foo PUBLIC ${GLIB_INCLUDE_DIRS})
No matter what I try, I get (note the Found glib-2.0 part):
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
-- Checking for module 'glib-2.0'
-- Found glib-2.0, version 2.56.1
CMake Warning at CMakeLists.txt:6 (message):
libs:
CMake Warning at CMakeLists.txt:7 (message):
includes:
CMake Error at CMakeLists.txt:9 (target_include_directories):
target_include_directories called with invalid arguments
-- Configuring incomplete, errors occurred!
See also "/tmp/aaa/CMakeFiles/CMakeOutput.log".
and I cannot see, reading the CMake reference what arguments are invalid (note: this question is different from cmake target_include_directories called with invalid arguments). I also looked at CMake's FindPkgConfig documentation which gives glib as an example and I am not able to reproduce it (${GLIB_VERSION}). I tried GLIB_ and GLIB2_ prefixes and all I get is empty strings.
The messages show the variables are empty, though pkg-config reports values correctly:
$ pkg-config glib-2.0 --cflags --libs
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0
The CMake version I have is 2.10.
Can someone shed light on the issue?
I think your problem is the casing of GLIB_INCLUDE_DIRS and GLIB_LIBRARIES. They should be GLib_INCLUDE_DIRS and GLib_LIBRARIES since you specified "GLib" as the first argument of pkg_check_modules. I'm guessing that the target_include_directories() doesn't like not getting any arguments after the
PUBLIC (although it works for me in cmake 3.5).

CMake is able to cross compile on 3rd invocation

I've to cross compile a 3rd party library which uses CMake.
In order to build something, I need to pass "-B path" flag to compiler.
foo.c :
void main(){}
Build command :
cross-gcc -B /path/to/somewhere -o foo.o foo.c
Without "-B path" option, the compiler simply does not work.
Hence I use the following lines in my cmake toolchain file :
SET(CMAKE_C_FLAGS "-B /path/to/somewhere")
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere")
But during the validation of compiler, CMake does not seem use the flags I set :
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /cross-path/cross-gcc
-- Check for working C compiler: /cross-path/cross-gcc -- broken
CMake Error at /usr/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:52 (MESSAGE):
The C compiler "cross-path/cross-gcc" is not able
to compile a simple test program.
....
....
/cross-path/cross-gcc -o CMakeFiles/cmTryCompileExec.dir/testCCompiler.c.o -c CMakeFiles/CMakeTmp/testCCompiler.c
When I invoke CMake second time, cross-gcc passes the simple compilation test but this time cross-g++ fails :
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /cross-path/cross-gcc
-- Check for working C compiler: /cross-path/cross-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /cross-path/cross-g++
-- Check for working CXX compiler: /cross-path/cross-g++ -- broken
CMake Error at /usr/share/cmake-2.8/Modules/CMakeTestCXXCompiler.cmake:45 (MESSAGE):
The C++ compiler "/cross-path/cross-g++" is not
able to compile a simple test program.
....
....
/cross-path/cross-g++ -o CMakeFiles/cmTryCompileExec.dir/testCXXCompiler.cxx.o -c CMakeFiles/CMakeTmp/testCXXCompiler.cxx
And if I invoke CMake 3rd time, everything works fine with only a warning :
....
CMake Warning:
Manually-specified variables were not used by the project:
CMAKE_TOOLCHAIN_FILE
I'm using CMake 2.8.7 and aware that it is quite outdated but upgrade is not an option if this is the case.
Edit :
Toolchain file :
# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_C_FLAGS "-B /path/to/somewhere")
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere")
# specify the cross compiler
SET(CMAKE_C_COMPILER /cross-path/cross-gcc)
SET(CMAKE_CXX_COMPILER /cross-path/cross-g++)
It looks that the problem of picking up the flags is very similar to one already discussed in SO in the past:
CMake cross-compiling: C flags from toolchain file ignored
Try the proposed solution, i.e. replace:
SET(CMAKE_C_FLAGS "-B /path/to/somewhere")
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere")
by
SET(CMAKE_C_FLAGS "-B /path/to/somewhere" CACHE STRING "" FORCE)
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere" CACHE STRING "" FORCE)
Try also to check if replacing your toolchain file with a new one that uses INCLUDE(CMakeForceCompiler) as mentioned in
http://www.vtk.org/Wiki/CMake_Cross_Compiling and
How can I make Cmake use specific compiler and flags when final compilation stage instead of detection? and
Cmake cross compile flags
INCLUDE(CMakeForceCompiler)
# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_C_FLAGS "-B /path/to/somewhere")
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere")
# specify the cross compiler
CMAKE_FORCE_C_COMPILER(/cross-path/cross-gcc GNU)
CMAKE_FORCE_CXX_COMPILER(/cross-path/cross-g++ GNU)
...
has the same effect as the first hack. The second option seems a cleaner solution.

Why does Cmake Always Choose GCC?

For reasons that I cannot completely understand, Cmake awlays chooses the GNU compiler toolset when compiling software.
My enviroment looks like this:
which cc
/opt/cray/xt-asyncpe/4.9/bin/cc
which CC
/opt/cray/xt-asyncpe/4.9/bin/CC
echo $CC
/opt/cray/xt-asyncpe/4.9/bin/cc
echo $CXX
/opt/cray/xt-asyncpe/4.9/bin/CC
but when I use cmake I get this
Using existing /opt/cmake/2.8.4/bin/cmake
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
And it builds all the software with g++ commands. Why is this going on? How does one set the compiler?
You can also set the env vars CC and CXX much like autotools.
CC=cc CXX=CC cmake ...
Make sure you start with an empty build tree.
I'm not sure why CMake favours GCC.
However, to set the compilers, use:
cmake <path to CMakeLists.txt> -DCMAKE_C_COMPILER=/opt/cray/xt-asyncpe/4.9/bin/cc -DCMAKE_CXX_COMPILER=/opt/cray/xt-asyncpe/4.9/bin/CC
These values will be cached, so subsequent runs of CMake (if required) can simply be invoked by:
cmake .