Cmake finding wrong compiler in my ITK project but finds correct compiler in ITK library?

Hi,
This problem did not happen several weeks ago but it happens now.
I am at a large installation with lots of linux workstations administered centrally. The choice of the administratiors is that all workstations have the as-released version of redhat EL 7 , and many newer versions of softare are then selected in the environment from a shared location.
In this particular case, a new version of GNU compiler collection is selected and a new version of CMAKE is selected using ‘module’ system (which alters environment variables and paths to suit)
When I use module cmake/3.12 and module gcc/9 , the relevant results of ‘which’ are:
apps/cmake/3.12.0/bin/cmake
apps/gcc/9.2.0/7/bin/gcc
/dls_sw/apps/gcc/9.2.0/7/bin/g++

When I create a fresh build dir for the ITK (release 5.1) and run cmake, it correctly finds the gcc and g++ ; and the library builds correctly.

I have a project, not all that complex , just to implement a few piplelines I found useful. Using exactly the same enviroment (checked now by using the same login window without changing any environment) if I run cmake or ccmake in a fresh build dir for my project, it finds the system gcc and g++ (4.8 ) and of course the project will not build.

I don’t see what feature in the ITK library enables the build system to find the correct compiler, whereas it does not find it in my project. WHat can I add to my CMakeLists to make this work properly? Since it works when I build the library itself, I don’t think that the environment is wrong …?
Thanks
Robert

Sharing your project’s CMakeLists.txt might help diagnose the problem.

Sure !

# This project is designed to be built outside the Insight source tree.

cmake_minimum_required(VERSION 3.10) #added this today to try to understand the problem

PROJECT(nbct C CXX) #added this today to try to understand the problem

# Find ITK.

FIND_PACKAGE(ITK REQUIRED)

IF(ITK_FOUND)

INCLUDE(${ITK_USE_FILE})

ELSE(ITK_FOUND)

MESSAGE(FATAL_ERROR

"Cannot build without ITK. Please set ITK_DIR.")

ENDIF(ITK_FOUND)

option(NBCT_BUILD_CONVERT "Build file converter" ON)

option(NBCT_BUILD_KMEANS "Build Kmeans filter applet" ON)

option(NBCT_BUILD_SCANSPHERE "Build scansphere unit" OFF)

option(NBCT_BUILD_INTERP "Build test interp" OFF)

option(NBCT_BUILD_SCANPAR "Build parallel scanner" OFF)

option(NBCT_WATCH "Include optional progress monitors" OFF)

option(NBCT_BUILD_FLOP "Build file reslicer" OFF)

option(NBCT_BUILD_MEDIAN "Build Median filter applet" OFF)

option(NBCT_BUILD_MEDSTREAM "Build Median filter streaming applet" OFF)

option(NBCT_BUILD_MREDUCE "Build Median+Reduce filter applet" OFF)

option(NBCT_BUILD_WATERSHED "Build Watershed filter applet" OFF)

option(NBCT_BUILD_JUNC "Build junciton counting applet" OFF)

option(NBCT_BUILD_SERIES "Build image series converers" OFF)

option(NBCT_BUILD_HESSIAN "Build hessian eigenvalue routine" OFF)

option(NBCT_DEBUG_GETDIR "Debug messages" OFF)

option(NBCT_BUILD_TEX_DOC "Build documents using LaTeX" OFF)

option(NBCT_SINGLETHREAD "Force Single Thread Only" OFF)

SET(CMAKE_INSTALL_PREFIX "/dls/i12/software/64" CACHE PATH "Local installation prefix" FORCE )

if(NBCT_DEBUG_GETDIR )

add_definitions(-DDEBUG_GETDIR)

endif(NBCT_DEBUG_GETDIR)

if(NBCT_SINGLETHREAD )

add_definitions(-DSINGLETHREAD)

endif(NBCT_SINGLETHREAD)

if(NBCT_WATCH)

add_definitions(-DWATCH)

endif(NBCT_WATCH)

ADD_DEFINITIONS(-fPIC)

if(NBCT_BUILD_SCANSPHERE)

ADD_LIBRARY(scansphere SHARED scansphere.cxx)

endif(NBCT_BUILD_SCANSPHERE)

if(NBCT_BUILD_INTERP)

ADD_EXECUTABLE(itk_interp_rca interp.cxx )

TARGET_LINK_LIBRARIES(itk_interp_rca ${ITK_LIBRARIES} m)

endif(NBCT_BUILD_INTERP)

