Search code examples
cmakepybind11proj

How to statically link PROJ to pybind11


I'm interested in statically linking PROJ to a library created with pybind11.
but, I get error by cmake.

Why am I getting this error and how can I fix it?

My CMakeLists.txt is this.

cmake_minimum_required(VERSION 3.4)
project(myearth LANGUAGES CXX)

add_subdirectory(pybind11)
pybind11_add_module(myearth MyEarth.cpp)

include_directories(${CMAKE_SOURCE_DIR}/PROJ/include)
add_library(proj4 STATIC IMPORTED)
set_target_properties(proj4 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/PROJ/lib/libproj.a)

target_link_libraries(myearth PRIVATE proj4)

CMake Error.
I think fPIC option is for shared library. so, libproj.a is not necessary this option because this is static library.

(venv) ~/Projects/LinkLibTest/MySample/build$ make
[ 50%] Building CXX object CMakeFiles/myearth.dir/MyEarth.cpp.o
[100%] Linking CXX shared module myearth.cpython-36m-x86_64-linux-gnu.so
/usr/bin/ld: ../PROJ/lib/libproj.a(proj_4D_api.o): relocation R_X86_64_PC32 against symbol `pj_errno' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/myearth.dir/build.make:98: myearth.cpython-36m-x86_64-linux-gnu.so] Error 1
make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/myearth.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
(venv) ~/Projects/LinkLibTest/MySample/build$ 

I created libproj.a by the following command.

$ ./configure --prefix=/output --disable-shared
$ sudo make
$ sudo make install

enter image description here

Thank you.


Solution

  • Shared libraries must have position independent code, but your version of PROJ was built without it; you will have to rebuild PROJ with PIC.

    $ ./configure --prefix=/output --with-pic