How can I properly tell cmake which CXX compiler to use? - cmake

I am trying to build a project from cmake on CentOS 7. The project requires C++ 17 and I'm getting this error from the build:
CMake Error at nvbench/nvbench/CMakeLists.txt:86 (target_compile_features):
target_compile_features The compiler feature "cxx_std_17" is not known to
CXX compiler
"GNU"
version 4.8.5.
L86:
target_compile_features(nvbench PUBLIC cuda_std_17 PRIVATE cxx_std_17)
Naturally, CentOS 7's g++ has version 4.8.5 and doesn't support C++ 17, but I have devtoolset 9 which has g++ version 9.3.1. I've added these lines near the top CMakeLists.txt:
set(CMAKE_C_COMPILER /opt/rh/devtoolset-9/root/bin/gcc)
set(CMAKE_CXX_COMPILER /opt/rh/devtoolset-9/root/bin/g++)
but cmake still references the default compiler and not the one in the devtoolset.
Also made sure to run with scl enable devtoolset-9 'cmake ..' but that didn't work either.
How can I fix this so cmake uses g++ in the devtooset?

Related

Unable to build flatc compiler flatbuffer v1.3.0

We are using flatbuffer v1.3.0 and seeing error building flatc compiler.
Same error for flatbuffer version v1.3.0 to v1.12.1
Build is successful for version 2.0.0
Should we use a particular version of make to build the flatc compiler? If so how to do it?
Steps
git clone https://github.com/google/flatbuffers/tree/v1.3.0
cd flatbuffers
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -Success
make --Error
Versions installed on MacOS BigSur Version 11.6
$ cmake --version
cmake version 3.19.4
CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ make --version
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for i386-apple-darwin11.3.0
$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.29.3)
Target: x86_64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
$ cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
CMake Warning:
No source or binary directory provided. Both will be assumed to be the
same as the current working directory, but note that this warning will
become a fatal error in future CMake releases.
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
Compatibility with CMake < 2.8.12 will be removed from a future version of
CMake.
Update the VERSION argument <min> value or use a ...<max> suffix to tell
CMake that the project does not need compatibility with older versions.
-- The C compiler identification is AppleClang 13.0.0.13000029
-- The CXX compiler identification is AppleClang 13.0.0.13000029
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - 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: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/abcd/Documents/FlatBuffer/flatbuffers
$ make
Scanning dependencies of target flatc
[ 2%] Building CXX object CMakeFiles/flatc.dir/src/idl_parser.cpp.o
In file included from /Users/abcd/Documents/FlatBuffer/flatbuffers/src/idl_parser.cpp:28:
In file included from /Users/abcd/Documents/FlatBuffer/flatbuffers/include/flatbuffers/idl.h:25:
/Users/abcd/Documents/FlatBuffer/flatbuffers/include/flatbuffers/flatbuffers.h:1086:25: error: definition of implicit copy constructor for 'TableKeyComparator<reflection::Object>' is deprecated because it has a user-declared copy assignment operator [-Werror,-Wdeprecated-copy]
TableKeyComparator& operator= (const TableKeyComparator&);
^
/Users/abcd/Documents/FlatBuffer/flatbuffers/include/flatbuffers/flatbuffers.h:1100:27: note: in implicit copy constructor for 'flatbuffers::FlatBufferBuilder::TableKeyComparator<reflection::Object>' first required here
std::sort(v, v + len, TableKeyComparator<T>(buf_));
^
/Users/abcd/Documents/FlatBuffer/flatbuffers/include/flatbuffers/flatbuffers.h:1113:12: note: in instantiation of function template specialization 'flatbuffers::FlatBufferBuilder::CreateVectorOfSortedTables<reflection::Object>' requested here
return CreateVectorOfSortedTables(v->data(), v->size());
^
/Users/abcd/Documents/FlatBuffer/flatbuffers/src/idl_parser.cpp:2008:35: note: in instantiation of function template specialization 'flatbuffers::FlatBufferBuilder::CreateVectorOfSortedTables<reflection::Object>' requested here```
Flatbuffer requires a specific version of clang to compile the 1.12.1.
Installing the required version.
sudo apt-get install clang-6.0 lld-6.0 clang-tidy-6.0
use update-alternatives to link the clang.
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-6.0 10
sudo update-alternatives --install /usr/bin/clang++ clang++
/usr/bin/clang++-6.0 10

Create Portable CMake/MingGW(G++) [duplicate]

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++.

How to make cmake identify correct version in centos 6 terminal?

After a very long and lengthy process of trying to get Emscripten going. I'm stuck trying to build fastcomp.
cmake .. -DCMAKE_BUILD_TYPE=Release
-DLLVM_TARGETS_TO_BUILD="X86;JSBackend" -DLLVM_INCLUDE_EXAMPLES=OFF -DLLVM_INCLUDE_TESTS=OFF -DCLANG_INCLUDE_TESTS=OFF
It gives this error:
CMake Error at cmake/modules/CheckCompilerVersion.cmake:12 (message):
Host GCC version must be at least 4.8!
I've installed devtools 2 and actiavted the toolset.
gcc --version reads as using gcc 4.8.2, but it just won't go.
I've also tried:
export CC=/opt/rh/devtoolset-2/root/usr/bin/gcc
export CXX=/opt/rh/devtoolset-2/root/usr/bin/c++
export CPP=/opt/rh/devtoolset-2/root/usr/bin/cpp
What am I doing wrong?
I had this same problem until I recompiled a recent version of cmake with the newer version of gcc already "activated".
Then, with the CC, CXX and CPP environment variables also set to the appropriate paths, cmake then stopped giving that "Host GCC version must be at least 4.8!" error.

How to configure ev3dev CMake to use cross compiler

I am attempting to build the ev3dev C++ bindings (see https://github.com/ddemidov/ev3dev-lang-cpp) for my EV3. But CMake isn't using my cross-compiler toolchain. I've successfully installed the code sourcery toolchain, built a simple hello world app, and run it on my brick. That works just fine. The trouble is that CMake uses my host's g++ compiler instead of the toolchain. I'm familiar with linux and cross-compilers, but I'm a total CMake noob. So I suspect CMake is where I'm going wrong.
I've added the following lines into CMakeLists.txt:
set(CMAKE_C_COMPILER "/cygdrive/c/Users/me/MentorGraphics/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-gcc.exe")
set(CMAKE_CXX_COMPILER "/cygdrive/c/Users/me/MentorGraphics/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-g++.exe")
Here's the host gcc compiler:
$ gcc --version
gcc (GCC) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
And the target toolchain gcc compiler:
$ /cygdrive/c/Users/me/MentorGraphics/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/arm-none-linux-gnueabi-gcc.exe --version
arm-none-linux-gnueabi-gcc.exe (Sourcery CodeBench Lite 2014.05-29) 4.8.3 20140320 (prerelease)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The compilers are different versions, so they are really easy to distinguish.
Now here's the terminal session for the cmake build:
$ mkdir build
$ cd build
$ cmake ../ -DEV3DEV_PLATFORM=EV3
-- No build type selected, default to RelWithDebInfo
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
CMake Warning at /usr/share/cmake-3.6.2/Modules/Platform/CYGWIN.cmake:15 (message):
CMake no longer defines WIN32 on Cygwin!
(1) If you are just trying to build this project, ignore this warning or
quiet it by setting CMAKE_LEGACY_CYGWIN_WIN32=0 in your environment or in
the CMake cache. If later configuration or build errors occur then this
project may have been written under the assumption that Cygwin is WIN32.
In that case, set CMAKE_LEGACY_CYGWIN_WIN32=1 instead.
(2) If you are developing this project, add the line
set(CMAKE_LEGACY_CYGWIN_WIN32 0) # Remove when CMake >= 2.8.4 is required
at the top of your top-level CMakeLists.txt file or set the minimum
required version of CMake to 2.8.4 or higher. Then teach your project to
build on Cygwin without WIN32.
Call Stack (most recent call first):
/usr/share/cmake-3.6.2/Modules/CMakeSystemSpecificInformation.cmake:36 (include)
CMakeLists.txt:7 (project)
-- 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++.exe
-- Check for working CXX compiler: /usr/bin/c++.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: /cygdrive/c/Users/me/Documents/AppProjects/ev3dev-lang-cpp/build
$
CMake is clearly using the cygwin host gcc/g++ compiler even though I've attempted to configure it to use the toolchain. Where am I going wrong?
EDIT: This is not a duplicate of cmake: problems specifying the compiler (2). I did actually use google, and the cross-compiling instructions on the CMake wiki (https://cmake.org/Wiki/CMake_Cross_Compiling) explicitly dictate using SET(). The problem turned out to be that CMake requires the compiler to be set before PROJECT(). I just moved the SET() lines up above PROJECT() and then CMake found the compiler.

Compiling code with Qt5 using CMake on Ubuntu 12.04

I have a fairly special setup here and I have not enough knowledge about CMake to find out what is wrong.
I am on Ubuntu 12.04.
I installed Qt5 from apt-add-repository ppa:ubuntu-sdk-team/ppa and CMake 2.8.11.2 from apt-add-repository ppa:kalakris/cmake (https://launchpad.net/~kalakris/+archive/ubuntu/cmake).
Here is the complete, minimal test-case:
daniel#omni-travis-test:~/projects/qtcmaketest$ ls
blubb.cpp CMakeLists.txt
daniel#omni-travis-test:~/projects/qtcmaketest$ cat CMakeLists.txt
cmake_minimum_required(VERSION 2.8.11)
project (qtcmaketest)
find_package (Qt5Widgets REQUIRED)
add_executable(qtcmaketest blubb.cpp)
target_link_libraries (qtcmaketest Qt5::Widgets)
daniel#omni-travis-test:~/projects/qtcmaketest$ cat blubb.cpp
#include <QWidget>
#include <QApplication>
int main (int argc, char * argv[])
{
QApplication app (argc, argv);
QWidget foo;
foo.show ();
}
daniel#omni-travis-test:~/projects/qtcmaketest$ cmake . && make
-- The C compiler identification is GNU 4.8.1
-- The CXX compiler identification is GNU 4.8.1
-- 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
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/daniel/projects/qtcmaketest
Scanning dependencies of target qtcmaketest
[100%] Building CXX object CMakeFiles/qtcmaketest.dir/blubb.cpp.o
/home/daniel/projects/qtcmaketest/blubb.cpp:1:19: fatal error: QWidget: No such file or directory
#include <QWidget>
^
compilation terminated.
make[2]: *** [CMakeFiles/qtcmaketest.dir/blubb.cpp.o] Error 1
make[1]: *** [CMakeFiles/qtcmaketest.dir/all] Error 2
make: *** [all] Error 2
Why is the include directory missing when find_package was successful?
I was using this guide:
http://qt-project.org/doc/qt-5/cmake-manual.html
I just noticed that, when following the instructions from the chapter "Using Qt 5 with CMake older than 2.8.9", compilation works. I added the following lines:
include_directories(${Qt5Widgets_INCLUDE_DIRS})
add_definitions(${Qt5Widgets_DEFINITIONS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
I know that this should work without those lines, since it runs on an Ubuntu 14.04 box (which includes the appropriate Qt and CMake versions by default). I don't expected to get a solution to this problem, but I could need some pointers on where to look for the problem.
I'm pretty sure that the problem lies in the version of Qt. I think that this early Qt 5 version does not include the CMake scripts to properly work with find_package alone.
On newer distros with newer Qt 5, it works as expected. Looks like you have to add special version checks to include or exclude the lines I mentioned as a workaround.
Additional info: Since Ubuntu 12.04 is really old, I decided not to support it as a target build platform for my project.
My initial reason was that Travis CI uses 12.04, but since it is so much trouble, I will hold my travis builds until they upgrade to 14.04, which seems to be quite a problem.
Travis tweeted to me, on December, 12th 2014:
Travis CI: We are working on it. Most pieces are there, but there is a critical issue with infrastructure. Thanks for your patience.
Here I recently had the same problem and found the following solution to install newer Qt5 versions which enables cmake to proceed properly:
sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu trusty universe"
sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu trusty main"
sudo add-apt-repository --yes ppa:kalakris/cmake
sudo apt-get update -qq
sudo apt-get install cmake
sudo apt-get install qdbus qmlscene qt5-default qt5-qmake qtbase5-dev-tools qtchooser qtdeclarative5-dev xbitmaps xterm libqt5svg5-dev qttools5-dev qtscript5-dev qtdeclarative5-folderlistmodel-plugin qtdeclarative5-controls-plugin -y
In the end I ended up using the following .travis.yml file to get things going:
https://github.com/hzdr/qutselect/blob/master/.travis.yml
Hope that also solves your problems.