0

I have a set of custom libraries I'm trying to build using CMake files I wrote. The current directory structure looks like this:

├── lib
├── src
│   ├── ac_d
│   ├── at_d
│   ├── bp_d
│   ├── fm_d
│   ├── hm_d
│   ├── pi_d
│   ├── pv_d
│   ├── ra_d
│   └── rc_d
└── test

My goal is to have the compiler use the .so files built in the ./lib directory when linking the executables in the ./src directory. I've tried the following, to no avail:

In top level CMakeLists.txt:

/* minimum version, project name etc */

set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/lib)

/* other stuff */
set(CDH_LIB_NAMES comm daemon debug tty sock)

set(CDH_LIB_INCLUDES
        ${CMAKE_CURRENT_SOURCE_DIR}/lib/global.h
        ${CMAKE_CURRENT_SOURCE_DIR}/lib/libcomm.h
        ${CMAKE_CURRENT_SOURCE_DIR}/lib/libdaemon.h
        ${CMAKE_CURRENT_SOURCE_DIR}/lib/libdebug.h
        ${CMAKE_CURRENT_SOURCE_DIR}/lib/libtty.h
        ${CMAKE_CURRENT_SOURCE_DIR}/lib/libsock.h
        ${CMAKE_CURRENT_SOURCE_DIR}/lib/uthash.h
        ${CMAKE_CURRENT_SOURCE_DIR}/lib/utlist.h)

add_subdirectory(lib) # make sure we build .so first
include_directories(lib) # then use this as an include directory
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib) # and a link directory
find_library(CDH_LIBS
    NAMES tty comm sock debug daemon
    PATHS "${CMAKE_CURRENT_SOURCE_DIR}/lib"
) # Not sure if this does anything
add_subdirectory(src) # Now build the src binaries

I also heard that you can use target_link_libraries(), so I added lines similar to this in all `src/*/CMakeLists.txt:

set(ACD_BIN ACdtest)
set(ACD_SRC
        ${CMAKE_CURRENT_SOURCE_DIR}/ACd.c
        ${CMAKE_CURRENT_SOURCE_DIR}/ACd.h
        ${CDH_LIB_INCLUDES})
add_executable(${ACD_BIN} ${ACD_SRC})
target_link_libraries(${ACD_BIN} ${CDH_LIB_NAMES})

For some reason I still get undefined references. So I tried compiling one such binary with make VERBOSE=1 pi-d and got the following output (only relevant lines for brevity):

Linking C executable pi-d
cd /home/user/projectname/build/src/pi_d && /usr/bin/cmake -E cmake_link_script CMakeFiles/pi-d.dir/link.txt --verbose=1
/usr/bin/cc  --std=gnu99     CMakeFiles/pi-d.dir/PId.c.o  -o pi-d  -L/home/user/projectname/lib -rdynamic ../../lib/libcomm.so ../../lib/libdaemon.so ../../lib/libsock.so -Wl,-rpath,/home/user/projectname/build/lib:/home/user/projectname/lib 

Which looks like it's only getting libcomm, libdaemon, and libsock, neglecting libdebug and libtty. Why is this the case? What can I change?

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
ijustlovemath
  • 703
  • 10
  • 21

2 Answers2

2

debug is a keyword for target_link_libraries command.

Linking with libraries followed that keyword is performed only for "Debug" configuration, which probably is not your case.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • I'm attempting to implement the answer you gave over here: http://stackoverflow.com/questions/39662985/linking-against-a-library-called-debug-in-cmake and running into the issue of "cannot find -ldebug_lib", which seems like something is getting lost in translation. – ijustlovemath May 04 '17 at 22:44
0

I fixed this by simply renaming the target to debug_lib.

add_library(debug_lib SHARED ${DEBUG_LIB_SRC})

This causes the library made to be called libdebug_lib.so, but that's fine for my purposes.

ijustlovemath
  • 703
  • 10
  • 21