0

I'm building a kmeans executable from this cmake file:

# check the minimum version
cmake_minimum_required( VERSION 3.16 )

# the project name
project( kmeans )


#########################################################################
#### Define all the global variables to compile the application
#########################################################################
set( APPLICATION_NAME "kmeans")

#########################################################################
#### Adjust compiling option for this application
#########################################################################

# force the Release build if not already set
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif(NOT CMAKE_BUILD_TYPE)

# setting common c++ flags
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp -pthread -std=c++17 -static-libgcc -DKMEANS_DATASET_PATH=\"\\\"${CMAKE_CURRENT_SOURCE_DIR}/dataset/\\\"\"")

# setting debug flags
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g3 -O0")

# setting release with debug info flags
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -march=native -mtune=native -g3 -O2")

# setting release flags
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native -mtune=native -O3")


#########################################################################
#### Find external libaries
#########################################################################

# Boost program options
find_package(Boost REQUIRED program_options)

# include the directories
include_directories(${Boost_INCLUDES} )

################################
#### Sources
################################

set( KMEANS_SRC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src)
set( KMEANS_HDR_PATH ${CMAKE_CURRENT_SOURCE_DIR}/include)
set( KMEANS_HDR_FILES
  ${KMEANS_HDR_PATH}/kmeans.h
)
set( KMEANS_SRC_FILES
  ${KMEANS_SRC_PATH}/main.cc
  ${KMEANS_SRC_PATH}/cluster.c
  ${KMEANS_SRC_PATH}/kmeans_clustering.c
)

include_directories( ${KMEANS_HDR_PATH} )


################################
#### Compilation
################################


#----- Set binary name for the mini-app
add_executable(${APPLICATION_NAME} ${KMEANS_SRC_FILES} ${KMEANS_HDR_FILES})

target_link_libraries(${APPLICATION_NAME} Boost::program_options)

################################
#### Install
################################

install( TARGETS ${APPLICATION_NAME} DESTINATION ${PROJECT_SOURCE_DIR}/bin )

After building the kmeans executable gets generated correctly inside the build dir (build/kmeans) and works perfectly. After a make install tho the generated binary (bin/kmeans) gives the following error ./kmeans: error while loading shared libraries: libboost_program_options.so.1.75.0: cannot open shared object file: No such file or directory. I tried to take a look at the install command doc but couldn't find any option taking care of the case. I suppose I'm missing something inside the cmake file but cannot spot it.

Barnercart
  • 1,523
  • 1
  • 11
  • 23
  • Where does `libboost_program_options.so.1.75.0` live on your filesystem? Running `ldd` against the version of `kmeans` in your build dir should give you the path. – Stephen Newell Jan 22 '21 at 16:13
  • It's inside `/usr/local/lib`. – Barnercart Jan 22 '21 at 16:24
  • Try setting the rpath for the installed version of the exe: [`INSTALL_RPATH`](https://cmake.org/cmake/help/v3.0/prop_tgt/INSTALL_RPATH.html#prop_tgt:INSTALL_RPATH) – fabian Jan 22 '21 at 16:48
  • @fabian thanks, doing `set(CMAKE_INSTALL_RPATH "${Boost_LIBRARY_DIRS}")` before the `install` command solved it. Just out of curiosity, why isn't this a default operation inside install targets? – Barnercart Jan 22 '21 at 17:07
  • 1
    @Barnercart: **Build tree** is not intended to be copied into other places, so CMake automatically sets RPATH for created libraries/executables for things just work. But **install tree** could be copied into other places on the same machine, or could be copied to other machine. Because of that, CMake leaves RPATH for a developer, who is aware about the location of libraries on other machines. You may find mode details about that in wiki [RPATH Handling](https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling). – Tsyvarev Jan 22 '21 at 17:54

0 Answers0