Unknown CMake command "CMAKE_DEPENDENT_OPTION" - cmake

I faced with an issue that my installed on Ubuntu cmake
cmake --version
cmake version 3.17.2
doesn't recognize CMAKE_DEPENDENT_OPTION command.
So, my CMakeLists.txt with dependent option example from cmake.org/v3.16:
cmake_minimum_required(VERSION 3.4.1)
project(myexe)
CMAKE_DEPENDENT_OPTION(USE_FOO "Use Foo" ON
"USE_BAR;NOT USE_ZOT" OFF)
file(GLOB SRC_FILES "src/*.cpp")
add_executable(${PROJECT_NAME} ${SRC_FILES})
The following error is displayed when running cmake:
CMake Error at CMakeLists.txt:5 (CMAKE_DEPENDENT_OPTION):
Unknown CMake command "CMAKE_DEPENDENT_OPTION".
Why does it happen because the spec says it is supported? Thanks for any help!

You need to add
include(CMakeDependentOption)
before accessing this function.

Related

How to get cmake generate -std=c++14 flag for qcc compiler

I'm trying to cross compile some c++ library for QNX neutrino using cmake. In CMakeLists.txt file I specified CMAKE_CXX_STANDARD 14 required, but the resulting compiler command line does not contain the -std=c++14 option.
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
I've tried using target compile features:
target_compile_features(my_library PRIVATE cxx_std_14)
but that gives me the following error:
CMake Error at CMakeLists.txt:53 (target_compile_features):
target_compile_features no known features for CXX compiler
"QCC"
version 5.4.0.
When I'm using check_cxx_compiler_flag feature, it seems to recognize the option:
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-std=c++14 CXX14_SUPPORT)
if(CXX14_SUPPORT)
message("c++14 support found")
else()
message("c++14 unsupported")
endif()
This outputs message
c++14 support found
Running qcc manually it accepts the -std=c++14 option just fine and the code using std::make_unique compiles just fine.
Also using the native compiler (Ubuntu 18.04, gcc) everything work fine with cmake generated makefiles. make VERBOSE=1 displays the following command line (I removed some directories):
/usr/local/bin/c++ -Dshm_transfer_EXPORTS -I... -fPIC -std=gnu++14 -o CMakeFiles/shm_transfer.dir/src/SharedMemoryTransfer.cpp.o -c .../SharedMemoryTransfer.cpp
as opposed to the command line using qcc toolchain:
.../qnx700/host/linux/x86_64/usr/bin/qcc -lang-c++ -Vgcc_ntox86_64 -lang-c++ -Dshm_transfer_EXPORTS -I... -fPIC -o CMakeFiles/shm_transfer.dir/src/SharedMemoryTransfer.cpp.o -c .../SharedMemoryTransfer.cpp
I would have expected the cmake command to recognize that qcc supports the -std=c++14 option and generates the corresponding command lines because of the CMAKE_CXX_STANDARD setting.
Use
set_property(TARGET ${PROJECT_NAME} PROPERTY LINKER_LANGUAGE CXX)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)
. Using this you can stick the compiler setting to the target, while global flags are dis encouraged and can be overwritten by other cmake consumers. This the reason I assume why the deprecated set(CMAKE_CXX_STANDARD 14) did not help you: I can not see your full CMakeLists.txt and bet you have many sub folders and other targets, which could reset the CMAKE_CXX_STANDARD them selfs. Also make sure of the ordering of the CMake commands.
And you can replace ${PROJECT_NAME} with my_library if you want.
add_compile_options(-std=gnu++14)
Add this to your project level CMakeLists.txt file, not in toolchain.

Build MPICH with CMake

