# Create a library by linking link external library

**URL:** <https://discourse.cmake.org/t/create-a-library-by-linking-link-external-library/3844>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [July 28, 2021, 9:28am UTC](https://discourse.cmake.org/t/create-a-library-by-linking-link-external-library/3844 "2021-07-28T09:28:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![swaraj\_somala](https://discourse.cmake.org/user_avatar/discourse.cmake.org/swaraj_somala/32/1668_2.png) [@swaraj\_somala](https://discourse.cmake.org/u/swaraj_somala)\
**Post date:** [July 28, 2021, 9:28am UTC](https://discourse.cmake.org/t/create-a-library-by-linking-link-external-library/3844/1 "2021-07-28T09:28:25Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [July 28, 2021, 1:38pm UTC](https://discourse.cmake.org/t/create-a-library-by-linking-link-external-library/3844/2 "2021-07-28T13:38:43Z")

</div>

> [@swaraj\_somala](#):
>
> `set(HASH_LIBRARY_FILES ${CMAKE_CURRENT_SOURCE_DIR}/lib/libdvm-hash.so ${CMAKE_CURRENT_SOURCE_DIR}/lib/libhash-ipc.so`

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`).
