1

Background:

I'm compiling a shared C library in a separate project and need to link against it with CMake.

I've used a C based neural network implementation called darknet to train a custom object detector.

Actually darknet is a standalone C executable. But I need to integrate the detector into a larger C++ codebase. I already found a small wrapper. Now I would like to convert the wrapper code into a CMake project, compile darknet into a shared library and link against it.

  • I've compiled this to a shared library with the compiler flags -fPIC and -shared.
  • The output was called darknet, I renamed it to darknet.so and copied it to /usr/lib/
  • sudo ldconfig

Question:

This is my CMakeLists.txt. As you can see, I'm linking against the darknet library. At compile time, there is an error, the library isn't found.

CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0)
project(wrapper)

add_definitions(-std=c++14)

find_package(OpenCV 3.2.0 EXACT REQUIRED)

include_directories(
        ${OpenCV_INCLUDE_DIRS}
        example
        wrapper
        darknet_src
)

SET(SOURCES
        example/main.cpp

        wrapper/darknet.cpp
        wrapper/darknet.h
        wrapper/darknet_detector.c
        wrapper/darknet_detector.h)

add_executable(wrapper ${SOURCES})
target_link_libraries(wrapper darknet ${OpenCV_LIBS})
lhk
  • 27,458
  • 30
  • 122
  • 201
  • 1
    `darknet,so` or `libdarknet.so` ? – MSalters May 02 '17 at 08:39
  • 1
    darknet.so is a wrong name. Make it libdarknet.so. – n. m. could be an AI May 02 '17 at 08:57
  • The **actual question/problem** - *CMake doesn't find the library*, am I right? If so, your `CMakeLists.txt` is the most valuable information in the post, and should be in the question post itself, not linked. Also, make sure that you have searched for the description of the *actual problem*. E.g., you may find [this question](http://stackoverflow.com/questions/31438916/cmake-cannot-find-library-using-link-directories) usefull. – Tsyvarev May 02 '17 at 09:59
  • @MSalters, you're right. That was indeed the problem. I'm still having compile problems, but that should probably be a different question. – lhk May 02 '17 at 10:03
  • @Tsyvarev , when I asked the question, I didn't know the actual problem. I think I'll completely update the question, to "CMake doesn't find library" and remove the other specifics – lhk May 02 '17 at 10:04
  • @Tsyvarev, I've followed your advice and made the question more precise. It is very well possible that this is now a duplicate. I'll search SO to check. – lhk May 02 '17 at 10:11
  • On Stack Overflow we tend to **not have** "Solution" part in the question post. Instead, you may provide your own answer with the same content. – Tsyvarev May 02 '17 at 10:16
  • @Tsyvarev fixed – lhk May 02 '17 at 10:19

0 Answers0