使用未声明的标识符'temp'

问题描述 投票:0回答:1

下面的c ++代码使我错误使用了未声明的标识符'temp'

/ home / test / include / memory.h

#ifndef MEMORY_H
#define MEMORY_H

#if __ENABLE_MEMORY

__device__ int temp = 50;
extern "C" inline __device__ void* memory(){
     ...
     temp =  temp + 100;
     ...
}

#endif
#endif

/ home / test / include / internal_memory.h

#ifndef INTERNAL_MEMORY_H
#define INTERNAL_MEMORY_H

#ifndef __ENABLE_MEMORY
#define __ENABLE_MEMORY 1
#endif

#if __ENABLE_MEMORY
extern "C" __device__ void* memory();
static inline __device__ void* call_memory(){ return memory();}
#endif

#include <memory.h> 
#endif

/ home / test / main.cpp

 #include "internal_memory.h"
 ..
 .
 .
 void show(){
    std::cout << "temp is: " << temp << std::endl;
 }
 .
 .
 .

我正在使用clang ++版本11.0.0编译代码。

clang --version看起来如下:

  clang version 11.0.0
  Target: x86_64-unknown-linux-gnu
  Thread model: posix
  InstalledDir: /opt/rocm/llvm/bin

我正在如下编译代码。

  /opt/rocm/llvm/bin/clang++ -DDEBUG -D__x86_64__ -I/home/test/include -I/home/test -g -fPIC -std=c++14 -o main.o -c main.cpp

我在这里想念什么。

c++ clang++
1个回答
0
投票

函数memory具有__device__限定符(我想您正在编译CUDA程序)。

memory是指temp,它是主机存储器中的全局变量。您可能想要__device__ int temp = 50;

要查询temp,您需要将其值复制回主机。沿线

int host;
cudaMemcpy(&host, &temp, sizeof(int), cudaMemcpyDeviceToHost);
© www.soinside.com 2019 - 2024. All rights reserved.