0

Here is my source code structure:

cd my_git_repo/
    CMakeLists.txt
    src/
        main.cpp
        mylibrary/
            a.hpp
            b.hpp
            a.cpp
            b.cpp
            CMakeLists.txt

Root CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
project(myexe CXX)
add_subdirectory(src/mylibrary)

find_library(mylib NAMES mylibrary.so PATHS "./src/mylibrary/mylibrary.so")
add_executable(myexe src/main.cpp)
target_link_libraries(myexe ${mylib})

mylibrary/CMakeLists.txt is very simple. It builds a shared library and installs them. Ideally, mylibrary target should be built and installed before myexe is built. But this doesn't happen. mylibrary is built followed by myexe. Installation happens later. Because of this, find_library fails. pkg_check_modules() works for other shared libraries but fails here because of the same reason.

I appreciate your help.

Edit: This question differs from the duplicate because the answers posted to that question seem to be statically linking the library target_link_libraries(game engine). I want to dynamically link the .so library.

Cinder Biscuits
  • 4,880
  • 31
  • 51
psy
  • 914
  • 3
  • 10
  • 31
  • Possible duplicate of [How to use libraries within my CMake project that need to be installed first?](https://stackoverflow.com/questions/31755870/how-to-use-libraries-within-my-cmake-project-that-need-to-be-installed-first) – Kevin Sep 11 '19 at 01:09

1 Answers1

0

The idea in CMake is to build modules and then link them together.

You haven't shared the CMakeLists.txt for my library, so we cannot tell what it is doing. However, assuming that it is something like:

ADD_LIBRARY(mylibrary
  file1.cpp
  file2.cpp
)

Since you specified that you want mylibrary to always be linked as shared, you need to tell CMake that as well by either setting BUILD_SHARED_LIBS TO ON or by specifying SHARED in add_library:

ADD_LIBRARY(mylibrary SHARED
  file1.cpp
  file2.cpp
)

This is your library module. We will keep it simple for now and not worry about packing the library archive and installation here.

Now, back to your main CMakeLists.txt and how to make myexe consume it. Since you have already add_subdirectory(src/mylibrary), CMake knows about mylibrary. So simply link it using the module name. There is no need to find_library as you have already defined the module.

add_executable(myexe src/main.cpp)
target_link_libraries(myexe mylibrary)

This should suffice.

Do note, however, this is a very basic example to explain to you how CMake is designed to work. If you aren't building the library, and it is already installed, you would call find_library. Modern CMake is a bit more sophisticated and uses generator expressions, so be sure to read up on that as you progress to more complex projects.

Cinder Biscuits
  • 4,880
  • 31
  • 51
  • Yes, `mylibrary` CMakeLists.txt is very simple. It does `add_library(mylibrary SHARED blah)`. I did try `target_link_libraries(myexe mylibrary)` and it worked. But, does it do dynamic linking? Looks like the library is statically linked which I am trying to avoid. – psy Sep 11 '19 at 03:05
  • 1
    Just set the cache variable [BUILD_SHARED_LIBS](https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html) to on. – havogt Sep 11 '19 at 07:51
  • Yes, as @havogt mentions, you can set `BUILD_SHARED_LIBS` to `ON`, or you can specify that the `mylibrary` module is always built and linked as a shared library by changing add_library to `ADD_LIBRARY(mylibrary SHARED file1.cpp file2.cpp )` – Cinder Biscuits Sep 11 '19 at 13:08
  • 1
    @psy You asked me to answer this but what's above is the correct answer. Let us know if it still doesn't work. – tamas.kenez Sep 12 '19 at 08:55