# How to import (link) external .so library

**URL:** <https://discourse.cmake.org/t/how-to-import-link-external-so-library/6858>\
**Category:** Code\
**Created:** [November 13, 2022, 12:07am UTC](https://discourse.cmake.org/t/how-to-import-link-external-so-library/6858 "2022-11-13T00:07:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathew](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@Mathew](https://discourse.cmake.org/u/Mathew)\
**Post date:** [November 13, 2022, 12:07am UTC](https://discourse.cmake.org/t/how-to-import-link-external-so-library/6858/1 "2022-11-13T00:07:39Z")

</div>

Hi there,

I need help to link an external library into my main.cpp. I downloaded and compiled a TPM\_ibmtss1.6.0 project from the internet and got the libraries ibmtss.so, ibmtssutils.so and ibmtssutils12.so. Now, I have no idea how to include them into my main.cpp project with CMake. I have watched some tutorials and tried examples but I am getting linker errors  
(#include \<ibmtss/tss.h\> → undefined reference)😕 .

Could someone help me please ?

Yours sincerely  
Mathew

—-\> This is what I have tried :  
cmake\_minimum\_required(VERSION 3.9…3.19)  
if(${CMAKE\_VERSION} VERSION\_LESS 3.12)  
cmake\_policy(VERSION ${CMAKE\_VERSION})  
endif()

project(project  
VERSION 0.0.1  
DESCRIPTION “Test”  
LANGUAGES CXX  
)

set(CMAKE\_CXX\_FLAGS “${CMAKE\_C\_FLAGS} -DTPM\_POSIX”)  
set(CMAKE\_CXX\_STANDARD 20)  
set(CMAKE\_CXX\_STANDARD\_REQUIRED ON)  
set(CMAKE\_CXX\_EXTENSIONS OFF)

add\_library(tpmibmtss SHARED IMPORTED)

set\_target\_properties(tpmibmtss PROPERTIES IMPORTED\_LOCATION ${CMAKE\_CURRENT\_BINARY\_DIR}/lib/TPM\_ibmtss1.6.0/lib/libibmtss.so)

set\_target\_properties(tpmibmtss PROPERTIES IMPORTED\_LOCATION ${CMAKE\_CURRENT\_BINARY\_DIR}/lib/TPM\_ibmtss1.6.0/lib/libibmtssutils.so)

set\_target\_properties(tpmibmtss PROPERTIES IMPORTED\_LOCATION ${CMAKE\_CURRENT\_BINARY\_DIR}/lib/TPM\_ibmtss1.6.0/lib/libibmtssutils12.so)  
target\_link\_libraries(${PROJECT\_NAME}  
PUBLIC  
tpmibmtss

)

add\_executable(test main.cpp tpmibmtss)

---

<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:** [November 14, 2022, 12:16am UTC](https://discourse.cmake.org/t/how-to-import-link-external-so-library/6858/2 "2022-11-14T00:16:11Z")

</div>

I don’t even know how the project you’ve posted here configures. You’re telling CMake that some target, named `${PROJECT_NAME}` to link to `tpmibmtss` which should error because…you have no target named `project`. I suspect that this example here is some trimmed down code instead of what you’re actually having issues with. Please post a minimal example that actually exhibits the problem. Additionally, you have added `tpmibmtss` as a source to the `test` executable which should error as (I presume) there is no such file in the source tree.

I think what you want is to remove `tpmibmtss` from the source list of `test` and instead `target_link_libraries` to it instead. You also need to set `INTERFACE_INCLUDE_DIRECTORIES` properties to these TPM targets for the include paths so that includes work.