if(NBCT_BUILD_SCANPAR)

ADD_LIBRARY(scanpar SHARED scanpar.cxx scanpar_routines.cxx getextents.cxx)

TARGET_LINK_LIBRARIES(itk_interp_rca ${ITK_LIBRARIES} m)

endif(NBCT_BUILD_INTERP)

if(NBCT_BUILD_SCANPAR)

ADD_LIBRARY(scanpar SHARED scanpar.cxx scanpar_routines.cxx getextents.cxx)

ADD_EXECUTABLE(itk_parallel_project itk_parallel_project.cxx projctrl_writer.c projctrl_reader.c matprops_reader.cxx matprops_writer.cxx eleme

TARGET_LINK_LIBRARIES(itk_parallel_project scanpar ${ITK_LIBRARIES} m)

ADD_EXECUTABLE(nonitk_parallel_project nonitk_parallel_project.cxx projctrl_writer.c projctrl_reader.c matprops_reader.cxx matprops_writer.cxx

TARGET_LINK_LIBRARIES(itk_parallel_project scanpar ${ITK_LIBRARIES} m)

ADD_CUSTOM_TARGET(ele ALL echo

DEPENDS ${nbct_BINARY_DIR}/elements.c

)

endif(NBCT_BUILD_SCANPAR)

if(NBCT_BUILD_WATERSHED)

ADD_DEFINITIONS(-DT_FLOAT)

ADD_DEFINITIONS(-I .)

ADD_EXECUTABLE(itk_wshd_rca itk_wshd_rca.cxx ctrl_struct_reader.cxx safeopen.cxx)

ADD_DEPENDENCIES(itk_wshd_rca includer)

TARGET_LINK_LIBRARIES(itk_wshd_rca ${ITK_LIBRARIES})

endif(NBCT_BUILD_WATERSHED)

if(NBCT_BUILD_KMEANS)

#ADD_DEFINITIONS(-DT_FLOAT)

ADD_EXECUTABLE(itk_kmeans_rca itk_kmeans_rca.cxx safeopen.cxx)

TARGET_LINK_LIBRARIES(itk_kmeans_rca ${ITK_LIBRARIES})

endif(NBCT_BUILD_KMEANS)

if(NBCT_BUILD_HESSIAN)

#ADD_DEFINITIONS(-DT_FLOAT)

ADD_EXECUTABLE(itk_hes_rca itk_hes_rca.cxx safeopen.cxx)

TARGET_LINK_LIBRARIES(itk_hes_rca ${ITK_LIBRARIES})

endif(NBCT_BUILD_HESSIAN)

if(NBCT_BUILD_MEDSTREAM)

ADD_DEFINITIONS(-DT_FLOAT)

ADD_EXECUTABLE(itk_medstream_rca itk_medstream_rca.cxx ctrl_struct_reader.cxx safeopen.cxx)

TARGET_LINK_LIBRARIES(itk_medstream_rca ${ITK_LIBRARIES})

endif(NBCT_BUILD_MEDSTREAM)

if(NBCT_BUILD_MEDIAN)

ADD_DEFINITIONS(-DT_FLOAT)

ADD_EXECUTABLE(itk_med_rca itk_med_rca.cxx ctrl_struct_reader.cxx safeopen.cxx)

TARGET_LINK_LIBRARIES(itk_med_rca ${ITK_LIBRARIES})

#ADD_EXECUTABLE(itk_medstream_rca itk_medstream_rca.cxx ctrl_struct_reader.cxx safeopen.cxx)

#TARGET_LINK_LIBRARIES(itk_medstream_rca ${ITK_LIBRARIES})

endif(NBCT_BUILD_MEDIAN)

if(NBCT_BUILD_MREDUCE)

ADD_DEFINITIONS(-DT_FLOAT)

ADD_EXECUTABLE(itk_mreduce_rca itk_mreduce_rca.cxx ctrl_struct_reader.cxx safeopen.cxx)

TARGET_LINK_LIBRARIES(itk_mreduce_rca ${ITK_LIBRARIES})

endif(NBCT_BUILD_MREDUCE)

if(NBCT_BUILD_CONVERT)

ADD_EXECUTABLE(itk_convert itk_convert.cxx )

TARGET_LINK_LIBRARIES(itk_convert ${ITK_LIBRARIES} m)

endif(NBCT_BUILD_CONVERT)

if(NBCT_BUILD_FLOP)

ADD_EXECUTABLE(itk_flop itk_flop.cxx )

TARGET_LINK_LIBRARIES(itk_flop ${ITK_LIBRARIES} m)

endif(NBCT_BUILD_FLOP)

if (NBCT_BUILD_JUNC)

ADD_EXECUTABLE(itk_nbct_rca itk_nbct_rca.cxx nc_str_reader.cxx Segments.cxx Pixlist.cxx paint.cxx scanners.cxx juncfuncs.cxx )

TARGET_LINK_LIBRARIES(itk_nbct_rca ${ITK_LIBRARIES} m)

endif(NBCT_BUILD_JUNC)

if(NBCT_BUILD_SERIES)

ADD_EXECUTABLE(itk_readwrite_series itk_readwrite_series.cxx )

TARGET_LINK_LIBRARIES(itk_readwrite_series ${ITK_LIBRARIES} m)

ADD_EXECUTABLE(itk_series_readwrite itk_series_readwrite.cxx )

TARGET_LINK_LIBRARIES(itk_series_readwrite ${ITK_LIBRARIES} m)

endif(NBCT_BUILD_SERIES)

if(NBCT_BUILD_TEX_DOC)

FIND_PACKAGE (LATEX)

set (LATEX_COMPILER pdflatex)

configure_file(itk_nbct_rca.tex itk_nbct_rca.tex COPYONLY)

configure_file(itk_scansphere_rca.tex itk_scansphere_rca.tex COPYONLY)

configure_file(itk_parallel_project.tex itk_parallel_project.tex COPYONLY)

configure_file(TableOfContents.tex TableOfContents.tex COPYONLY)

configure_file(tweaklist.sty tweaklist.sty COPYONLY)

IF (LATEX_COMPILER)

ADD_CUSTOM_COMMAND (OUTPUT ${nbct_BINARY_DIR}/documentation.pdf

COMMAND ${LATEX_COMPILER}

ARGS ${nbct_SOURCE_DIR}/documentation.tex

DEPENDS ${nbct_SOURCE_DIR}/documentation.tex

${nbct_SOURCE_DIR}/itk_scansphere_rca.tex

${nbct_SOURCE_DIR}/itk_nbct_rca.tex

${nbct_SOURCE_DIR}/TableOfContents.tex

${nbct_SOURCE_DIR}/itk_parallel_project.tex

${nbct_SOURCE_DIR}/tweaklist.sty

COMMENT "Tex2pdf"

)

endif (LATEX_COMPILER)

ADD_CUSTOM_TARGET(doc ALL echo

DEPENDS ${nbct_BINARY_DIR}/documentation.pdf

)

endif(NBCT_BUILD_TEX_DOC)

#create an include-file for incorporating the build details in the executable

#this should also somehow include a dependency for getcflags.inc ?

Pastebin of the CmakeLists file

The lines:

#create an include-file for incorporating the build details in the executable
#this should also somehow include a dependency for getcflags.inc ?
#
set(INCFILE ./getcflags.h)

seem suspicious.
Is it possible that one of those files, getcflags.inc is setting the CMAKE_C_COMPILER variable?

Can you explain why this would cause the build system to begin by finding the wrong compiler at the very outset?

It ‘seems unrelated’ to me.

I am not sure why CMake finds the wrong compiler, it shouldn’t, it should only depend on the environment from where you are calling it.
I would start printing:

 message(WARNING "CMAKE_C_COMPILER: ${CMAKE_C_COMPILER}")
 message(WARNING "CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}")

at the start and at the end for debugging purposes, just in case something inside your CMake logic changes it. That’s what I thought your getcflags was doing.

Also try to explicitly set the env flags (first command in a fresh build directory):

CC=/path/c_compiler CXX=/path/cxx_compiler cmake . ...

but I am guessing setting those env variables is exactly what the rhel modules are doing anyway.
I would bet this is a problem with your environment, not with CMake.

Another guess, if you first configure CMake with a set of compilers, changing them in future configurations can be problematic. Just be sure that your first configuration in a fresh build folder is with the desired environment. HTH

I would bet this is a problem with your environment, not with CMake. I cannot think on anything else that can help.

Problem with that, is that within exactly the same environment, the build does find the specified CC / CXX compilers.

The getcflags.h section has not been undertaken so far at that point, the annotated session below shows how I checked this.

But, Setting the environment variables CC and CXX in the shell does make the project build. The module seems to be only setting the PATH , LD_LIBRARY_PATH, and MANPATH

Nonetheless, building the ITK project in this environment finds the correct compiler and link libraries .

testbuild]$ module add gcc

testbuild]$ echo $PATH

