problem while compiling .cu to .o file

Hello,
I have problem while making .so file
I have head file, 1.cu, 2.cu and declare a variable AE as: extern device managed int AE in head file
in 1.cu there is also a declaration of AE device managed int AE, so both 1.cu and 2.cu recognize AE
First I did as following
nvcc --gpu-architecture=sm_50 --device-c 1.cu to get 1.o (either to 2.o)
then
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 1.cu -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 1.cu
#include "head2.cuh"
#include "head1.h"
#include <stdio.h>
__device__ __managed__ int AE;

void test1(){

  test2();
  printf("AE = %d\n", AE);
}
$ cat 2.cu
#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(){

  test1();
}
$ nvcc -Xcompiler -fPIC -shared -rdc=true 1.cu 2.cu -o test.so
$ g++ -o test test.so 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