Linking C++ with CUDA objects links against libstdc++ from CUDA host compiler
I'm on Arch Linux with CUDA 10.1 and gcc 9.1, where /opt/cuda/bin/gcc
links to gcc 8.3.
I am not sure, if the following is a CMake or a CUDA issue.
When linking C++ (using gcc 9.1) and CUDA (using gcc 8.3) objects, libstdc++ from gcc 8.3 is chosen, which is caused by a -L/usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0
flag in the linker command (CMakeFiles/bug.dir/link.txt in the example below).
Without this flag, everything works just fine.
Another possible workaround is to use -static-libstdc++
.
Example
This example will fail to link, since the symbol std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream()
exists in libstdc++.so.6.0.26 (gcc 9.1) but not in libstdc++.so.6.0.25 (gcc 8.3).
a.cpp
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::ostringstream os;
os << "hello world" << std::endl;
std::cout << os.str();
return 0;
}
b.cu
void f() {}
CMakeLists.txt
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(bug LANGUAGES CXX CUDA)
add_executable(bug a.cpp b.cu)
# workaround
# target_link_libraries(bug PRIVATE -static-libstdc++)