the path contains the compiler location at the outset

/sw/apps/gcc/9.2.0/7/bin:/sw/apps/cmake/3.12.0/bin/:/sw/i12/software/tomoTilt/cod……

testbuild]$ cmake …/newtrunk/

– The C compiler identification is GNU 4.8.5 ß ß ß :frowning: :frowning:

– The CXX compiler identification is GNU 4.8.5

– Check for working C compiler: /usr/bin/cc

– Check for working C compiler: /usr/bin/cc – works

– Detecting C compiler ABI info

^C

cmake finds the system compiler , if allowed to continue, the project won’t link as the itk build requires more recent standards I beleve, judging from the large number of error messages at the link step.

After cancelling it , the getcflags.h has not been generated yet:

testbuild]$ ls -al

total 16

4096 Jun 24 11:41 .

4096 Jun 24 10:34 …

2034 Jun 24 11:41 CMakeCache.txt

4096 Jun 24 11:41 CMakeFiles

Now try in the same environment, the build of the ITK project.

Indicate exactly what version I have: From git, ‘release’ branch:

testbuild]$ cd /science/users/$USER /sources/itk/5.git/

5.git]$ mkdir itk_testbuild

5.git]$ cd ITK/

ITK]$ git branch

master

  • release

ITK]$ git status

