Search code examples
c++macosgccclion

Unable to set C++ compiler to GCC in CLion on Intel Mac


I am working on a C++ project on my Mac using CLion as my IDE, therefore I started with the Apple clang compiler, cmake as build tool and ninja as generator. Now I would like to use more parallelism, especially the execution policies, which are not supported by the clang compiler. I had a version of GCC (12.2.0) already installed and wanted to use it.

Trying to set the C++ compiler as -D CMAKE_CXX_COMPILER=/usr/local/Cellar/gcc/12.2.0/bin/g++-12 flag did not work, neither did specifying the path to the GCC compiler in the toolchains (both done in the GUI of CLion, under Build, Execution, Deployment > Toolchains/CMake as suggested here). I specified the whole path, even though I generated symlinks for gcc/g++ to point to the GCC instead of the Apple clang compiler.

I am always getting the error, that the compiler fails to compile a simple test program, and furthermore, that the compiler identification is unknown and might be broken.

-- The C compiler identification is AppleClang 14.0.3.14030022
-- The CXX compiler identification is unknown
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: /usr/local/Cellar/gcc/12.2.0/bin/g++-12
-- Check for working CXX compiler: /usr/local/Cellar/gcc/12.2.0/bin/g++-12 - broken
CMake Error at /Applications/CLion.app/Contents/bin/cmake/mac/share/cmake-3.25/Modules/CMakeTestCXXCompiler.cmake:63 (message):
  The C++ compiler

    "/usr/local/Cellar/gcc/12.2.0/bin/g++-12"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /Users/xy/CLionProjects/MasterThesis/QASM-Parser/cmake-build-debug-gcc/CMakeFiles/CMakeScratch/TryCompile-rKisQk
    
    Run Build Command(s):/Applications/CLion.app/Contents/bin/ninja/mac/ninja cmTC_ff5c4 && [1/2] Building CXX object CMakeFiles/cmTC_ff5c4.dir/testCXXCompiler.cxx.o
    FAILED: CMakeFiles/cmTC_ff5c4.dir/testCXXCompiler.cxx.o 
    /usr/local/Cellar/gcc/12.2.0/bin/g++-12    -o CMakeFiles/cmTC_ff5c4.dir/testCXXCompiler.cxx.o -c /Users/cedricgaberle/CLionProjects/MasterThesis/QASM-Parser/cmake-build-debug-gcc/CMakeFiles/CMakeScratch/TryCompile-rKisQk/testCXXCompiler.cxx
    /usr/local/bin/clang: /usr/local/bin/clang: cannot execute binary file
    ninja: build stopped: subcommand failed.
    
    

  

  CMake will not be able to correctly generate this project.

It looks like, clang is still trying to execute the binaries, which might be the root of the problem. Any ideas how to resolve this?

Actually it should be no problem, building and executing the project using GCC in the terminal works perfectly fine.

Any help is appreciated, thank you!


Solution

  • After days of trouble shooting with the JetBrains support, it all came down to some environment issues. I am using conda as environment manager, which automatically starts a so called base environment. I had a workaround to deactivate it in terminal, but it was still set in CLion. This ultimately led to the problems not finding the GCC compiler.

    In short: conda provides a configuration to disable this behaviour. Then CLion doesn't activate conda either and the environment issues, and ultimately the Compiler issues, were solved.