Clang version confusion (OSX and Cmake) - c++

I'm attempting to lock down the compiler version I use (for a number of reasons I won't go into). To do this, I'd like to check the version of the compiler on build using CMAKE_CXX_COMPILER_VERSION and CMAKE_CXX_COMPILER_VERSION.
On OSX CMake reports:
CMAKE_CXX_COMPILER_VERSION 7.0.2.7000181
CMAKE_CXX_COMPILER_ID Clang
CMAKE_CXX_COMPILER /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
However, the version reported via terminal is:
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
This is true of:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ --version
clang --version
gcc --version
g++ --version
Why the discrepancy in version numbers as reported by CMake and the command line (for the same executable)?
And for the purposes of restricting to a particular compiler version, is CMAKE_CXX_COMPILER_VERSION reliable?

Turns out CMake sets the compiler version in CMakeFiles/3.5.0/CmakeCXXCompiler.cmake (for version 3.5.0). Only by deleting this file and re-building is the correct compiler version reported (assuming you had a different one installed at some point).

Related

Why do `gcc -v` and `whereis gcc` output different paths?

I get the following output when I run respectively gcc -v and whereis gcc:
Apple clang version 13.1.6 (clang-1316.0.21.2.3)
Target: x86_64-apple-darwin21.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
gcc: /usr/bin/gcc
Does this mean I have multiple installations of gcc on my mac ? If so, how do I uninstall the 'extra/unnecessary' ones ?
Thanks

CMake Error at CMakeLists.txt:61: Your compiler does not fully support the C++17 standard and libraries

I am trying to install a library from https://github.com/Koihik/LuaFormatter, and I got an error when I am using cmake .
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- 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
-- IPO / LTO enabled
-- Looking for C++ include filesystem
-- Looking for C++ include filesystem - not found
CMake Warning at CMakeLists.txt:30 (message):
filesystem not found, use experimental/filesystem as fallback. It's
recommended to use a stable C++17 compliant compiler/libstdc++.
I am not a C++ developer and I am not sure what is the next step I need to take. I have checked this question, it seems like I need to have gcc 9 or 10, clang 9 or 10 and XCode LLVM 9.3 or later. I have installed all packages, but it is still not working. These are my version check:
❯ clang --version
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Is gcc the problem here? It is Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
❯ gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
❯ xcode-select --version
xcode-select version 2354.
Update
Following this answer, I have made an alias in .zshrc, but still I can't build correctly:
alias gcc='gcc-10'
alias cc='gcc-10'
alias g++='g++-10'
alias c++='c++-10'
Update 2
This is the error I am getting when I cmake .
#include <experimental/filesystem>
^~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make[1]: *** [CMakeFiles/cmTC_a8010.dir/fs.cpp.o] Error 1
make: *** [cmTC_a8010/fast] Error 2
CMake Error at CMakeLists.txt:61 (message):
Your compiler does not fully support the C++17 standard and libraries

How to get the gcc version of .so file on OSX 10.11.6

On mac, Enter "gcc -v" in Terminal to get the gcc version as below.
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
How to get the gcc version used to generate the .so file? For example, I want to get the gcc version from a .so file on Mac.

gcov error: Multiple sources for a single basic block

When I invoke gcov as follows it returns the following error message:
gcov -a -p dynamic-maps.gcda
Multiple sources for a single basic block: <path-to-file>/dynamic-maps.cpp != (in _ZN16AnalysisPipeline16AnalysisServices22calculate_dynamic_mapsERKNSt3__110shared_ptrINS_6Common16AnalysisRegistryEEERKN9Utilities6ImagesERKNS3_10TimeSeriesERKNS3_14T1ParameterFitE).
Invalid .gcno File!
Please can someone explain what the cause of this might be.
gcov -version returns:
LLVM (http://llvm.org/):
LLVM version 3.4svn
Optimized build.
Default target: x86_64-apple-darwin13.4.0
Host CPU: core-avx-i
The file was compiled using Xcode 5.1 on OS X Mavericks with the following version of clang:
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
Updating Xcode to version 6.1.1 has resolved the issue.

How can I make CMake use GCC instead of Clang on Mac OS X?

I can't find any info on it, but only the other way around (e.g., how to set CMake to use clang).
I've installed gcc-4.8 using brew, setup all dependencies, headers, etc, and now CMake refuses to use gcc.
I've set my bash profile with both aliases and actual entries:
export CC=/usr/bin/gcc
export CXX=/usr/bin/g++
alias gcc='gcc-4.8'
alias cc='gcc-4.8'
alias g++='g++-4.8'
alias c++='c++-4.8'
Yet CMake stubbornly refuses to use gcc and instead reverts back to clang:
air:build alex$ cmake -DCMAKE_BUILD_TYPE=DEBUG ..
-- The C compiler identification is Clang 5.1.0
-- The CXX compiler identification is Clang 5.1.0
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
CMake doesn't (always) listen to CC and CXX. Instead use CMAKE_C_COMPILER and CMAKE_CXX_COMPILER:
cmake -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++ ...
See also the documentation.
Alternatively, you can provide a toolchain file, but that might be overkill in this case.
Current versions of CMake do not respect the CC and CXX environment variables like one would expect. Specifically if they are absolute paths to the compiler binaries they seem to be ignored. On my system with a freshly compiled cmake 3.7.1 I have to do cmake -H. -Bbuild -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX.
As others have stated it is not a great idea to force a compiler choice within your CMakeLists.txt, however if this is required for your use case here's how you do it:
cmake_minimum_required(VERSION 3.5) # Or whatever version you use
# THIS HAS TO COME BEFORE THE PROJECT LINE
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")
# THIS HAS TO COME BEFORE THE PROJECT LINE
project(my_project VERSION 0.0.0 LANGUAGES C CXX)
In this case cmake will fail if the indicated compiler is not found. Note that you must set these variables before the project line as this command is what finds and configures the compilers.
I guess the case in OP was run on a macOS, and the real problem is /usr/bin/gcc and /usr/bin/g++ by default are clang and clang++.
% ls -al /usr/bin/clang
-rwxr-xr-x 1 root wheel 167088 8 Dec 07:39 /usr/bin/clang
% ls -al /usr/bin/gcc
-rwxr-xr-x 1 root wheel 167088 8 Dec 07:39 /usr/bin/gcc
% /usr/bin/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.30)
Target: x86_64-apple-darwin21.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
One way avoid that is to install gcc/g++ and then use the versioned binary names.
% which gcc-11 g++-11
/usr/local/bin/gcc-11
/usr/local/bin/g++-11
% gcc-11 --version
gcc-11 (Homebrew GCC 11.2.0_3) 11.2.0
Copyright (C) 2021 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.
So, instead of
cmake .. -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++
one would use
cmake .. -DCMAKE_C_COMPILER=/usr/local/bin/gcc-11 -DCMAKE_CXX_COMPILER=/usr/local/bin/g++-11
it should be enough to use CC and CXX environment variables, but in macOS are not "working as expected".
but it works in this way instead eg:
CXX="gcc-8" CC="gcc-8" cmake ..
Just to add that, there is also a CMake variable CMAKE_Fortran_COMPILER to pick up GNU FORTRAN rather than clang FORTRAN. But it seems to be missing in the documentation
cmake -DCMAKE_Fortran_COMPILER=/usr/.../bin/gfortran-6.x.0