0

I've my package with CMakeLists.txt and I have a libNewLib.so compiled from another package from another library (NewLib). I've also the header files of that library that I include like so:

set(LIB_MANUALLY "${CMAKE_CURRENT_SOURCE_DIR}/lib/") #I put the file libNewLib.so here
include_directories(
  include/NewLib
)
LINK_DIRECTORIES(${LIB_MANUALLY})
target_link_libraries(estimation libNewLib.so)

But I still get the error:

/usr/bin/ld: cannot find -lNewLib

Is it the correct way to do it? I tried a couple of solutions but it didn't work.

I'm using Ros kinetic catkin package

make VERSION 2.8.3

Full CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.3)
project(localization)

add_compile_options(-std=c++11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Ofast -march=native")
set(LIB_MANUALLY "${CMAKE_CURRENT_SOURCE_DIR}/lib/")
message(STATUS "LIB_MANUALLY : ${LIB_MANUALLY}")


find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  image_transport
  roscpp
  rospy
  std_msgs
)

find_package(OpenCV REQUIRED
NO_MODULE # should be optional, tells CMake to use config mode
PATHS /usr/local # look here
NO_DEFAULT_PATH) # and don't look anywhere else
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES localization
  CATKIN_DEPENDS cv_bridge image_transport roscpp rospy std_msgs
#  DEPENDS system_lib
)

include_directories(
  include
  include/NewLib
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)
add_executable(estimation
  src/pose_map_estimation.cpp
  src/pose_map_estimation_main.cpp
)

target_link_libraries(estimation ${catkin_LIBRARIES} ${OpenCV_LIBRARIES} libNewLib.so)
LINK_DIRECTORIES(${LIB_MANUALLY})
Ja_cpp
  • 2,426
  • 7
  • 27
  • 49
  • If I remember correctly, you need to use a shortened form of the libaries filename, as in `target_link_libraries(estimation NewLib)`. Can you try that? Btw do you use `catkin_make` to build this? – moooeeeep Jan 17 '18 at 10:34
  • I tried it but it didn't work.. I'm using catkin_make to build my project. – Ja_cpp Jan 17 '18 at 10:39
  • Then please show the full `CMakeLists.txt`. You can remove the comments. – moooeeeep Jan 17 '18 at 10:43
  • yes okaaay done – Ja_cpp Jan 17 '18 at 10:52
  • Related: https://stackoverflow.com/questions/31438916/cmake-cannot-find-library-using-link-directories – ComicSansMS Jan 17 '18 at 11:50
  • Why do you have `link_directories()` at the end of the file? You should be calling `link_directories(`) quite literally, the line after you set `${LIB_MANUALLY}` – Chris Jan 17 '18 at 17:44

1 Answers1

0

In https://cmake.org/cmake/help/v3.0/command/link_directories.html it says:

Specify the paths in which the linker should search for libraries. The command will apply only to targets created after it is called.

So, try if it works if you create the target AFTER the link_directories command.

You could also consider to write a proper cmake file for locating your library and use find_package to find it.

wolff
  • 426
  • 4
  • 11
  • Not in the code snippet you showed us. Where do you call `add_executable` or `add_library`? – wolff Jan 17 '18 at 10:31
  • I don't have add_library and I've these in that order: include_directories, LINK_DIRECTORIES, add_executable then target_link_libraries at the end – Ja_cpp Jan 17 '18 at 10:38
  • 1
    Please update you question then and show us the complete cmake file. – wolff Jan 17 '18 at 10:47
  • 1
    Now link_directories is at the end of the file. This cannot be right. – wolff Jan 17 '18 at 10:49
  • I tried it before and after target_link_libraries but the same issue – Ja_cpp Jan 17 '18 at 10:53
  • 1
    `link_directories` shoud be **before** `add_executable`, which creates the target. This is what the answer means. – Tsyvarev Jan 17 '18 at 17:24