S2OPC build: PIE and BUILD_SHARED_LIBS options are incompatible
Description
When both POSITION_INDEPENDENT_EXECUTABLE and BUILD_SHARED_LIBS S2OPC build options are set, the following failure occurs when building the S2OPC shared libraries:
/usr/local/bin/ld: /usr/lib/x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make[2]: *** [src/Common/CMakeFiles/s2opc_common.dir/build.make:1533: lib/libs2opc_common.so] Error 1
make[1]: *** [CMakeFiles/Makefile2:142: src/Common/CMakeFiles/s2opc_common.dir/all] Error 2
make: *** [Makefile:130: all] Error 2
Error: build failed
Analysis
This is due to the fact that the -pie
flag was added to link flags in S2OPC libraries link options:
if (POSITION_INDEPENDENT_EXECUTABLE)
# necessary to build binaries as PIE (see CMake CMP0083)
list(APPEND S2OPC_LINKER_FLAGS $<$<AND:${IS_GNU},$<NOT:${IS_MINGW}>>:-pie>)
And this flag is used to link the shared S2OPC common library which is not compatible.
One solution is to add the link flags only for compiling the executable using CMAKE_EXE_LINKER_FLAGS
.
Moreover since CMake 3.14 it is possible to activate CMP0083 and check compiler supports PIE which should be used in this case.