8

In integrating a library into my cmake project, I've been copy-pasting "dependency_libs" from the library's .la file into a variable and then using "target_link_library".

I need to get this "dependency_libs" variable directly from the .la file somehow. Is there a way to do this within cmake? If not, I can always write a script in some other language to parse the file with a regex, then write it to a file and import it to the variable.

user1122069
  • 1,767
  • 1
  • 24
  • 52

1 Answers1

0

To answer your question:

# Assume libtool_file is set somewhere to your .la file.

file(READ "${libtool_file}" contents)
if (contents MATCHES "dependency_libs *= *'([^']*)'")
  string(STRIP "${CMAKE_MATCH_1}" deps)
  string(REGEX REPLACE " +" ";" deps "${deps}")
  target_link_libraries(my_target PRIVATE ${deps})
else ()
  message(WARNING "${libtool_file} does not declare dependencies")
endif ()

But I would really, REALLY encourage you to use something more standard like PkgConfig, for which CMake has native support. Or better yet, the library's own find_package-compatible config package and its imported targets if they are available.

For what it's worth, I could only find one, single .la file on my system that included a dependency. That file was /usr/lib/x86_64-linux-gnu/libltdl.la and it contained a single dependency on -ldl (which can be better accessed through ${CMAKE_DL_LIBS}, anyway).

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86