How to import (link) external .so library

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):confused: .

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)

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.