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?