How to merge libraries into single shared library

Consider the following simple example below. Suppose I have library a and library b, where b is linked to a. I want to create a shared library b_merged that contains all the compiled code necessary to use library b. I want to be able to distribute this single shared library (b_merged), which users can link to to use library b. I don’t want to have to also distribute a - I want it to be incorporated into b_merged.

How should I make b_merged? Below, I tried add_library(b_merged SHARED b), but then b_merged does not end up containing func1 from library a, so the build fails.

cmake_minimum_required(VERSION "3.14")
project(TEST LANGUAGES C)

add_library(a a.c)
add_library(b b.c)
target_link_libraries(b a)

add_library(b_merged SHARED b)

add_executable(test test.c)
target_link_libraries(test b_merged)

a.c

int func1(){return 0;}

b.c

int func2(){return 0;}

test.c

#include "stdio.h"
int func1();
int func2();

int main()
{
  int a = func1();
  int b = func2();
  printf("Hello World\n");
  return 0;
}

try

add_library(a OBJECT a.c)
add_library(b OBJECT b.c)

the object libraries could be merged into one SHARED library b_merged.

I gave add_library(b OBJECT b.c) a try and it didn’t seem to solve the problem. I tried

add_library(a OBJECT a.c)
add_library(b OBJECT b.c)
target_link_libraries(b a)

add_library(b_merged SHARED $<TARGET_OBJECTS:b>)

And the library b_merged does not contain the function from library a

try to link a and b directly to b_merged:

target_link_libraries(b_merged a b)