C++ 如何编译C++;有幼兽图书馆吗?

C++ 如何编译C++;有幼兽图书馆吗?,c++,compilation,cuda,cub,C++,Compilation,Cuda,Cub,我正在使用CUB设备函数,就像这里的示例()。在上面的示例中,我能够使用nvcc编译.cu源文件 但是,我想知道是否可以在.cpp源文件中调用CUB设备函数并编译.cpp源文件(使用nvcc或g++)?我知道它可能用于推力,因为这个例子对我很有用 目前,我只是将main函数移动到一个新的main.cpp文件中,并在main.cpp中包含cub头文件,但由于相同的错误,我无法使用nvcc或g++编译它,部分错误消息: /home/xx/cub/cub/block/specializations/.

我正在使用CUB设备函数,就像这里的示例()。在上面的示例中,我能够使用
nvcc
编译.cu源文件

但是,我想知道是否可以在.cpp源文件中调用
CUB
设备函数并编译.cpp源文件(使用
nvcc
g++
)?我知道它可能用于
推力
,因为这个例子对我很有用

目前,我只是将main函数移动到一个新的main.cpp文件中,并在main.cpp中包含cub头文件,但由于相同的错误,我无法使用nvcc或g++编译它,部分错误消息:

/home/xx/cub/cub/block/specializations/../../block/../util_type.cuh:261:5: error: ‘__host__’ does not name a type; did you mean ‘__loff_t’?
         __host__ __device__ __forceinline__ NullType& operator =(const T&) { return *this; }
         ^~~~~~~~
         __loff_t
/home/xx/cub/cub/block/specializations/../../block/../util_type.cuh:316:19: error: ‘short4’ was not declared in this scope
     __CUB_ALIGN_BYTES(short4, 8)
                       ^
/home/xx/cub/cub/block/specializations/../../block/../util_type.cuh:314:52: error: ISO C++ forbids declaration of ‘__align__’ with no type [-fpermissive]
         { enum { ALIGN_BYTES = b }; typedef __align__(b) t Type; };
                                                        ^
/home/xx/cub/cub/block/specializations/../../block/../util_type.cuh:545:9: error: ‘__host__’ does not name a type; did you mean ‘__loff_t’?
             __host__ __device__ __forceinline__ CubVector operator+(const CubVector &other) const {         \
             ^
/home/xx/cub/cub/block/specializations/../../block/../util_arch.cuh:64:38: error: ‘__host__’ does not name a type; did you mean ‘CUhostFn’?
         #define CUB_RUNTIME_FUNCTION __host__ __device__
                                      ^
/home/xx/cub/cub/device/../iterator/arg_index_input_iterator.cuh:144:25: error: ‘__forceinline__’ does not name a type; did you mean ‘__thrust_forceinline__’?
     __host__ __device__ __forceinline__ ArgIndexInputIterator(
                         ^~~~~~~~~~~~~~~
                         __thrust_forceinline__
/home/xx/cub/cub/device/device_reduce.cuh:148:12: error: ‘cudaError_t’ does not name a type; did you mean ‘cudaError_enum’?
     static cudaError_t Reduce(
            ^~~~~~~~~~~
            cudaError_enum
以下是我的源文件:

设备.h

#pragma once

#include <cub/cub.cuh>

void scan_on_device();
第一步进行得很顺利,但第二步给出了上述错误。欢迎提出任何意见。也许我弄乱了一些链接(CUDA或CUB)?

< P> Cub头库(例如,代码> Cub. Cuh < /Cuth>)包含CUDA C++代码。这样的代码不能由像g++这样的普通主机编译器编译。如果您尝试这样做,您将得到编译错误

但是,您的项目不要求
cub.cuh
位于
device.h
头文件中,也不要求由g++编译
cub.cuh
device.h
头文件中唯一需要的是
scan\u on\u device()
的函数原型


因此,如果在函数实现文件
device.cu
中包含cub头文件,并将其删除到项目中的其他位置,则代码将被编译。

删除
#从
device.h
host.cpp
中包含
。仅将其放入
设备.cu
。然后你的代码应该编译。有人应该为这个问题添加一个答案,以供将来的访问者使用。。。。。
#include "device.h"

void scan_on_device()
{
  // Declare, allocate, and initialize device pointers for input and output
  int num_items = 7;
  int *d_in;
  int h_in[]  = {8, 6, 7, 5, 3, 0, 9};
  int sz = sizeof(h_in)/sizeof(h_in[0]);
  int *d_out; // e.g., [ , , , , , , ]
  cudaMalloc(&d_in,  sz*sizeof(h_in[0]));
  cudaMalloc(&d_out, sz*sizeof(h_in[0]));
  cudaMemcpy(d_in, h_in, sz*sizeof(h_in[0]), cudaMemcpyHostToDevice);
  printf("\nInput:\n");
  for (int i = 0; i < sz; i++) printf("%d ", h_in[I]);
  // Determine temporary device storage requirements
  void *d_temp_storage = NULL;
  size_t temp_storage_bytes = 0;
  cub::DeviceScan::InclusiveSum(d_temp_storage, temp_storage_bytes, d_in, d_out, num_items);
  // Allocate temporary storage
  cudaMalloc(&d_temp_storage, temp_storage_bytes);
  // Run inclusive prefix sum
  cub::DeviceScan::InclusiveSum(d_temp_storage, temp_storage_bytes, d_in, d_out, num_items);
// d_out s<-- [8, 14, 21, 26, 29, 29, 38]
  cudaMemcpy(h_in, d_out, sz*sizeof(h_in[0]), cudaMemcpyDeviceToHost);
  printf("\nOutput:\n");
  for (int i = 0; i < sz; i++) printf("%d ", h_in[i]);
  printf("\n");
}
#include "device.h"
#include <cub/cub.cuh>

int main(void)
{
    scan_on_device();

    return 0;
}
nvcc -O2 -c device.cu -I/home/xx/cub
g++  -O2 -c host.cpp  -I/usr/local/cuda/include/ -I/home/xx/cub
g++ -o tester device.o host.o -L/usr/local/cuda/lib64 -lcudart