0

In my project I got a local library I am shipping with the project.

For that reason I included in the cmake file

LINK_DIRECTORIES(bls/build/src)
INCLUDE_DIRECTORIES(bls/src)

LINK_DIRECTORIES(bls/build/contrib/relic/lib)
INCLUDE_DIRECTORIES(bls/build/contrib/relic/include)
INCLUDE_DIRECTORIES(bls/contrib/relic/include)

And then link it to the executables

add_executable(keygen_bls
        src/keygen_bls.cpp)
target_link_libraries(keygen_bls blstmp relic_s)

With this cmake runs fine. However when I run make then I get.

/usr/bin/ld: cannot find -lblstmp
/usr/bin/ld: cannot find -lrelic_s

Even though those libraries are at the places I specified above and not at /usr/bin/ld.

Project paths

enter image description here

raycons
  • 735
  • 12
  • 26
  • 1
    Re: [`link_directories`](https://cmake.org/cmake/help/latest/command/link_directories.html) "_This command is rarely necessary and should be avoided where there are other choices._" – Ted Lyngmo Jun 23 '20 at 10:31
  • I do have the library in a relative path though in the project and not in an absolute path where it will always be found. – raycons Jun 23 '20 at 10:46
  • Ok, but you should be able to find the full path from the relative path using [`find_library`](https://cmake.org/cmake/help/latest/command/find_library.html#command:find_library) and then you could add a debug `message` showing what you've found before the `add_executable` command. – Ted Lyngmo Jun 23 '20 at 10:51
  • D you have an example for me? – raycons Jun 23 '20 at 10:56
  • Perhaps `find_library(RELIC_S_PATH relic_s bls/build/contrib/relic/lib)` and `message(STATUS "RELIC_S_PATH " ${RELIC_S_PATH})` will show something useful. – Ted Lyngmo Jun 23 '20 at 11:04
  • RELIC_S_PATH RELIC_S_PATH-NOTFOUND – raycons Jun 23 '20 at 11:07
  • That should be useful info when debugging this. :-) – Ted Lyngmo Jun 23 '20 at 11:15
  • 1
    "Even though those libraries are at the places I specified above" - Above there is a specification of **several** places, and actual location of your libraries is not clear... Please, show **absolute path** to library files corresponded to `blstmp` and `relic_s` libraries. This absolute path should include also the exact filenames (`lib` prefix, if it is, and the library extension, `.a` or `.so`). Show also the **absolute path to the project** (a directory, where `CMakeLists.txt` is located): This directory is used for interpretation of the relative paths passed to `LINK_DIRECTORIES` command. – Tsyvarev Jun 23 '20 at 11:18
  • I've added more information in the head post. – raycons Jun 23 '20 at 11:23
  • I think I missed `PATHS` before the path in the `find_library` command above. Also, the actual path may need to be `"${CMAKE_PREFIX_PATH}/bls/build/contrib/relic/lib"` – Ted Lyngmo Jun 23 '20 at 11:30
  • CMAKE_PREFIX_PATH is empty for me, so I tried find_library(RELIC_S_PATH relic_s PATHS ${CMAKE_CURRENT_SOURCE_DIR}/bls/build/contrib/relic/lib) Where the source dir is not empty, also resulted in not found – raycons Jun 23 '20 at 11:37
  • The file `libbls.a` could denote the library `bls` but not the library `blstmp` which you actually use for linking. – Tsyvarev Jun 23 '20 at 12:30
  • libblstmp.a is in the src folder (lthat's why I included bls/build/src in include directories) – raycons Jun 23 '20 at 12:49
  • Have you seen [this](https://stackoverflow.com/questions/8774593/cmake-link-to-external-library) post with several ways to link an external library to your project. Note, the answer with the most up-votes does **not** use `link_directories()`. – Kevin Jun 23 '20 at 12:54
  • That brought me a bit further to: No rule to make target 'bls/build/src/libblstmp.a', needed by 'hotstuff-keygen_bls'. Stop. – raycons Jun 23 '20 at 13:09
  • add_library( relic_s SHARED IMPORTED ) set_target_properties( relic_s PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/bls/build/contrib/relic/lib/librelic_s.a ) – raycons Jun 23 '20 at 13:35
  • I edited the full view in the post of the libraries and their locations. – raycons Jun 23 '20 at 13:50
  • The error message `No rule to make target 'bls/build/src/libblstmp.a'` tells that path you have specified in the `IMPORTED_LOCATION` property does actually **not exist**. Try to find out the **absolute path** of the library file (e.g. by picking it from the file's property, or from the shell), and **copy-paste** it to your `CMakeLists.txt` as a value for IMPORTED_LOCATION property. Note also, that `.a` is an extension for the `STATIC` library, not the `SHARED` one as you create IMPORTED target. – Tsyvarev Jun 23 '20 at 16:39

1 Answers1

-1

Thanks to the helpful comments leading me into the right direction I was able to get this to run:

INCLUDE_DIRECTORIES(bls/src)
INCLUDE_DIRECTORIES(bls/build/contrib/relic/include)
INCLUDE_DIRECTORIES(bls/contrib/relic/include)

add_library( blstmp STATIC IMPORTED )
set_target_properties( blstmp PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/bls/build/src/libblstmp.a )

add_library( relic_s STATIC IMPORTED )
set_target_properties( relic_s PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/bls/build/contrib/relic/lib/librelic_s.a )

Besides that, I'd noticed that git had excluded the library .a files when I copied them in that's why this wasn't working at the beginning either...

raycons
  • 735
  • 12
  • 26