I was attempting to cross-compile gRPC (https://github.com/grpc/grpc) from Windows to Linux as a third-party dependency using CMake and the Clang compiler; however, I encountered what I believe may be incorrect behavior in CMake:
When attempting this cross-compilation, my project (executable) was unable to link to the static libraries in gRPC. The reported error was something to the following effect: “relocation R_X86_64_32S against `***’ can not be used when making a shared object; recompile with -fPIC”.
I initially suspected the CMAKE_POSITION_INDEPENDENT_CODE variable was not being set correctly; however, I confirmed the CMAKE_POSITION_INDEPENDENT_CODE had the correct value of ‘ON’.
Upon further investigation, I noticed that when using the Clang compiler, the CMAKE_C(XX)_COMPILE_OPTIONS_PIC variables do not get set to ‘-fPIC’ in cmake/Modules/Compiler/clang.cmake; however, this same variable is set to ‘-fPIC’ in cmake/Modules/Compiler/GNU.cmake.
This difference in behavior explains why I saw the linker error reported above. It also explains why this cross-compilation fails when using Clang, but is successful when using the GNU compiler.
Is this difference in behavior for the CMAKE_C(XX)_COMPILE_OPTIONS_PIC variables when using the GNU and Clang compilers expected? In case it helps, this issue is similar to the one addressed by the following merge request: https://gitlab.kitware.com/cmake/cmake/-/merge_requests/1692/diffs.