Multiple definition error, how to correct it?

I am trying to build a library, multiple functions will need one same data array. This data array has a large number of data, so I put the data in one single file. Say I name it as “a.h”

#ifndef A_H_
#define A_H_

int a[30000] = {1, 0, -1, 0, -1, 0, ......}

#endif

In fun1.cu, I use: #include “a.h” ; In fun2.cu, I also use: #include “a.h”

Compiler gives me error of multiple definition of “a”

How can I correct it?

I understand that if both fun1.cu and fun2.cu need to utilize the same function, common(). I can put the function prototype in a header file, common.h, and can put function definition in a source file, common.cpp. Then both fun1.cu and fun2.cu can use #include “common.h” to include the same function, in this way there is no multiple definition error for using this common().

But how about one common data array?

Thanks for any advice

a.h

#ifndef A_H_
#define A_H_

struct Thing{
	static int a[30000];
};

#endif /* A_H_ */

a.cpp/a.cu

#include <a.h>
int Thing::a[30000] = {1, 0, -1, 0, -1, 0, ......};

Hi, Thanks for your code. It works. But I am not quite clear about this usage of “::” operator.

After this kind of definition, I can directly use Thing::a to access the data array without using Thing as a structure type to define a new variable. Can you explain it a little bit? I could not find this kind of usage in my textbook. Thanks

Look up “static member variables” in C++. You will probably find them described for C++ classes, but structs in C++ are basically classes, but members default to public instead of private access.