4

I have a static library from the project A (let's call it liba.so) and I want to compile a shared library in my project B (let's call it libb.so) and embed liba.so in it.

Also, I have a binary in that project B which also depends on liba.so, so I want to embed it in the binary.

Is that possible? How?

alexandernst
  • 14,352
  • 22
  • 97
  • 197
  • 3
    Are you sure project A creates a static library ? liba.so is the name of a shared library, which you cannot embed into another shared library - liba.a would be a proper name for a static library. However, see also http://stackoverflow.com/questions/2763988/how-to-include-all-objects-of-an-archive-in-a-shared-object – nos Jan 07 '16 at 19:05
  • @nos Indeed, you're right. It's `liba.a`. So I should compile `libb.a`. Anyways, I'm still not sure how to make meson do it. – alexandernst Jan 07 '16 at 19:12
  • please update the question with `liba.a` as appropriate. This is confusing as is – joel Feb 25 '22 at 01:28

2 Answers2

5

When A is a Separate Code Base

What you do is build and install project A. Then create a dependency on project A in project B's definition.

That looks like this:

a_dep = dependency('a', version : '>=1.2.8')    
lib_b = shared_library('proj_b', sources: 'prog_b.c', dependencies : a_dep)

The version section in dependency is optional.

When A is in the Same Meson Project as B

When A and B are in the same meson project, it's a little uglier. You have to declare a dependency anchor in A.

That looks like this:

incdirs = include_directories('include')
lib_a = static_library('a', 'proj_a.c', include_directories : indirs)

liba_dependency = declare_dependency(
   include_directories : incdirs,
   link_with : lib_a,
   sources : ['proj_a.c'])

Then project B becomes:

lib_b = shared_library('proj_b', sources: 'prog_b.c', dependencies : lib_a)
oz10
  • 153,307
  • 27
  • 93
  • 128
4

If you have an existing, precompiled library, then you can directly wrap it in a dependency:

cpp = meson.get_compiler('cpp')

# (Meson requires an absolute path for find_library().)
libdir = meson.current_source_dir() + './lib/

precompiledA_dep = cpp.find_library('A', dirs : libdir) # ./lib/libA.lib

...
# Link against libA.lib here ...
B_lib = library('libB', 'libB.cpp', dependencies : precompiledA_dep)
B_exe = executable('exeB', 'source.cpp', dependencies : precompiledA_dep)

(tested with Meson 0.57)

ManuelAtWork
  • 2,198
  • 29
  • 34