Create a library by linking link external library

I am using yocto build environment for my project. There are multiple applications in the project and some of them are depending on one third party library(It contains the *.s0 files, header files). So I am planning to create one static wrapper library around the third party library and link the wrapper library for all the applications.

The structure of the project:

.
├── App1
├── App2
├── App3
└── third-party
     └── inc
     └── src
     └── lib
          └── libdvm-hash.so 
          └── libhash-ipc.so
     └── CMakeLists.txt

CMakeLists.txt

project(hash LANGUAGES CXX VERSION "1.0.0")
set(LIBRARY_NAME hash)

set(HASH_LIBRARY_FILES ${CMAKE_CURRENT_SOURCE_DIR}/lib/libdvm-hash.so ${CMAKE_CURRENT_SOURCE_DIR}/lib/libhash-ipc.so 
add_library(${LIBRARY_NAME} STATIC test.cpp)

target_include_directories(${LIBRARY_NAME}
	PUBLIC
		$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/inc/>
		$<INSTALL_INTERFACE:include>
)
target_link_libraries(${LIBRARY_NAME} PUBLIC ${HASH_LIBRARY_FILES} ssl)

set_target_properties(${LIBRARY_NAME} PROPERTIES
						OUTPUT_NAME ${LIBRARY_NAME})

install( FILES
		${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
		${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake
		DESTINATION
		${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
install(
	TARGETS ${LIBRARY_NAME}
	EXPORT ${PROJECT_NAME}-targets
	ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
	LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
	RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

install(EXPORT ${PROJECT_NAME}-targets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} NAMESPACE dvm::)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/inc/	DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT dev)

Now the problem is in the exported hash-targets.cmake the path to the library is hardcoded.

hash-targets.cmake

set_target_properties(hash PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
  INTERFACE_LINK_LIBRARIES "/home/mypc/path/to/the/ibdvm-hash.so;/home/mypc/pathto/the/libhash-ipc.so;ssl"
)

Is there any way to fix the hardcode path .*so and use the installed *.so from /usr/lib?

The problem is here. CMake doesn’t know that these can be relocated. The best way, IMO, is to make an IMPORTED target that represents these. When you install, your target will reference these imported targets. You’ll need to remake them in your hash-config.cmake file using the install-time locations (which you can implement by doing a find_library).

1 Like