problem while compiling .cu to .o file

I have problem while making .so file
I have head file,, and declare a variable AE as: extern device managed int AE in head file
in there is also a declaration of AE device managed int AE, so both and recognize AE
First I did as following
nvcc --gpu-architecture=sm_50 --device-c to get 1.o (either to 2.o)
nvcc --gpu-architecture=sm_50 -o a.out 1.o 2.o to execute my computing by a.out. It works and there is no error
Now I am trying to compile these .obj files as .so file. I find one way in the internet to get .o files as:
nvcc --gpu-architecture=sm_50 --gpu-code=sm_50 -DGPU -I/usr/local/cuda-10.1/include/ -DCUDNN
–compiler-options “-fPIC” -c -o 1.o
and then I get a error
error:redefinition of ‘int* AE’
device managed int AE
It seems that there is conflict between head file and cu file. Does any one have idea to solve this problem? I will be grateful if you could give me advise or how could I pack cuda as .so file.Thank you

Amongst other things, this requires a compilation flow that includes proper use of relocatable device code compilation/linking. Your use of –device-c enabled that. Your subsequent use of -c did not:

$ cat
#include "head2.cuh"
#include "head1.h"
#include <stdio.h>
__device__ __managed__ int AE;

void test1(){

  printf("AE = %d\n", AE);
$ cat
#include "head2.cuh"

void test2(){

  AE = 37;
$ cat head2.cuh
extern __device__ __managed__ int AE;
void test2();
$ cat head1.h
void test1();
$ cat main.cpp
#include "head1.h"

int main(){

$ nvcc -Xcompiler -fPIC -shared -rdc=true -o
$ g++ -o test main.cpp
$ ./test
AE = 37

Dear Crovella,
I deeply appreciate your program. I will try it later.
After your explanation and NVIDIA manual I understand what the “linker” means