0

I have a project called myproject, with a build directory myhome/myproject/build.

I have a library called libmylibrary.so that lives in myhome/folder/lib with headers in myhome/folder/include

Then I have a CMakeLists.txt in myhome/myproject which looks like:

cmake_minimum_required(VERSION 3.5.1)                                                                                                                                                                       
project (myproject)
file(GLOB SOURCES "src/*.cpp")
add_executable(myproject ${SOURCES})

find_library(MYLIB mylibrary REQUIRED)
target_link_libraries(myproject PRIVATE mylibrary)

I run cmake ../ in the build directory, and then make, and I get:

/usr/bin/ld: cannot find -lmylibrary
collect2: error: ld returned 1 exit status

I simply do not understand how this is so difficult. What am I missing? I have tried what feels like every combination of random but extremely similarly named CMake directives, with no success (and no information about the reasons for success or failure, so it's literally just trial and error).

How can I tell [whatever thing needs to know] where my library is, so that CMake can find it, without it being hard-coded in the CMake script? Thanks.

Thanks

ricky116
  • 744
  • 8
  • 21
  • Side note: `REQUIRED` is a keyword for `find_package` call; it has *no sence* for [find_library](https://cmake.org/cmake/help/v3.9/command/find_library.html) and other `find_*` commands. Also, your `find_library` call sets `MYLIB` **variable** to the location of the library. So, for link with the library you need to use `${MYLIB}`, not a plain `mylibrary`. – Tsyvarev Feb 09 '19 at 22:09

1 Answers1

0

For hint CMake about library location, you may set CMAKE_LIBRARY_PATH variable to the directory, which contains the needed library:

cmake -DCMAKE_LIBRARY_PATH=myhome/folder/lib <other-cmake-arguments>

Alternatively, if your library is contained under lib/ subdirectory, you may hint about upper directory with CMAKE_PREFIX_PATH variable:

cmake -DCMAKE_PREFIX_PATH=myhome/folder/lib <other-cmake-arguments>

The latter way hints other find_* commands too, see that my answer for more info.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153