背景
我正在尝试使用CUDA后端学习SYCL (我用这些指示编译了这些指示编译器,向量加法成功了)。然而,第二天,我无法通过使用CMake从书中获得第一个示例,但是使用命令行和调用编译器直接解决了这个问题。
#include <CL/sycl.hpp>
#include <iostream>
#include <string>
using namespace sycl;
const std::string secret =
"Ifmmp-!xpsme\"\012J(n!tpssz-!Ebwf/!"
"J(n!bgsbje!J!dbo(u!ep!uibu/!.!IBM\01";
const auto sz = secret.size();
int main() {
queue Q;
char* result = malloc_shared<char>(sz, Q);
std::memcpy(result, secret.data(), sz);
Q.parallel_for(sz, [=](auto& i) {
result[i] -= 1;
}).wait();
std::cout << result << '\n';
}
我使用这个脚本来设置环境变量:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/shino/software/sources/llvm/build/lib/
export DPCPP_HOME=/home/shino/software/sources
export CUDA_LIB_PATH=/usr/local/cuda/lib64/stubs/
export CXX=/home/shino/software/sources/llvm/build/bin/clang++
export CC=/home/shino/software/sources/llvm/build/bin/clang
export CUDA_PATH=/usr/local/cuda
这是我的CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(sycl-convolution)
add_executable(convolution main.cpp)
target_include_directories(convolution PRIVATE /home/shino/software/sources/llvm/sycl/include)
target_compile_features(convolution PRIVATE cxx_std_17)
target_compile_options(convolution PRIVATE -fsycl -fsycl-targets=nvptx64-nvidia-cuda --cuda-path=$ENV{CUDA_PATH})
target_link_directories(convolution PRIVATE /home/shino/software/sources/llvm/build/lib/)
下面是链接器错误的前几行:
/usr/bin/ld: CMakeFiles/convolution.dir/main.cpp.o: in function `main':
main-4cf7ed.cpp:(.text+0x122): undefined reference to `cl::sycl::event::wait()'
/usr/bin/ld: CMakeFiles/convolution.dir/main.cpp.o: in function `main::{lambda(auto:1&)#1} cl::sycl::queue::submit<cl::sycl::queue::parallel_for_impl<cl::sycl::detail::auto_name, main::{lambda(auto:1&)#1}, 1>(cl::sycl::range<1>, main::{lambda(auto:1&)#1}, cl::sycl::detail::code_location const&)::{lambda(cl::sycl::handler&)#1}>(cl::sycl::queue::parallel_for_impl<cl::sycl::detail::auto_name, main::{lambda(auto:1&)#1}, 1>(cl::sycl::range<1>, main::{lambda(auto:1&)#1}, cl::sycl::detail::code_location const&)::{lambda(cl::sycl::handler&)#1}, cl::sycl::detail::code_location const)':
main-4cf7ed.cpp:(.text+0x3c0): undefined reference to `cl::sycl::event::event()'
/usr/bin/ld: main-4cf7ed.cpp:(.text+0x3cc): undefined reference to `cl::sycl::queue::is_host() const'
/usr/bin/ld: main-4cf7ed.cpp:(.text+0x47b): undefined reference to `cl::sycl::queue::submit_impl_and_postprocess(std::function<void (cl::sycl::handler&)>, cl::sycl::detail::code_location const&, std::function<void (bool, bool, cl::sycl::event&)> const&)'
/usr/bin/ld: main-4cf7ed.cpp:(.text+0x543): undefined reference to `cl::sycl::queue::submit_impl(std::function<void (cl::sycl::handler&)>, cl::sycl::detail::code_location const&)'
/usr/bin/ld: CMakeFiles/convolution.dir/main.cpp.o: in function `void cl::sycl::handler::parallel_for_lambda_impl<cl::sycl::detail::auto_name, main::{lambda(auto:1&)#1}, 1>(cl::sycl::range<1>, main::{lambda(auto:1&)#1})':
main-4cf7ed.cpp:(.text+0xa23): undefined reference to `cl::sycl::handler::GetRangeRoundingSettings(unsigned long&, unsigned long&, unsigned long&)'
我该怎么解决这个问题?我的直觉告诉我,编译器在直接调用时调用链接器的方式是不同的,而且我没有指定链接过程中一些非常重要的部分。
使用详细模式调用编译器提供了以下链接命令:
"/usr/bin/ld" -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /lib/x86_64-linux-gnu/crt1.o /lib/x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/home/shino/software/sources/llvm/build/bin/../lib -L/lib -L/usr/lib /tmp/main-7d74d3.o /tmp/a-cacdd3.o -lstdc++ -lm -lgcc_s -lgcc -lsycl -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/9/crtend.o /lib/x86_64-linux-gnu/crtn.o
除了链接器错误之外,还有更多的警告,这表明编译器没有处于正确的“模式”。我不知道下一步从哪里开始挖掘。
发布于 2021-10-02 12:37:20
您缺少要链接的实际库,请使用target_link_libraries
指令指定它们。在科马克文件中查找更多信息。
注意,target_link_directories
是不够的-它只指定链接器应该搜索的库的路径。
更具体地说,通过阅读发布的链接命令,您应该添加:
target_link_libraries(convolution PRIVATE sycl)
将SYSTEM
添加到target_include_directories
指令中将使警告保持沉默:
target_include_directories(convolution SYSTEM PRIVATE /home/shino/software/sources/llvm/sycl/include)
https://stackoverflow.com/questions/69416737
复制相似问题