CMake doesn't pass CUDA arguments? - cmake

I am trying to compile a library with both, C++ and CUDA source files. I am using GNU make with CMake. My compiler of choice is clang, since CUDA only supports gcc up to version 5 and Debian 9 has gcc 6 as its oldest version and I have to use software that is provided by the Debian 9 or 10 repositories.
CMake version is 3.9.0
clang version is 3.8.1
cc and c++ in /usr/bin correctly link to clang and clang++ ẃhich also link to the correct files.
Unfortunately the initial checks of CMake for CUDA fail although everything, as far as I can see, seems to be set up correctly. It looks like the arguments aren't passed correctly to the CUDA compiler.
This is a part of my project's main CMake file:
cmake_minimum_required (VERSION 3.9.0 FATAL_ERROR)
project (dev)
find_package(CUDA REQUIRED)
set(CUDA_HOST_COMPILATION_CPP ON)
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -Wno-deprecated-gpu-targets -ccbin clang-3.8")
The library's CMake file looks like this:
cmake_minimum_required(VERSION 3.9.0 FATAL_ERROR)
project (libname LANGUAGES CXX CUDA)
file(GLOB SOURCES "*.cu" "*.cpp")
add_library(libname ${SOURCES})
set_target_properties(libname PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
set_target_properties(libname PROPERTIES POSITION_INDEPENDENT_CODE ON)
This is the output of CMake:
-- The C compiler identification is Clang 3.8.1
-- The CXX compiler identification is Clang 3.8.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found CUDA: /usr (found version "8.0")
-- The CUDA compiler identification is unknown
-- Check for working CUDA compiler: /usr/bin/nvcc
-- Check for working CUDA compiler: /usr/bin/nvcc -- broken
The CUDA/nvcc tests of CMake fails with the following errors:
Change Dir: /home/user/projects/hamonIC-linux-experimental/current_state/working_copy/code/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_6d1a9/fast"
/usr/bin/make -f CMakeFiles/cmTC_6d1a9.dir/build.make
CMakeFiles/cmTC_6d1a9.dir/build
make[1]: Entering directory
'/home/user/projects/hamonIC-linux-experimental/current_state/working_copy/code/build/CMakeFiles/CMakeTmp'
Building CUDA object CMakeFiles/cmTC_6d1a9.dir/main.cu.o
/usr/bin/nvcc -x cu -c
/home/user/projects/hamonIC-linux-experimental/current_state/working_copy/code/build/CMakeFiles/CMakeTmp/main.cu
-o CMakeFiles/cmTC_6d1a9.dir/main.cu.o
nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are
deprecated, and may be removed in a future release (Use
-Wno-deprecated-gpu-targets to suppress warning).
ERROR: No supported gcc/g++ host compiler found, but clang-3.8 is
available.
Use 'nvcc -ccbin clang-3.8' to use that instead.
CMakeFiles/cmTC_6d1a9.dir/build.make:65: recipe for target
'CMakeFiles/cmTC_6d1a9.dir/main.cu.o' failed
make[1]: *** [CMakeFiles/cmTC_6d1a9.dir/main.cu.o] Error 1
make[1]: Leaving directory
'/home/user/projects/hamonIC-linux-experimental/current_state/working_copy/code/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_6d1a9/fast' failed
make: *** [cmTC_6d1a9/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
hic_iplibrary/source/ciccommon/CMakeLists.txt:7 (project)

The CUDA NVCC flags in CMake have to be semicolon delimited and not whitespace delimited.
Change the flags in your CMakeLists.txt to use:
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};-Wno-deprecated-gpu-targets;-ccbin=clang-3.8")
and that should allow CMake to pass your flags to NVCC.

Related

MinGW on Windows: cmake arguments to find wxWidgets?

Similar questions already exist but I haven't found an answer that works.
I need to build a wxWidgets project in Windows using MinGW, I would like to use the cmake command from the command line (I installed mingw, cmake and bash using chocolatey)
I would like to avoid compiling wxWidgets so I am using the pre-built binaries MinGW-w64 10.2 (Headers + Dev x64 + Release x64), I unpack them to C:\wxWidgets-3.1.5
I've tried a number of combinations of arguments for cmake but haven't found one that works on the first run, I say first run because I've found one that works on the second:
cmake .. -G "MinGW Makefiles" \
-DwxWidgets_ROOT_DIR=/c/wxWidgets-3.1.5/ \
-DwxWidgets_LIB_DIR=/c/wxWidgets-3.1.5/lib/gcc1020_x64_dll/
I am not a cmake expert but I imagine that by specifying these arguments in the first run they are cached and in the second run they are used, bypassing the search.
What I would like to know is what arguments I have to give to get them to be found correctly, _CONFIGURATION, _ROOT_DIR, _LIBRARIES, _INCLUDE_DIRS don't seem to have any effect.
↓ edit ↓
CMakelists.txt:
cmake_minimum_required(VERSION 3.18)
project(Test)
set(wxWidgets_USE_LIBS)
find_package(wxWidgets REQUIRED)
if(wxWidgets_FOUND)
include(${wxWidgets_USE_FILE})
add_executable(MyTest WIN32 main.cpp)
target_link_libraries(MyTest ${wxWidgets_LIBRARIES})
else(wxWidgets_FOUND)
message("wxWidgets not found!")
endif(wxWidgets_FOUND)
command line used:
cmake .. -G "MinGW Makefiles" -DwxWidgets_ROOT_DIR=/c/wxWidgets-3.1.5/ -DwxWidgets_LIB_DIR=/c/wxWidgets-3.1.5/lib/gcc1020_x64_dll/ -DwxWidgets_wxrc_EXECUTABLE=/c/wxWidgets-3.1.5/lib/gcc1020_x64_dll/wxrc.exe -DCMAKE_BUILD_TYPE=Release -DwxWidgets_LIBRARIES=/c/wxWidgets-3.1.5/lib/gcc1020_x64_dll/ -DwxWidgets_INCLUDE_DIRS=/c/wxWidgets-3.1.5/include/
first run:
-- The C compiler identification is GNU 10.2.0
-- The CXX compiler identification is GNU 10.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/ProgramData/chocolatey/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/ProgramData/chocolatey/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Could NOT find wxWidgets (missing: wxWidgets_LIBRARIES wxWidgets_INCLUDE_DIRS core base)
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Alex/Documents/Progetti/wx-test/build
wxWidgets not found!
second run:
-- Found wxWidgets: debug;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxmsw31ud_core.a;optimized;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxmsw31u_core.a;debug;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxbase31ud.a;optimized;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxbase31u.a;debug;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxbase31ud_net.a;optimized;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxbase31u_net.a;debug;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxpngd.a;optimized;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxpng.a;debug;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxtiffd.a;optimized;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxtiff.a;debug;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxjpegd.a;optimized;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxjpeg.a;debug;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxzlibd.a;optimized;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxzlib.a;debug;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxregexud.a;optimized;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxregexu.a;debug;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxexpatd.a;optimized;C:/wxWidgets-3.1.5/lib/gcc1020_x64_dll/libwxexpat.a;winmm;comctl32;uuid;oleacc;uxtheme;rpcrt4;shlwapi;version;wsock32 (found version "3.1.5") found components: core base net png tiff jpeg zlib regex expat
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Alex/Documents/Progetti/wx-test/build
if I use find_package(wxWidgets REQUIRED):
CMake Error at C:/Program Files/CMake/share/cmake-3.21/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find wxWidgets (missing: wxWidgets_LIBRARIES
wxWidgets_INCLUDE_DIRS)
Call Stack (most recent call first):
C:/Program Files/CMake/share/cmake-3.21/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files/CMake/share/cmake-3.21/Modules/FindwxWidgets.cmake:1025 (find_package_handle_standard_args)
CMakeLists.txt:24 (find_package)
Thanks to Process Monitor I noticed that FindwxWidgets.cmake looks for libraries in gcc_dll/ (not gcc2010_x64_dll/), once the directory was renamed cmake -G "MinGW Makefiles" -DwxWidgets_ROOT_DIR=/c/wxWidgets-3.1.5 was enough to find the directory on the first run.

Missing variable is: CMAKE_CUDA_COMPILE_WHOLE_COMPILATION

I am trying to use CMake to compile a library using cuda.
I want cuda to be optional, i.e. I want my library to compile if nvcc is not available on the system.
Here is how I am checking if cuda is available in my CMake:
include(CheckLanguage)
check_language(CUDA)
if (CMAKE_CUDA_COMPILER)
enable_language(CUDA)
else()
message(WARNING "CUDA not found: GPU features won't be available.")
endif ()
Here are the environment variables I am setting:
$> env | grep CXX
CUDAHOSTCXX=/usr/bin/g++-6
CUDACXX=/usr/bin/nvcc
CXX=/usr/bin/g++
Here is the output of the cmake command:
$> cmake ..
-- The CXX compiler identification is GNU 7.4.0
-- Check for working CXX compiler: /usr/bin/g++
-- Check for working CXX compiler: /usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for a CUDA compiler
-- Looking for a CUDA compiler - /usr/bin/nvcc
-- The CUDA compiler identification is NVIDIA 9.1.85
-- Check for working CUDA compiler: /usr/bin/nvcc
-- Check for working CUDA compiler: /usr/bin/nvcc -- works
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Configuring done
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
CMAKE_CUDA_COMPILE_WHOLE_COMPILATION
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
I am not finding anything usefull conserning CMAKE_CUDA_COMPILE_WHOLE_COMPILATION.
How can I make this cmake works? What did I missed?
I just fingure out what the problem was...
I was calling a function
function(enable_cuda_if_available)
# Here CUDA is properly found and variable are correctly set
include(CheckLanguage)
check_language(CUDA)
if (CMAKE_CUDA_COMPILER)
enable_language(CUDA)
else ()
message(WARNING "CUDA not found: GPU features won't be available.")
endif ()
endfunction()
enable_cuda_if_available()
# Here CUDA's variables have not been forwarded to the parent scope and leading to an error
I still don't know how to automatically forward all variables set by enable_cuda_if_available to the parent scope

CMake cannot find OpenMPI head file when I Install it by using conda

I am not very familiar with installing package by conda. I install the OpenMPI by conda install -c conda-forge/label/gcc7 openmpi. After that I write the following CMakeLists.txt file
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(CUDA_MPI LANGUAGES CXX CUDA)
find_package(CUDA REQUIRED)
find_package(MPI REQUIRED)
add_library(Timer STATIC timer.h
timer.cpp)
add_library(Coll STATIC collectives.h
collectives.cu)
add_executable(CUDA_MPI ${CMAKE_CURRENT_SOURCE_DIR}/test/test.cpp)
include_directories(/usr/lib/x86_64-linux-gnu/openmpi/include)
target_link_libraries(CUDA_MPI PUBLIC Timer Coll MPI::MPI_CXX ${CUDA_LIBRARIES})
My configuration log is as the following. It seems that CMake tool can find the library of OpenMPI but it cannot find the head file.
-- The CXX compiler identification is GNU 7.3.0
-- The CUDA compiler identification is NVIDIA 10.0.130
-- Check for working CXX compiler: /home/szhangcj/.conda/envs/distributed/bin/x86_64-conda_cos6-linux-gnu-c++
-- Check for working CXX compiler: /home/szhangcj/.conda/envs/distributed/bin/x86_64-conda_cos6-linux-gnu-c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc
-- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc -- works
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found CUDA: /usr/local/cuda (found version "10.0")
-- Found MPI_CXX: /home/szhangcj/.conda/envs/distributed/lib/libmpi_cxx.so (found version "3.1")
-- Found MPI: TRUE (found version "3.1")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/szhangcj/HPC/MPI/baidu-allreduce/build
The error message from the generating stage is as the following.
Scanning dependencies of target Timer
[ 14%] Building CXX object CMakeFiles/Timer.dir/timer.cpp.o
[ 28%] Linking CXX static library libTimer.a
[ 28%] Built target Timer
Scanning dependencies of target Coll
[ 42%] Building CUDA object CMakeFiles/Coll.dir/collectives.cu.o
/home/szhangcj/HPC/MPI/baidu-allreduce/collectives.cu:9:10: fatal error: mpi.h: No such file or directory
#include <mpi.h>
^~~~~~~
compilation terminated.
CMakeFiles/Coll.dir/build.make:62: recipe for target 'CMakeFiles/Coll.dir/collectives.cu.o' failed
make[2]: *** [CMakeFiles/Coll.dir/collectives.cu.o] Error 1
CMakeFiles/Makefile2:147: recipe for target 'CMakeFiles/Coll.dir/all' failed
make[1]: *** [CMakeFiles/Coll.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I have checked the installation directory and I found that there is no include directory under the openmpi folder. How can I fix this?
What's more, on another machine, I repeat the same process to install the Openmpi by using conda. After that, I even cannot find the MPI library.

Can't find CUDA_INCLUDE_DIRS in latest CMAKE [duplicate]

This question already has answers here:
Listing include_directories in CMake
(3 answers)
Closed 3 years ago.
Since in CMAKE 3.10, CUDA macro is supported by default (https://cmake.org/cmake/help/latest/module/FindCUDA.html).
But I can't find the variable CUDA_INCLUDE_DIRS
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(cmake_and_cuda LANGUAGES CXX CUDA)
message(${CUDA_INCLUDE_DIRS})
the errors are
-- The CXX compiler identification is GNU 5.4.0
-- The CUDA compiler identification is NVIDIA 10.0.130
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc
-- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc -- works
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
CMake Error at CMakeLists.txt:7 (message):
message called with incorrect number of arguments
-- Configuring incomplete, errors occurred!
See also "/home/tumh/code-samples/posts/cmake/build/CMakeFiles/CMakeOutput.log".
Any idea?
Just use the ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}.
In all .cu src files, there is no need to manually add
include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}),
because CMake will do it for you.
But for .cpp src files, if you need to include <cuda_runtime.h>, you must manually add
include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
in your CMakeLists.txt.

