Skip to content
Advertisement

cmake linking shared libraries on ubuntu

Hi I am having problems with linking two libraries on ubuntu using cmake 2.8.12

Directory structure

libraries
  lib1
    CMakeLists.txt
    source1.cpp
  lib2
    CMakeLists.txt
    source2.cpp
build

CMakeLists.txt for lib1

cmake_minimum_required(VERSION 2.8.4)
project(lib1)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ../../build/)
set(BASE_DIR ../)
set(SOURCE_FILES source1.cpp)

include_directories ("${BASE_DIR}")
add_library(lib1 SHARED ${SOURCE_FILES})

target_include_directories (lib1 PUBLIC ${BASE_DIR})

This builds fine.

Then for lib2 I have the following CMakeLists.txt

cmake_minimum_required(VERSION 2.8.4)
project(lib2)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ../../build)
set(BASE_DIR ../)
set(SOURCE_FILES
    source2.cpp)

include_directories ("${BASE_DIR}")

add_library(lib2 SHARED ${SOURCE_FILES})

# include lib1
list(APPEND CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
find_library(lib1 lib1)
target_link_libraries(lib2 LINK_PUBLIC lib1)

Here I get

Linking CXX shared library ../build/liblib2.so

/usr/bin/ld: cannot find -llib1

I am not clear how to correctly link these two shared libraries using CMake. Anyone an idea what is going wrong.

Cheers, Mike

Advertisement

Answer

1) If you use

 target_include_directories (lib1 PUBLIC ${BASE_DIR})

why do you think you need

 include_directories ("${BASE_DIR}")

?

2) Try this:

find_library(lib1_location lib1)
message("Lib1 is at: ${lib1_location}")
target_link_libraries(lib2 LINK_PUBLIC ${lib1_location})

and see what happens.

3) See http://www.cmake.org/cmake/help/v3.0/manual/cmake-packages.7.html

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement