how to use cmake with msys2/mingw64 gcc

Hi,
I have a thresh msys2 install with mingw64 packages for cmake and gcc.
The compiler is working in standalone (gcc hello.cpp -o hello) but it does not work anymore with cmake.
Here is my command line from a build directory at the same level as my CMakeLists.txt:
cmake --debug-trycompile .. -G "MinGW Makefiles" -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER=gcc
And it is failing with:

debug trycompile on
– The C compiler identification is unknown
– The CXX compiler identification is unknown
– Detecting C compiler ABI info
CMake Debug Log at […]/msys64/mingw64/share/cmake/Modules/CMakeDetermineCompilerABI.cmake:57 (try_compile):
Executing try_compile (CMAKE_C_ABI_COMPILED) in:
[…]/build/Release/CMakeFiles/CMakeScratch/TryCompile-41abgu
Call Stack (most recent call first):
[…]/msys64/mingw64/share/cmake/Modules/CMakeTestCCompiler.cmake:26 (CMAKE_DETERMINE_COMPILER_ABI)
CMakeLists.txt:47 (project)
– Detecting C compiler ABI info - failed
– Check for working C compiler: […]/msys64/ucrt64/bin/gcc.exe
CMake Debug Log at […]/msys64/mingw64/share/cmake/Modules/CMakeTestCCompiler.cmake:56 (try_compile):
Executing try_compile (CMAKE_C_COMPILER_WORKS) in:
[…]/build/Release/CMakeFiles/CMakeScratch/TryCompile-dsx3w6
Call Stack (most recent call first):
CMakeLists.txt:47 (project)
– Check for working C compiler: […]/msys64/ucrt64/bin/gcc.exe - broken
CMake Error at […]/msys64/mingw64/share/cmake/Modules/CMakeTestCCompiler.cmake:67 (message):
The C compiler
“[…]/msys64/ucrt64/bin/gcc.exe”
is not able to compile a simple test program.
It fails with the following output:
Change Dir: ‘[…]/build/Release/CMakeFiles/CMakeScratch/TryCompile-dsx3w6’
Run Build Command(s): […]/msys64/mingw64/bin/cmake.exe -E env VERBOSE=1 […]/msys64/ucrt64/bin/mingw32-make.exe -f Makefile cmTC_ecb19/fast
[…]/msys64/ucrt64/bin/mingw32-make.exe -f CMakeFiles\cmTC_ecb19.dir\build.make CMakeFiles/cmTC_ecb19.dir/build
mingw32-make[1]: Entering directory ‘[…]/build/CMakeFiles/CMakeScratch/TryCompile-dsx3w6’
Building C object CMakeFiles/cmTC_ecb19.dir/testCCompiler.c.obj
[…]\msys64\ucrt64\bin\gcc.exe -o CMakeFiles\cmTC_ecb19.dir\testCCompiler.c.obj -c […]/build\Release\CMakeFiles\CMakeScratch\TryCompile-dsx3w6\testCCompiler.c
mingw32-make[1]: *** [CMakeFiles\cmTC_ecb19.dir\build.make:77: CMakeFiles/cmTC_ecb19.dir/testCCompiler.c.obj] Error 1
mingw32-make[1]: Leaving directory ‘[…]/build/CMakeFiles/CMakeScratch/TryCompile-dsx3w6’
mingw32-make: *** [Makefile:126: cmTC_ecb19/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:47 (project)
– Configuring incomplete, errors occurred!

I’ve seen lots of post on the web with similar issues but I’ve never found a solution.

NB I previously installed gcc directly from a mingw64 installation, cmake was installed manually, and it worked.
NB the issue is now the same with the original cmake install or the msys2 one.
NB msys2 was installed without admin rights, user env path has been updated to find gcc and cmake

a hint: It might be a conflict with conda environment.

the issue seems to disappear when I leave the environment.

I observe that in conda env, $PATH is containing \Anaconda3\Library\mingw-w64\bin while it is not when the env is deactivated.

If it’s the root of the issue, I’m in deep trouble as I’m using CMake through python script which might be run inside anaconda.

Yet the error message seems to indicate that it is the msys2/mingw-w64 gcc version that is used but it crashes silently (I could reproduce the issue : gcc hello.c -o hello works without conda and crashes under conda).

I often find that Anaconda environment conflicts with what I’m trying to build. I make settings in CMakeLists.txt (that work across operating systems) that allow using Anaconda Python while ignoring Anaconda for finding packages etc.:

if(DEFINED ENV{CONDA_PREFIX})
  set(CMAKE_IGNORE_PATH $ENV{CONDA_PREFIX}/bin)
endif()

project(my LANGUAGES C)

# (Optional next two lines if needing Python in CMake project
unset(CMAKE_IGNORE_PATH)
find_package(Python ...)
# end optional lines

# exclude Anaconda directories from search
if(DEFINED ENV{CONDA_PREFIX})
  list(APPEND CMAKE_IGNORE_PREFIX_PATH $ENV{CONDA_PREFIX})
  list(APPEND CMAKE_IGNORE_PATH $ENV{CONDA_PREFIX}/bin)
  # need CMAKE_IGNORE_PATH for CMake < 3.23
  # and to ensure system env var PATH doesn't interfere
  # despite CMAKE_IGNORE_PREFIX_PATH
endif()

you can unset(CMAKE_IGNORE_PATH) after project() if you want to do find_package(Python) or such.

reference

Thanks for your answer.
I gave it a try but it didn’t work.
Actually I think that it’s gcc which is impaired by conda. Not cmake (at least so far).
Nevertheless, I’ll keep your snippet aside in case of needs…

I don’t know about Anaconda environments, but venv environments don’t need “activated” (hrm…Windows may be different) if the tools are executed from their venv path. For example, venv/bin/pip install foo will install into the venv. This might allow you to keep PATH clean if you can otherwise convince just the scripts/tools you need into PATH. You might still need to create the environment IIUC how Conda usually does it.