Creating a shared library that statically includes ffmpeg

I find it difficult to create a shared library in which ffmpeg libraries are "baked" as static.

Consider the following directory scheme:

include/
  my own .h files
  ext/
    ffmpeg .h files
lib/
  libav*.a archive files (softlinks to the actual .a files)
  libValkka.so (my shared library)
test/
  mytest.cpp
bin/
  (binaries appear here)

I have come a long way (see Including objects in a shared library from a C ++ archive (.a) ), and the library compiles this with this: ([STUFF] omitted for brevity)

/ usr / bin / C ++ -fPIC -std = C ++ 14 -pthread -Iinclude / ext -I / usr / include / libdrm -g -shared -Wl, -soname, libValkka.so -o lib / libValkka. so CMakeFiles / Valkka.dir / src / avthread.cpp.o CMakeFiles / Valkka.dir / src / opengl.cpp.o CMakeFiles / Valkka.dir / src / openglthread.cpp.o [STUFF] CMakeFiles / Valkka.dir / src /filters.cpp.o -lX11 -lGLEW -lGLU -lGL -Wl, - allow-multiple-definition -Wl, -Bsymbolic -Wl, -whole-archive -Wreorder lib / libavdevice.a lib / libavfilter.a lib / libavformat .a lib / libavcodec.a lib / libavutil.a lib / libswscale.a lib / libswresample.a -Wl, - no-whole-archive

However, when creating executable files, their source code does not use the ffmpeg api (only my own api) - with:

C ++ -std = C ++ 14 -pthread -Iinclude -Iinclude / ext -Llib test / mytest.cpp -lValkka -g -o bin / mytest

ffmpeg. , - :

lib/libValkka.so: undefined reference to `pa_stream_get_index'
lib/libValkka.so: undefined reference to `deflateInit_'
lib/libValkka.so: undefined reference to `pa_stream_get_state'
lib/libValkka.so: undefined reference to `lzma_stream_decoder'
lib/libValkka.so: undefined reference to `BZ2_bzDecompress'
lib/libValkka.so: undefined reference to `vaInitialize'
lib/libValkka.so: undefined reference to `pa_stream_unref'
lib/libValkka.so: undefined reference to `deflateInit2_'
lib/libValkka.so: undefined reference to `snd_pcm_close'
...
lib/libValkka.so: undefined reference to `vaGetDisplayDRM'
lib/libValkka.so: undefined reference to `vaMaxNumEntrypoints'
lib/libValkka.so: undefined reference to `uncompress'
lib/libValkka.so: undefined reference to `pa_stream_drop'
lib/libValkka.so: undefined reference to `pa_context_connect'
lib/libValkka.so: undefined reference to `FT_Get_Kerning'
lib/libValkka.so: undefined reference to `ass_free_track'
lib/libValkka.so: undefined reference to `pa_operation_unref'
lib/libValkka.so: undefined reference to `FT_Stroker_Done'
lib/libValkka.so: undefined reference to `vaTerminate'
lib/libValkka.so: undefined reference to `ass_new_track'
lib/libValkka.so: undefined reference to `jack_client_close'
...
lib/libValkka.so: undefined reference to `xcb_xfixes_query_version'
lib/libValkka.so: undefined reference to `xcb_shape_rectangles'
lib/libValkka.so: undefined reference to `pa_mainloop_free'
lib/libValkka.so: undefined reference to `snd_device_name_hint'
lib/libValkka.so: undefined reference to `vaCreateImage'
lib/libValkka.so: undefined reference to `vaBeginPicture'
lib/libValkka.so: undefined reference to `DtsSetColorSpace'
lib/libValkka.so: undefined reference to `vaDestroyConfig'
lib/libValkka.so: undefined reference to `pa_stream_writable_size'
lib/libValkka.so: undefined reference to `snd_pcm_hw_params_get_buffer_size_max'
lib/libValkka.so: undefined reference to `ass_read_file'

, , ..!

nm lib/libValkka.so | grep "vaBeginPicture"

U vaBeginPicture

... , .a, :

..... -Wl, - allow-multiple-definition -Wl, -Bsymbolic -Wl, - start-group -Wl, - whole-archive -Wreorder lib/libavdevice.a lib/libavfilter. lib/libavformat.a lib/libavcodec.a lib/libavutil.a lib/libswscale.a lib/libswresample.a -Wl, - no-whole-archive -Wl, - end-group

.

, "" .a , ffmpeg, .

. - , - ? !

0
2

/ , ffmpeg: libbz2, libva, libxcb, libass, freetype2 .. - ffmpeg distribution/build (automake.pc )

- ; , ; , . , , libavdevice, , , , . , , libavdevice - , .

+2

, :

++ -std = ++ 14 -pthread -Iinclude -Iinclude/ext -Llib test/mytest.cpp -lValkka -g -o bin/mytest -Wl, - unresolved-symbols = ignore-all

.

, , . , ? ?

, ffmpeg, ffmpeg configure script. , python script, . , :

#!/usr/bin/python3
"""
* Creates script "run_configure.bash" that launches ffmpeg "configure" script with correct parameters (enabling/disabling stuff)
* Run in the same directory where you have ffmpeg configure script
"""
import subprocess
import os
import re

def features(switch, adstring="", remove=[]):
  p=subprocess.Popen(["./configure",switch],stdout=subprocess.PIPE)
  st=p.stdout.read()
  fst=""
  for s in st.split():
    ss=s.decode("utf-8")
    ok=True
    for rem in remove:
      if (ss.find(rem)!=-1):
        ok=False
    if ok: fst+=adstring+ss+" "
  return fst


def disable_external():  
  p=subprocess.Popen(["./configure","-h"],stdout=subprocess.PIPE)
  st=p.stdout.read().decode("utf-8")

  # find some text tags from the configure output:
  # i1=st.find("External library support:")
  i1=st.find("themselves, not all their features will necessarily be usable by FFmpeg.")
  i2=st.find("Toolchain options:")
  st=st[i1:i2]
  """ # debugging ..
  print(st)
  stop
  """
  p=re.compile('--(enable|disable)-(\S*)')
  switches=[]
  for sw in p.findall(st):
    if (sw[1] not in switches):
      # print(sw[1]) # debugging
      switches.append(sw[1])
  fst=""
  for sw in switches:
    fst+="--disable-"+sw+" "
  return fst

st ="./configure "
st+="--disable-everything --disable-doc --disable-gpl --disable-pthreads --enable-static --enable-shared "
st+= disable_external()
st+= features("--list-decoders",adstring="--enable-decoder=", remove=["vdpau","crystalhd","zlib"])
st+= features("--list-muxers",  adstring="--enable-muxer=")
st+= features("--list-demuxers",adstring="--enable-demuxer=")
st+= features("--list-parsers", adstring="--enable-parser=")

f=open("run_configure.bash","w")
f.write("#!/bin/bash\n")
f.write(st+"\n")
f.close()
os.system("chmod a+x run_configure.bash")
print("\nNext run ./run_configure.bash\n")

"""
For cleaning up .a and .so files, use
find -name *.a -exec ls {} \;
find -name *.so* -exec ls {} \;
"""

, - . python3.

0

Source: https://habr.com/ru/post/1685980/


All Articles