CUDA: Device linking fails with FindMPI because of unknown -pthread flag
See #17929 (closed) and my comment there. That issue is about the -pthread
flag in the compiler arguments. This issue is about the -pthread
in the linker arguments.
A minimal example:
CMakeLists.txt
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(sandox LANGUAGES CXX CUDA)
find_package(MPI REQUIRED)
add_executable(mytest mytest.cu)
target_link_libraries(mytest PRIVATE MPI::MPI_CXX)
mytest.cu
#include <iostream>
int main( int, char** )
{
std::cout << "Hello World\n";
return 0;
}
fails when building with
mkdir build && cd $_ && cmake .. && make VERBOSE=1
because of this error:
-- The CXX compiler identification is GNU 7.3.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
-- Found MPI_CXX: /usr/lib/x86_64-linux-gnu/openmpi/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: /opt/cmakePThreadsBug/build
/opt/cmake/bin/cmake -S/opt/cmakePThreadsBug -B/opt/cmakePThreadsBug/build --check-build-system CMakeFiles/Makefile.cmake 0
/opt/cmake/bin/cmake -E cmake_progress_start /opt/cmakePThreadsBug/build/CMakeFiles /opt/cmakePThreadsBug/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/opt/cmakePThreadsBug/build'
make -f CMakeFiles/mytest.dir/build.make CMakeFiles/mytest.dir/depend
make[2]: Entering directory '/opt/cmakePThreadsBug/build'
cd /opt/cmakePThreadsBug/build && /opt/cmake/bin/cmake -E cmake_depends "Unix Makefiles" /opt/cmakePThreadsBug /opt/cmakePThreadsBug /opt/cmakePThreadsBug/build /opt/cmakePThreadsBug/build /opt/cmakePThreadsBug/build/CMakeFiles/mytest.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/opt/cmakePThreadsBug/build'
make -f CMakeFiles/mytest.dir/build.make CMakeFiles/mytest.dir/build
make[2]: Entering directory '/opt/cmakePThreadsBug/build'
[ 33%] Linking CUDA device code CMakeFiles/mytest.dir/cmake_device_link.o
/opt/cmake/bin/cmake -E cmake_link_script CMakeFiles/mytest.dir/dlink.txt --verbose=1
/usr/local/cuda/bin/nvcc -Xcompiler=-fPIC -Wno-deprecated-gpu-targets -shared -dlink CMakeFiles/mytest.dir/mytest.cu.o -o CMakeFiles/mytest.dir/cmake_device_link.o -L/usr/lib -pthread
nvcc fatal : Unknown option 'pthread'
CMakeFiles/mytest.dir/build.make:85: recipe for target 'CMakeFiles/mytest.dir/cmake_device_link.o' failed
make[2]: *** [CMakeFiles/mytest.dir/cmake_device_link.o] Error 1
make[2]: Leaving directory '/opt/cmakePThreadsBug/build'
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/mytest.dir/all' failed
make[1]: *** [CMakeFiles/mytest.dir/all] Error 2
make[1]: Leaving directory '/opt/cmakePThreadsBug/build'
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
It looks to me like e2d54e5c only has to be applied somehow to the linker flags two lines below.
Edited by Brad King