Swapping project to xlc 17.1.1 with mixed C / C++ code

We’ve been using cmake for a couple of months on AIX 7.2 with XLC 16, we need to swap to the clang based xlc 17.1.1 however have hit a bit of an issue as our app is a mix of c and c++ when we compile our c source with the ibm-clang++_r compiler.

i can demonstrate the problem with a hello.c and the CC / CXX environment variables set to /opt/IBM/openxlC/17.1.1/bin/ibm-clang++_r (see below).

We’re using CMake 3.26.3 .

hello.c

#include <stdio.h>

int main(int argc, char **argv) {
  printf("hello world\n");
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.23)

project(hello LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 11)

add_compile_options (-q64 -qrtti -qchars=signed -+)
add_link_options (-brtl -qrtti -q64 -qarch=pwr8 -qtune=pwr9)

set_source_files_properties(hello.c PROPERTIES LANGUAGE CXX)

add_executable(hello hello.c)

output of cmake .

-bash-5.1$ cmake .
-- The C compiler identification is unknown
-- The CXX compiler identification is IBMClang 17.1.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /opt/IBM/openxlC/17.1.1/bin/ibm-clang++_r
-- Check for working C compiler: /opt/IBM/openxlC/17.1.1/bin/ibm-clang++_r - broken
CMake Error at /opt/cmake/cmake-3.26.3-xlc/share/cmake-3.26/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "/opt/IBM/openxlC/17.1.1/bin/ibm-clang++_r"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /data/js/cpptest/CMakeFiles/CMakeScratch/TryCompile-7Flaeb

    Run Build Command(s):/opt/cmake/cmake-3.26.3-xlc/bin/cmake -E env VERBOSE=1 /usr/local/bin/gmake -f Makefile cmTC_78528/fast && /usr/local/bin/gmake  -f CMakeFiles/cmTC_78528.dir/build.make CMakeFiles/cmTC_78528.dir/build
    gmake[1]: Entering directory '/data/js/cpptest/CMakeFiles/CMakeScratch/TryCompile-7Flaeb'
    Building C object CMakeFiles/cmTC_78528.dir/testCCompiler.c.o
    /opt/IBM/openxlC/17.1.1/bin/ibm-clang++_r    -o CMakeFiles/cmTC_78528.dir/testCCompiler.c.o -c /data/js/cpptest/CMakeFiles/CMakeScratch/TryCompile-7Flaeb/testCCompiler.c
    .ibm-clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
    /data/js/cpptest/CMakeFiles/CMakeScratch/TryCompile-7Flaeb/testCCompiler.c:2:3: error: "The CMAKE_C_COMPILER is set to a C++ compiler"
    # error "The CMAKE_C_COMPILER is set to a C++ compiler"
      ^
    1 error generated.
    CMakeFiles/cmTC_78528.dir/build.make:77: recipe for target 'CMakeFiles/cmTC_78528.dir/testCCompiler.c.o' failed
    gmake[1]: *** [CMakeFiles/cmTC_78528.dir/testCCompiler.c.o] Error 1
    gmake[1]: Leaving directory '/data/js/cpptest/CMakeFiles/CMakeScratch/TryCompile-7Flaeb'
    Makefile:127: recipe for target 'cmTC_78528/fast' failed
    gmake: *** [cmTC_78528/fast] Error 2





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:3 (project)


-- Configuring incomplete, errors occurred!

I don’t think you should try to use the C++ compiler as the C compiler to do what you want. Instead, you should set the LANGUAGE source file property on the C sources to use the C++ compiler (if I understand what your goal is here at least).

Yep - i did, that, in my test hello project i updated the CMakeLists.txt to set LANGUAGES to CXX

i also set

set_source_files_properties(hello.c PROPERTIES LANGUAGE CXX)

so i thought it would compile with the CXX compiler.

This technique works fine on linux with GCC/G++.

so bit perplexed. its almost as the warning generated by the C compile check is being subsequently treated as an error. (but i am only speculating)

ignore me - i am being dense… i may have it sorted

File save error, this does indeed address my issue. It appears in our main project a number of CMakeLists.txt files still had LANGUAGES set to C and CXX.

The error comes from this code which errors if the C compiler sets __cplusplus.