问题描述:

In a project with many .cu files and a .h file, I have some constants defined in my main.cu like this (shown just one as example):

__device__ __constant__ unsigned int x[1];

#include "second.cu"

... some code...

In the file second.cu I am trying to use that constant, like this:

cudaMemcpyToSymbol(x, y, sizeof(xx));

But Eclipse is giving me the error: identifier "x" is undefined.

I noticed that #includes in my main.cu, like the header.h, I need to specifically add in all the .cu files again. Which produced some redefinition problems that I solved using #pragma once.

I am new to Eclipse in general, found some complains about the CDT regarding include files not being indexed. I tried the Index Rebuild/Update/Freshen/Re-resolve method that worked for some in this regard, but with no luck with my problems.

Also, tried disabling the 'heuristic resolution of includes' in Properties -> Indexer. I thought I got it for a few moments but then the error showed up again.

Any ideas to solve this problem?

网友答案:

This is a C/C++ problem and have nothing to do with CUDA.

Generally people don't include source files like .cu .cpp .c. Only header files like .h should be included.

If you have a global variable int x need to be referenced in many source files. You could define it in one souce file as

// main.cu
int x;
...

declare it in a header file as

// main.h
extern int x;
...

and include this header file in all the source files you will reference that variable as

// second.cu
#include "main.h"
void foo() {
    int local=x;
}
...

and

// third.cu
#include "main.h"
void bar() {
    int private=x;
}
...
相关阅读:
Top