Finding GLib with CMake: target_include_directories called with invalid arguments

I have this following (minimal) CMakeLists.txt supposed to find GLib via pkg-config and add the libs to the foo target:
cmake_minimum_required(VERSION 2.10 FATAL_ERROR)
project(foo)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLib REQUIRED glib-2.0)
add_executable(foo foo.cpp)
message(WARNING "libs:" ${GLIB_LIBRARIES})
message(WARNING "includes:" ${GLIB_INCLUDE_DIRS})
target_link_libraries(foo PUBLIC ${GLIB_LIBRARIES})
target_include_directories(foo PUBLIC ${GLIB_INCLUDE_DIRS})
No matter what I try, I get (note the Found glib-2.0 part):
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
-- Checking for module 'glib-2.0'
-- Found glib-2.0, version 2.56.1
CMake Warning at CMakeLists.txt:6 (message):
libs:
CMake Warning at CMakeLists.txt:7 (message):
includes:
CMake Error at CMakeLists.txt:9 (target_include_directories):
target_include_directories called with invalid arguments
-- Configuring incomplete, errors occurred!
See also "/tmp/aaa/CMakeFiles/CMakeOutput.log".
and I cannot see, reading the CMake reference what arguments are invalid (note: this question is different from cmake target_include_directories called with invalid arguments). I also looked at CMake's FindPkgConfig documentation which gives glib as an example and I am not able to reproduce it (${GLIB_VERSION}). I tried GLIB_ and GLIB2_ prefixes and all I get is empty strings.
The messages show the variables are empty, though pkg-config reports values correctly:
$ pkg-config glib-2.0 --cflags --libs
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0
The CMake version I have is 2.10.
Can someone shed light on the issue?
I think your problem is the casing of GLIB_INCLUDE_DIRS and GLIB_LIBRARIES. They should be GLib_INCLUDE_DIRS and GLib_LIBRARIES since you specified "GLib" as the first argument of pkg_check_modules. I'm guessing that the target_include_directories() doesn't like not getting any arguments after the
PUBLIC (although it works for me in cmake 3.5).