On branch release

Your branch is up to date with ‘origin/release’.

nothing to commit, working tree clean

In a clean fresh build directory:

ITK]$ cd …

5.git]$ cd itk_testbuild/

itk_testbuild]$ ls -al

total 8

4096 Jun 24 11:43 .

4096 Jun 24 11:43 …

Just get the first output of cmake where the compiler is detected:

itk_testbuild]$ cmake …/ITK/

– Setting build type to ‘Release’ as none was specified.

– The CXX compiler identification is GNU 9.2.0 ßß ß ß :astonished: :astonished: :astonished: :astonished: Why does it find the right one , here, but not up above???

– The C compiler identification is GNU 9.2.0

– Check for working CXX compiler: /sw/apps/gcc/9.2.0/7/bin/c++

– Check for working CXX compiler: / sw/apps/gcc/9.2.0/7/bin/c++ – works

– Detecting CXX compiler ABI info

^C

The environment variables are not set. Is this a requirement? Then why does it work just now with the ITK project cmake???

Itk_testbuild]$ env | grep CXX

Itk_testbuild]$ env | grep CC

(UPDATE, the project builds with these set manually in the shell, using CC=which gcc;CXX=which g++ , thanks for the workaround, but , why does ITK build without these set? Is it a requirement, or not??? Since ‘which’ returns the correct one, why should this be necessary? )

UPDATE:

I noticed that the output of the ITK build had –

– The CXX compiler identification is GNU 9.2.0 ßß ß ß :astonished: :astonished: :astonished: :astonished: Why does it find the right one , here, but not up above???

– The C compiler identification is GNU 9.2.0

Whereas the output of my build had:

– The CXX compiler identification is GNU 9.2.0 ßß ß ß :astonished: :astonished: :astonished: :astonished: Why does it find the right one , here, but not up above???

– The C compiler identification is GNU 9.2.0

The order of C and CXX is different. SO , I tried changing this line:

3 PROJECT(nbct C CXX)

To the following

3 PROJECT(nbct CXX C)

Now it works

testbuild]$ cmake …/newtrunk/

– The CXX compiler identification is GNU 9.2.0

– The C compiler identification is GNU 9.2.0

Switching it back makes it stop working again.

testbuild]$ cmake …/newtrunk/

– The C compiler identification is GNU 4.8.5

– The CXX compiler identification is GNU 4.8.5

I would not say that is ‘fixed’ , it will be difficult for me to be convinced that this is correct, meaningful behaviour!!

That’s very surprising, specially because when no language is specified, the defaults are C CXX in that order.
Could you try, out of curiosity, what happen if you omit the languages? Using just PROJECT(nbct).

I agree. You might find more detailed answers opening a thread in https://discourse.cmake.org/, or an issue in cmake gitlab, maybe they can link it to some recent changes in CMake source.

Glad that you can at least compile your project for now explicitly setting the env variables.

Indeed , I did not have the languages specified initially and it got the wrong (system) compiler, Then I added the language specifications as in another recommendation on the web, but did’t try the opposite order!

image001.jpg