FindOpenMP: fails in try_compile check if compiler option -Werror=return-type is set
In CMake v3.9 the FindOpenMP.cmake module saw some massive changes, including the test program used for detection of OpenMP support. Previous versions used
#include <omp.h>
int main() {
#ifdef _OPENMP
return 0;
#else
breaks_on_purpose
#endif
}
whereas CMake v3.9 and newer uses
#include <omp.h>
int main() {
#ifndef _OPENMP
breaks_on_purpose
#endif
}
This should not make much of a difference unless you enable -Werror=return-type
which is done in the OpenCV build for example. The C compiler will complain about the missing return statement whereas the CXX compiler does not care. This results in CMake reporting OpenMP_C to be missing.
The problem can be reproduced easily with this minimal CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)
cmake_policy(SET CMP0012 NEW)
project(example)
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=return-type")
find_package(OpenMP)
Without the line setting the CMAKE_C_FLAGS
you will get the following output:
-- Found OpenMP_C: -fopenmp (found version "3.1")
-- Found OpenMP_CXX: -fopenmp (found version "3.1")
With the line you get this:
-- Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES) (found version "1.0")
-- Found OpenMP_CXX: -fopenmp (found version "3.1")