c++cgccopenmpoffloading

OpenMP offloading in a dynamic library fails to compile


I want to compile C code with OpenMP offloading and create a dynamic library libtest.so. When I use the following command:

gcc -fPIC -shared -fopenmp -foffload=nvptx-none="-fPIC" test.c -o libtest.so

I get this error:

/usr/bin/ld: /tmp/ccWnqb5o.target.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
collect2: error: ld returned 1 exit status

GCC version is 10.2.0. I'm not sure what I do wrong here since -fPIC is included in the command. I wonder is it even possible to do what I want?

My test.c source just checks if the offloading works:

#include <omp.h>
#include <stdio.h>

void test()
{
  #pragma omp target teams
  {
    if (omp_is_initial_device())
      printf("on host\n");
    else
      printf("on target device\n");
  }
}

Solution

  • I spoke to the GCC developers and it seems to be a bug. They seem to have it resolved for GCC 11, but the fix was not backported. See https://gcc.gnu.org/g:a8b522311beef5e02de15427e924752ea02def2a for more information.