20

My project (in C) has a third party dependency at build time. But the third party library is, by default, installed to /opt/ instead of /lib, and I cannot find it in pkg-config. From mesonbuild's documentation, should I use declare_dependency, I don't have its source code to treat it as my sub-project. If I use dependency() to define it, I can't find the correct argument to define a customized location.

How to declare dependency for a non-standard third party library?

Chong
  • 933
  • 2
  • 10
  • 28

2 Answers2

22

As documented here and here

The main use case for this [declare_dependency()] is in subprojects.

and

[dependency()] finds an external dependency ... with pkg-config [or] library-specific fallback detection logic ...

You can, instead, use find_library() provided by the compiler object and include_directories() . find_library() returns an object just like the one declare_dependency() returns. include_directories() returns an opaque object which contains the directories.

Assuming you are using a C compiler and your 3rd party library and its header file are /opt/hello/libhello.so and /opt/hello/hello.h, you can do:

project('myproj', 'c')

cc = meson.get_compiler('c')
lib_hello = cc.find_library('hello',
               dirs : ['/opt/hello'])
inc_hello = include_directories('/opt/hello')
exec = executable('app',
                  'main.c',
                  dependencies : [lib_hello],
                  include_directories : inc_hello)
Yasushi Shoji
  • 4,028
  • 1
  • 26
  • 47
  • I get error - `Search directory opt/hello is not an absolute path.`. Any idea? – Royi Apr 21 '19 at 02:03
  • Assuming from the error message, did you forget to add the leading `/`? – Yasushi Shoji Apr 21 '19 at 07:10
  • I am not after `/opt/hello` as I'm on Windows. I just want to comment that for some reason `dirs` requires absolute path. – Royi Apr 21 '19 at 07:15
  • And if libhello has one million dependencies already specified by pklf-config you just happily replicate that configuration in your project? Not convinced by this approach in general. – joaerl Aug 31 '23 at 19:19
1

Refer to meson object here : current_source_dir() method returns a string to the current source directory.

Use it for the case libhello.so and libhello.h are located in <workspace>/hello directory

<workspace>/main.c
<workspace>/meson.build

<workspace>/hello/libhello.so
<workspace>/hello/libhello.h
<workspace>/hello/meson.build

In <workspace>/hello/meson.build:

lib_hello = cc.find_library('hello', dirs : meson.current_source_dir())

In <workspace>/meson.build:

project('myproj', 'c')
subdir('hello')

inc_hello = include_directories('./')
exec = executable('app',
                  'main.c',
                  dependencies : [lib_hello],
                  include_directories : inc_hello)