HIP: C++ linker fails to link static library on NVIDIA platform
A static library that contains HIP sources cannot be linked into a C++ executable when LINKER_LANGUAGE
is set to CXX
.
When the linked library only exports host symbols, a device linking step shouldn't be required.
Reproducer:
cmake_minimum_required(VERSION 3.27)
set(CMAKE_HIP_PLATFORM nvidia)
project(asd LANGUAGES CXX HIP CUDA)
add_library(hip STATIC library.hip)
add_executable(main-hip main.cpp)
target_link_libraries(main-hip PRIVATE hip)
set_target_properties(main-hip PROPERTIES LINKER_LANGUAGE CXX)
add_library(cuda STATIC library.cu)
add_executable(main-cuda main.cpp)
target_link_libraries(main-cuda PRIVATE cuda)
set_target_properties(main-cuda PROPERTIES LINKER_LANGUAGE CXX)
Configure with cmake -DCMAKE_HIP_PLATFORM=nvidia
and try to build. It fails with the error message
[ 50%] Linking CXX executable main-hip
/opt/cmake-dev/bin/cmake -E cmake_link_script CMakeFiles/main-hip.dir/link.txt --verbose=1
/usr/bin/c++ "CMakeFiles/main-hip.dir/main.cpp.o" -o main-hip libhip.a -lcudadevrt -lcudart_static -lrt -lpthread -ldl
/usr/bin/ld: cannot find -lcudadevrt
/usr/bin/ld: cannot find -lcudart_static
collect2: error: ld returned 1 exit status
Comparing the command line to a static lib with cuda sources:
[100%] Linking CXX executable main-cuda
/opt/cmake-dev/bin/cmake -E cmake_link_script CMakeFiles/main-cuda.dir/link.txt --verbose=1
/usr/bin/c++ "CMakeFiles/main-cuda.dir/main.cpp.o" -o main-cuda -L/usr/local/cuda/targets/x86_64-linux/lib libcuda.a -lcudadevrt -lcudart_static -lrt -lpthread -ldl
The library path to the cuda runtime libraries was not passed to the linker.
Note that using the CUDA
language or on the AMD HIP platform this works.
Ref: !8817 (merged)