Abdullah Alattar
Abdullah Alattar

Reputation: 123

How to link with all libraries inside a directory?

I'm using meson-build for a c++ project.

I created a directory called libs and put all the libraries I need inside it , how do I link with it?

Upvotes: 0

Views: 2832

Answers (3)

Cyrille Cormier
Cyrille Cormier

Reputation: 225

I am reading all the file entries into a variable, then I parse them to get short lib name thanks to regexps. Then I loop on them to link them to my client lib (in my case imported libs...):

set(LIBS_DIR ${CMAKE_CURRENT_LIST_DIR}/../SMP/libs)
file(GLOB LIBS_FULL_NAME ${LIBS_DIR}/*.so)
message(STATUS "LIBS ${LIBS_FULL_NAME}")


FOREACH(LIB_FULL_NAME ${LIBS_FULL_NAME})
    message(STATUS "${LIB_FULL_NAME}")
    string(REGEX REPLACE "^.+/lib(.+).so$" "\\1" LIB_NAME ${LIB_FULL_NAME})
    message(STATUS "LIB_NAME ${LIB_NAME}")

    add_library( ${LIB_NAME}
            SHARED
            IMPORTED )

    set_target_properties( # Specifies the target library.
            ${LIB_NAME}

            # Specifies the parameter you want to define.
            PROPERTIES IMPORTED_LOCATION

            # Provides the path to the library you want to import.
            ${LIB_FULL_NAME} )

    target_link_libraries(clientlib ${LIB_NAME})
ENDFOREACH()

Upvotes: 0

Abdullah Alattar
Abdullah Alattar

Reputation: 123

Okay this is what I was looking for,

    cmplr = meson.get_compiler('cpp')
    mylib1 = cmplr.find_library('lib_name1', dir : 'path_to_directory')
    mylib2 = cmplr.find_library('lib_name2', dir : 'path_to_directory')
    ....
    executable(.... , dependencies : [mylib1, mylib2])

And thanks for the Tips.

Upvotes: 2

Daniel Trugman
Daniel Trugman

Reputation: 8491

After reading meson's dependencies manual, I don't think it has such an option. You should specify a dependency for every library you want to link.

And here is a snippet from the manual on how you should do that with your own libraries:

my_inc = include_directories(...)
my_lib = static_library(...)
my_dep = declare_dependency(link_with : my_lib, include_directories : my_inc)

BUT This is for the best, since you SHOULD control the linked libraries very carefully, why?

  1. Linking can be successful even if not all symbols are resolved, and the app will crash only at runtime.
  2. You want to control linkage order, in case you have more than one library with the same symbols
  3. You don't want to link unwanted libraries, because they might insert malicious code into your application (e.g. Your co-worker can plant a malicious library in that folder, and you will never know, but have a backdoor in your app)

Upvotes: 2

Related Questions