Is there any way to use a libtool ".la" file with cmake "target_link_libraries"
Asked Answered
L

1

9

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.

Lasko answered 30/9, 2018 at 1:32 Comment(0)
E
1

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).

Equitable answered 18/8, 2021 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.