I need to force my Cmake to build and link my MPI code with MPICH. My MPICH is installed using the Ubuntu Package manager, in a standard location /usr/lib/mpich/. However, CMake still looks for the OpenMPI libraries, which I do not use. How can I instruct CMake to look for MPICH instead?
Below, you can see the output of some basic diagnostics:
$ whereis openmpi
openmpi:
$ whereis mpich
mpich: /usr/lib/mpich /usr/include/mpich
$ mpicc -v
mpicc for MPICH version 3.2
Below, I also provide the Cmake script and the errors I get from cmake and the mpirun.mpich. My Cmake is 3.5.1 and I run on Ubuntu Xenial 16.04.
cmake_minimum_required(VERSION 3.0)
message (STATUS "Adding mpiService")
find_package(MPI REQUIRED)
set(CMAKE_C_COMPILER mpicc)
set(CMAKE_CXX_COMPILER mpicxx)
set(MPI_GUESS_LIBRARY_NAME MPICH2)
message(STATUS ${MPI_INCLUDE_PATH})
message(STATUS ${MPI_C_LIBRARIES})
#add_definitions(-DOMPI_SKIP_MPICXX)
add_executable(mpiService main.cpp)
set(CMAKE_VERBOSE_MAKEFILE ON)
include_directories(SYSTEM ${MPI_INCLUDE_PATH})
target_link_libraries(
mpiService
PRIVATE
${MPI_C_LIBRARIES}
)
From the Cmake STATUS I get the following output:
/usr/lib/openmpi/include/openmpi/opal/mca/event/libevent2021/libevent/usr/lib/openmpi/include/openmpi/opal/mca/event/libevent2021/libevent/include/usr/lib/openmpi/include/usr/lib/openmpi/include/openmpi
/usr/lib/openmpi/lib/libmpi.so
And when I run the binary I get the following:
ubuntu#node1:~$ mpirun.mpich -np 2 --host node1,node2 mpiService
mpiService: error while loading shared libraries: libmpi.so.12: cannot open shared object file: No such file or directory
mpiService: error while loading shared libraries: libmpi.so.12: cannot open shared object file: No such file or directory
How can I instruct CMake to look for MPICH instead?
According to FindMPI documentation, you may set MPI_<lang>_COMPILER variable to the desired MPI compiler:
Set MPI_<lang>_COMPILER to the MPI wrapper (mpicc, etc.) of your
choice and reconfigure. FindMPI will attempt to determine all the
necessary variables using THAT compiler's compile and link flags.
set(MPI_CXX_COMPILER <path-to-mpich-compiler>)
find_package(MPI REQUIRED)
Alternatively, since CMake version 3.10, variable MPI_EXECUTABLE_SUFFIX can be set instead:
A suffix which is appended to all names that are being looked for. For instance you may set this to .mpich or .openmpi to prefer the one or the other on Debian and its derivatives.
set(MPI_EXECUTABLE_SUFFIX ".mpich")
find_package(MPI REQUIRED)
Herewith my current solution.
find_package(MPI REQUIRED)
# ----------------
# This is the only thing that made it work
# ----------------
set(MPI_C_LIBRARIES "/usr/lib/mpich/lib/libmpich.so")
set(MPI_INCLUDE_PATH "/usr/include/mpich")
# ----------------
add_executable(mpiService main.cpp)
include_directories(SYSTEM ${MPI_INCLUDE_PATH})
target_link_libraries(
mpiService
${MPI_C_LIBRARIES}
)
I personally do not like this solution, as I have to explicitly specify the path. Any other proposed solution was still building with OpenMPI. If I find a better alternative, I will re-post.

error while working with Makefile project

I am trying to debug my Makefile based project which I have imported in CLion. I created a simple CMake file as below
cmake_minimum_required(VERSION 2.8.4)
project(Project1)
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ")
add_custom_target(myProject COMMAND make -j4 DEBUG=1
CLION_EXE_DIR=${PACKAGE_DIR})
CMake tool shows me error: CMake executable not specified. I tried adding add_executable(myProject ${SOURCE_FILES}) with correct source files, but still same error.
Where as on Edit Configurations page, I cannot select any Configuration. The drop down for Configuration is empty. At the bottom I get error Error: Configuration is not specified..
When I try to debug the program, I get a warning message Configuration is still incorrect. Do you want to edit it again? I click on Continue Anyway, which compiles the program as I expect and it generates the correct executable file as well. But it cannot run the executable because of the errors in the Configurations.
I assume "CMake executable" refers to the location of the executable cmake which is called to configure your project. Probably you have to search for a setting in CLion where you can define /usr/bin/cmake or whereever your cmake resides.
This solved the problem for me (Ubuntu):
sudo apt-get install cmake

CMake error: For MinGW make to work correctly sh.exe must NOT be in your path

