Я пытаюсь создать общую библиотеку с cmake на linux. Часть кода, отвечающая за его настройку:
if (BUILD_SHARED)
set(BUILD_SHARED_LIBS ON)
set(CMAKE_VERBOSE_MAKEFILE ON)
# shared library
add_library(semsim SHARED "${SEMSIM_HEADERS}" "${SEMSIM_SOURCES}")
#Note: not sure if this command does anything. It was supposed to be to link static libraries into my shared one
# target_link_options(semsim PUBLIC -Wl,--whole-archive)
#
message(STATUS "LIBBZ_INCLUDE_DIR ${LIBBZ_INCLUDE_DIR}")
target_include_directories(semsim PUBLIC # make public so include dirs propagate into test binaries
${CMAKE_SOURCE_DIR}
${LIBXML2_INCLUDE_DIR}
${LIBSBML_DEPS_INCLUDE_DIR}
${LIBSBML_INCLUDE_DIR}
${ZLIB_INCLUDE_DIR}
${ZIPPER_INCLUDE_DIR}
${LIBCOMBINE_INCLUDE_DIR}
${BZ2_INCLUDE_DIR}
${REDLAND_INCLUDE_DIR}
${RASQAL_INCLUDE_DIR}
${RAPTOR2_INCLUDE_DIR}
)
target_link_libraries(semsim PUBLIC # make public so libraries propagate into test binaries
${LIBCOMBINE_STATIC_LIBRARY}
${ZIPPER_STATIC_LIBRARY}
${ZLIB_STATIC_LIBRARY}
${LIBSBML_STATIC_LIB}
${LIBBZ_STATIC_LIBRARY}
${REDLAND_LIBRARY}
${RASQAL_LIBRARY}
${RAPTOR2_LIBRARY}
${LIBXML2_STATIC_LIBRARY}
dl
${BZ2_STATIC_LIBRARY}
curl xslt
)
endif ()
Это дает следующую команду для выполнения:
/usr/bin/c++ -fPIC -std=c++14 -g -shared -Wl,-soname,libsemsim.so -o libsemsim.so CMakeFiles/semsim.dir/CurlGet.cpp.o CMakeFiles/semsim.dir/RDFNode.cpp.o CMakeFiles/semsim.dir/Subject.cpp.o CMakeFiles/semsim.dir/Predicate.cpp.o CMakeFiles/semsim.dir/Resource.cpp.o CMakeFiles/semsim.dir/Triple.cpp.o CMakeFiles/semsim.dir/SemsimUtils.cpp.o CMakeFiles/semsim.dir/MetaID.cpp.o CMakeFiles/semsim.dir/XmlAssistant.cpp.o CMakeFiles/semsim.dir/Reader.cpp.o CMakeFiles/semsim.dir/Editor.cpp.o CMakeFiles/semsim.dir/Writer.cpp.o CMakeFiles/semsim.dir/RDF.cpp.o CMakeFiles/semsim.dir/Participant.cpp.o CMakeFiles/semsim.dir/PhysicalEntity.cpp.o CMakeFiles/semsim.dir/PhysicalPhenomenon.cpp.o CMakeFiles/semsim.dir/PhysicalProcess.cpp.o CMakeFiles/semsim.dir/PhysicalPropertyResource.cpp.o CMakeFiles/semsim.dir/PhysicalForce.cpp.o CMakeFiles/semsim.dir/Query.cpp.o CMakeFiles/semsim.dir/SemsimCombineArchive.cpp.o CMakeFiles/semsim.dir/Triples.cpp.o ../../third_party/libCombine-0.2.3/INSTALL/lib/libCombine-static.a ../../third_party/zipper/INSTALL/lib/libZipper-static.a ../../third_party/zlib-1.2.11/INSTALL/lib/libz.a /usr/local/lib/libxml2.a -ldl ../../third_party/libsbml-dependencies/INSTALL/lib/libbz2.a -lcurl -lxslt
Если я позволю CMake попытаться построить с этой командой, я получу следующая ошибка:
/usr/bin/ld: /usr/local/lib/libxml2.a(uri.o): relocation R_X86_64_PC32 against symbol `xmlFree' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
Однако я заметил, что в команде, созданной CMake, отсутствуют флаги -L
. Поэтому я изменил команду на:
/usr/bin/c++ -fPIC -std=c++14 -g -shared -o libsemsim.so CMakeFiles/semsim.dir/CurlGet.cpp.o CMakeFiles/semsim.dir/RDFNode.cpp.o CMakeFiles/semsim.dir/Subject.cpp.o CMakeFiles/semsim.dir/Predicate.cpp.o CMakeFiles/semsim.dir/Resource.cpp.o CMakeFiles/semsim.dir/Triple.cpp.o CMakeFiles/semsim.dir/SemsimUtils.cpp.o CMakeFiles/semsim.dir/MetaID.cpp.o CMakeFiles/semsim.dir/XmlAssistant.cpp.o CMakeFiles/semsim.dir/Reader.cpp.o CMakeFiles/semsim.dir/Editor.cpp.o CMakeFiles/semsim.dir/Writer.cpp.o CMakeFiles/semsim.dir/RDF.cpp.o CMakeFiles/semsim.dir/Participant.cpp.o CMakeFiles/semsim.dir/PhysicalEntity.cpp.o CMakeFiles/semsim.dir/PhysicalPhenomenon.cpp.o CMakeFiles/semsim.dir/PhysicalProcess.cpp.o CMakeFiles/semsim.dir/PhysicalPropertyResource.cpp.o CMakeFiles/semsim.dir/PhysicalForce.cpp.o CMakeFiles/semsim.dir/Query.cpp.o CMakeFiles/semsim.dir/SemsimCombineArchive.cpp.o CMakeFiles/semsim.dir/Triples.cpp.o -L ../../third_party/libCombine-0.2.3/INSTALL/lib/libCombine-static.a -L ../../third_party/
zipper/INSTALL/lib/libZipper-static.a -L ../../third_party/zlib-1.2.11/INSTALL/lib/libz.a -L /usr/local/lib/libxml2.a -l
dl -L ../../third_party/libsbml-dependencies/INSTALL/lib/libbz2.a -lcurl -lxslt
запустил ее в оболочке, и она сработала. Может ли кто-нибудь предложить, почему CMake не использует флаг -L
в соответствующих местах?