How to link with libdl library in cmake

7k views Asked by At

My application depends on OpenSSL. So, I downloaded and built OpenSSL, and placed the static libraries (libssl.a and libcrypto.a) I require in known directory I have control over. In my application's cmake file, I use the find_library command to determine the full path of each library, store the paths in a list variable, and add that list variable to the target_link_libraries command. That works, insofar as the OpenSSL symbols are visible to my application.

OpenSSL further depends on the "Dynamic linking loader" library (a.k.a. libdl). I know that the OpenSSL build used the -ldl compiler option because I can see it in the build output.

On my Linux develpment machine, the libdl library seems to have already been installed. The evidence is:

  1. libdl.so and libdl.so.2 (which are symbolic links to the actual libdl-2-17.so library file) exist in what seems to be a standard directory for such things: /usr/lib64
  2. When I run man dlclose, I get the expected manual page about the "Dynamic linking loader" library.

However, when I build my application, I get linker errors like the following. The libdl library is definitely not being found.

/usr/bin/ld: ../install/lib/libcrypto.a(dso_dlfcn.o): undefined reference to symbol 'dlclose@@GLIBC_2.2.5'
/usr/lib64/libdl.so.2: error adding symbols: DSO missing from command line

Question 1

Should cmake have been able to find libdl.so on its own?

Question 2

In my application's cmake file, I can use the find_library command to determine the full path of the libdl library, and add that path to the target_link_libraries command. Notice that I did not have to give it a path hint. That works, but it feels like a workaround.

Should it be necessary for me to explicitly search for the full path of libdl.so?

Question 3

Is there a more proper way in cmake to link with a "standard" library such as libdl?

cmake file

The following are the relevant snippets of my application's cmake file.

...

#------------------------------
# Search for openssl libraries.
set( OPENSSL_LIBRARIES )
find_library( temp libssl.a PATHS "${CMAKE_SOURCE_DIR}/3rd_party/" )
list( APPEND OPENSSL_LIBRARIES ${temp} )
unset( temp CACHE )

find_library( temp libcrypto.a PATHS "${CMAKE_SOURCE_DIR}/3rd_party/" )
list( APPEND OPENSSL_LIBRARIES ${temp} )
unset( temp CACHE )

#------------------------------
# Search for dl library.
# TODO:  Is this workaround really necessary?
find_library( DL_LIBRARY libdl.so  )

...

#------------------------------
# My application.
add_executable( myapp main.cxx )

set_target_properties( myapp PROPERTIES COMPILE_FLAGS "-g" )

target_link_libraries( myapp
  ${OPENSSL_LIBRARIES}

  # TODO:  Is this workaround really necessary?
  ${DL_LIBRARY}
)

install( TARGETS myapp DESTINATION ${BIN_INSTALL_DIR} )

cmake version

I am using cmake version 2.8.12, but the particular version may not be important.

0

There are 0 answers