I am trying to built a "Hello World" project with Qt 5 and cmake under MinGW.
This is the CMakeLists.txt file (taken from the on-line doc):
project(Qt5_cmake_test)
cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.1.1/5.1.1/mingw48_32")
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Find the QtWidgets library
find_package(Qt5Widgets)
# Add the source files from the current directory
aux_source_directory(. SRC_LIST)
# Tell CMake to create the executable
add_executable(${PROJECT_NAME} WIN32 ${SRC_LIST})
# Use the Widgets module from Qt5
target_link_libraries(${PROJECT_NAME} Qt5::Widgets)
The source code is the one generated automatically when creating a new project (which produces an empty window).
Configuring from the Windows command prompt with: cmake -G "MinGW Makefiles" ..\Qt5_cmake_test
I get these errors:
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeMinGWFindMake.cmake:20 (message):
sh.exe was found in your PATH, here:
C:/Program Files (x86)/Git/bin/sh.exe
For MinGW make to work correctly sh.exe must NOT be in your path.
Run cmake from a shell that does not have sh.exe in your PATH.
If you want to use a UNIX shell, then use MSYS Makefiles.
Call Stack (most recent call first):
CMakeLists.txt:8 (project)
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER
CMake Error: Could not find cmake module file:C:/Users/pietro.mele/projects/tests/buildSystem_test/Qt5_cmake_test-build/CMakeFiles/2.8.11.2/CMakeCCompiler.cmake
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER
CMake Error: Could not find cmake module file:C:/Users/pietro.mele/projects/tests/buildSystem_test/Qt5_cmake_test-build/CMakeFiles/2.8.11.2/CMakeCXXCompiler.cma
ke
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
So it seems it is not able to find the compiler. Is there a way to let cmake find it on its own, or just giving it the CMAKE_PREFIX_PATH directory?
Do I have to manually specify all those variables in the makefile or as environment variables in Windows?
I tried both from the standard Windows command prompt and from the one provided by Qt, with the same result. Is it OK to build from the Windows command prompt, or should I do it from the MinGW's shell?
Platform:
Qt 5.1
CMake 2.8.11.2
MinGW/GCC 4.8
Windows 7
Get the git path out of your PATH before running cmake.
Here is the magic to do that:
set PATH=%PATH:C:/Program Files (x86)/Git/bin;=%
This CMakeLists.txt file works properly:
project(Qt5_cmake_test)
cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.1.1/5.1.1/mingw48_32")
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Find the Qt libraries
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
# Add the source files from the current directory
aux_source_directory(. SRC_LIST)
# Tell CMake to create the executable
add_executable(${PROJECT_NAME} WIN32 ${SRC_LIST})
# Use Qt5 modules
target_link_libraries(${PROJECT_NAME}
Qt5::Widgets
Qt5::WinMain)
The changes are:
Added find_package(Qt5Core REQUIRED).
Added Qt5::WinMain to target_link_libraries.
In some of my answer here on SO, I have described. CMake does not like sh.exe.
sh.exe was found in your PATH, here:
C:/Program Files (x86)/Git/bin/sh.exe
Solution : Rename C:/Program Files (x86)/Git/bin/sh.exe shortly.
For example:
C:/Program Files (x86)/Git/bin/shxx.exe
But do not forget when everything is built. rename properly again.

CMake cannot find OpenMP

I am trying to compile with OpenMP. My CMakeLists.txt contains the line
find_package(OpenMP REQUIRED)
and CMake errors out with
CMake Error at /opt/ros/groovy/share/catkin/cmake/catkinConfig.cmake:72 (find_package):
Could not find a configuration file for package openmp.
Set openmp_DIR to the directory containing a CMake configuration file for
openmp. The file will have one of the following names:
openmpConfig.cmake
openmp-config.cmake
Checking my filesystem, I see that I have /usr/share/cmake-2.8/Modules/FindOpenMP.cmake but no openmpConfig.cmake or openmp-config.cmake. What do I need to do to fix this?
CMake has a FindOpenMP module even in 2.x versions. See http://www.cmake.org/cmake/help/v3.0/module/FindOpenMP.html
So I'll do this:
OPTION (USE_OpenMP "Use OpenMP" ON)
IF(USE_OpenMP)
FIND_PACKAGE(OpenMP)
IF(OPENMP_FOUND)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
ENDIF()
ENDIF()
According to the Modern CMake online book, this is how you configure OpenMP support with CMake:
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
target_link_libraries(MyTarget PUBLIC OpenMP::OpenMP_CXX)
endif()
What you definitely should not do is to add flags like -fopenmp manually (like the accepted answer recommends) because that may not be portable.
OpenMp is not a package, if it's supported, it comes as a part of the your compiler. Try setting CMAKE_C_FLAGS or CMAKE_CXX_FLAGS accordingly. e.g:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp") activates OpenMP for compiling C sources when gcc is used. For other compilers, you should first detect the compiler and then add appropriate flags
iNFINITEi's answer doesn't work for me.
I use Ubuntu, trying to compile some code with OpenCV static library. After linking, I got this:
'"/usr/bin/ld: /usr/local/lib/libopencv_core.a(parallel.cpp.o): undefined reference to symbol 'omp_set_dynamic##OMP_1.0'"'
So I tried iNFINITEi's approach, then I have:
'CMake Error at /usr/local/share/cmake-3.13/Modules/FindPackageHandleStandardArgs.cmake:211 (message):
No REQUIRED_VARS specified for FIND_PACKAGE_HANDLE_STANDARD_ARGS()
Call Stack (most recent call first):
/usr/local/share/cmake-3.13/Modules/FindOpenMP.cmake:513 (find_package_handle_standard_args)
CMakeLists.txt:8 (FIND_PACKAGE)'
At last, I add "-fopenmp=libomp" to CMAKE_CXX_FLAGS, solved my problem.
You should install libomp with brew install libomp
i use macOS and it worked smoothly for me.