Construire un object partagé Python avec cmake, qui dépend de bibliothèques externes

Nous avons un fichier AC appelé dbookpy.c, qui fournira un Python liant certaines fonctions C.

Ensuite, nous avons décidé de construire un fichier .so avec cmake, mais il semble que nous fassions quelque chose de mal en ce qui concerne la liaison de la bibliothèque externe ‘libdbook’ dans la liaison:

Le CMakeLists.txt est comme suit:

PROJECT(dbookpy) FIND_PACKAGE(PythonInterp) FIND_PACKAGE(PythonLibs) INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH}) INCLUDE_DIRECTORIES("/usr/local/include") LINK_DIRECTORIES(/usr/local/lib) OPTION(BUILD_SHARED_LIBS "turn OFF for .a libs" ON) ADD_LIBRARY(dbookpy dbookpy) SET_TARGET_PROPERTIES(dbookpy PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES dbook) SET_TARGET_PROPERTIES(dbookpy PROPERTIES LINKER_LANGUAGE C) #SET_TARGET_PROPERTIES(dbookpy PROPERTIES LINK_INTERFACE_LIBRARIES dbook) #SET_TARGET_PROPERTIES(dbookpy PROPERTIES ENABLE_EXPORTS ON) #TARGET_LINK_LIBRARIES(dbookpy LINK_INTERFACE_LIBRARIES dbook) SET_TARGET_PROPERTIES(dbookpy PROPERTIES SOVERSION 0.1 VERSION 0.1 ) 

Ensuite, nous construisons:

 x31% mkdir build x31% cd build x31% cmake .. -- Check for working C comstackr: /usr/bin/gcc -- Check for working C comstackr: /usr/bin/gcc -- works -- Check size of void* -- Check size of void* - done -- Check for working CXX comstackr: /usr/bin/c++ -- Check for working CXX comstackr: /usr/bin/c++ -- works -- Configuring done -- Generating done -- Build files have been written to: /home/edd/dbook2/dbookpy/build x31% make Scanning dependencies of target dbookpy [100%] Building C object CMakeFiles/dbookpy.dir/dbookpy.o Linking C shared library libdbookpy.so [100%] Built target dbookpy 

Jusqu’ici tout va bien. Test en Python:

 x31% python Python 2.5.4 (r254:67916, Apr 24 2009, 15:28:40) [GCC 3.3.5 (propolice)] on openbsd4 Type "help", "copyright", "credits" or "license" for more information. >>> import libdbookpy python:./libdbookpy.so: undefined symbol 'dbook_isbn_13_to_10' python:./libdbookpy.so: undefined symbol 'dbook_isbn_10_to_13' python:./libdbookpy.so: undefined symbol 'dbook_sanitize' python:./libdbookpy.so: undefined symbol 'dbook_check_isbn' python:./libdbookpy.so: undefined symbol 'dbook_get_isbn_details' Traceback (most recent call last): File "", line 1, in  ImportError: Cannot load specified object 

Hmmm. Erreur de l’éditeur de liens On dirait qu’il ne lie pas libdbook:

 x31% ldd libdbookpy.so libdbookpy.so: Start End Type Open Ref GrpRef Name 05ae8000 25aec000 dlib 1 0 0 /home/edd/dbook2/dbookpy/build/libdbookpy.so.0.1 

Non ce n’est pas. Un lien approprié à libdbook ressemble à ceci:

 x31% ldd /usr/local/bin/dbook-test /usr/local/bin/dbook-test: Start End Type Open Ref GrpRef Name 1c000000 3c004000 exe 1 0 0 /usr/local/bin/dbook-test 08567000 28571000 rlib 0 2 0 /usr/lib/libm.so.5.0 09ef7000 29efb000 rlib 0 1 0 /usr/local/lib/libdbook.so.0.1 053a0000 253d8000 rlib 0 1 0 /usr/lib/libc.so.50.1 0c2bc000 0c2bc000 rtld 0 1 0 /usr/libexec/ld.so 

Quelqu’un at-il des idées pourquoi cela ne fonctionne pas?

Merci beaucoup.

Edd

Vous devez associer dbookpy à dbook:

 target_link_libraries(dbookpy dbook) 

Ajouter cela juste après la ligne ADD_LIBRARY(dbookpy dbookpy) devrait le faire.

Je vois que vous utilisez IMPORTED – l’aide de IMPORTED_LINK_INTERFACE_LIBRARIES lit comme suit:

  Lists libraries whose interface is included when an IMPORTED library target is linked to another target. The libraries will be included on the link line for the target. Unlike the LINK_INTERFACE_LIBRARIES property, this property applies to all imported target types, including STATIC libraries. This property is ignored for non-imported targets. 

Cela signifie donc que “dbook”, qui se trouve dans / usr / local / lib, devrait être une bibliothèque imscope:

  add_library(dbook SHARED IMPORTED) 

Est-ce vraiment ce que tu voulais? Je veux dire que les bibliothèques imscopes sont celles qui sont construites en dehors de CMake mais sont incluses dans votre arbre source. La bibliothèque dbook semble être installée ou du moins censée être installée. Je ne pense pas que vous ayez besoin d’importations ici – cela semble être un problème de liaison régulier. Mais cela peut simplement être un effet secondaire de la création d’un exemple minimal pour poster ici.

Au son de cela, pour obtenir les bibliothèques liées et les répertoires de liens sortingés, j’utiliserais probablement find_library() , qui examinera les emplacements par défaut sensibles comme / usr / local / lib, puis appenda cela aux bibliothèques de liens. .

 find_library(DBOOK_LIBRARY dbook REQUIRED) target_link_libraries(dbookpy ${DBOOK_LIBRARY}) 

En tout cas, on dirait que vous l’avez sortingé maintenant.

Merci de votre aide.

Vous avez raison de dire que IMPORTED n’est probablement pas nécessaire. L’ajout de LINK_LIBRARIES (dbookpy dbook) ajoute en effet -ldbook à l’exécution de gcc, donc c’est super.

Cependant, cmake semble ignorer LINK_DIRECTORIES, et ne trouve donc jamais -ldbook:

 /usr/bin/gcc -fPIC -shared -o libdbookpy.so.0.1 "CMakeFiles/dbookpy.dir/dbookpy.o" -ldbook /usr/bin/ld: cannot find -ldbook 

Voici le CMakeList tel quel:

 PROJECT(dbookpy) SET(CMAKE_VERBOSE_MAKEFILE ON) OPTION(BUILD_SHARED_LIBS "turn OFF for .a libs" ON) ADD_LIBRARY(dbookpy dbookpy) SET_TARGET_PROPERTIES(dbookpy PROPERTIES LINKER_LANGUAGE C) FIND_PACKAGE(PythonInterp) FIND_PACKAGE(PythonLibs) INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH}) INCLUDE_DIRECTORIES(/usr/local/include) target_link_libraries(dbookpy dbook) LINK_DIRECTORIES("/usr/local/lib") SET_TARGET_PROPERTIES(dbookpy PROPERTIES SOVERSION 0.1 VERSION 0.1 ) INSTALL(TARGETS dbookpy LIBRARY DESTINATION lib ) 

Des idées?