Skip to content

Commit 23b0cbf

Browse files
committed
Static ffmpeg build for Linux
Previously the system version of ffmpeg was linked dynamically by accident
1 parent 6879295 commit 23b0cbf

File tree

3 files changed

+53
-28
lines changed

3 files changed

+53
-28
lines changed

CMakeLists.txt

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@ else()
3434
endif()
3535

3636

37-
set(ffmpegLibHint ${defFFmpegLibHint} CACHE PATH "directory with static ffmpeg libs")
37+
set(ffmpegLibHint "${CMAKE_CURRENT_SOURCE_DIR}/${defFFmpegLibHint}" CACHE PATH "directory with static ffmpeg libs")
3838
set(libSuffix ${defLibSuffix} CACHE STRING "output library suffix")
3939
set(pluginDir ${defPluginDir} CACHE PATH "TS3 plugin directory (for file copy)")
4040

41-
42-
find_path(ffmpegIncludeDir libavcodec/avcodec.h HINTS ${ffmpegIncHint})
43-
find_library(avcodec avcodec HINTS ${ffmpegLibHint})
44-
find_library(avformat avformat HINTS ${ffmpegLibHint})
45-
find_library(avutil avutil HINTS ${ffmpegLibHint})
46-
find_library(swresample swresample HINTS ${ffmpegLibHint})
41+
find_path(ffmpegIncludeDir libavcodec/avcodec.h PATHS ${ffmpegIncHint} NO_DEFAULT_PATH)
42+
find_library(avcodec avcodec PATHS ${ffmpegLibHint} NO_DEFAULT_PATH)
43+
find_library(avformat avformat PATHS ${ffmpegLibHint} NO_DEFAULT_PATH)
44+
find_library(avutil avutil PATHS ${ffmpegLibHint} NO_DEFAULT_PATH)
45+
find_library(swresample swresample PATHS ${ffmpegLibHint} NO_DEFAULT_PATH)
4746

4847
find_package(Qt5 COMPONENTS Core Widgets Gui Network REQUIRED)
4948

@@ -55,6 +54,9 @@ set(CMAKE_AUTORCC ON)
5554
# Find includes in corresponding build directories
5655
set(CMAKE_INCLUDE_CURRENT_DIR ON)
5756

57+
# Turn fPIC on
58+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
59+
5860
# version advance script (generates version.h file)
5961
add_custom_command(
6062
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/version/version.h"
@@ -67,27 +69,40 @@ set_property(SOURCE "src/version/version.h" PROPERTY SKIP_AUTOGEN ON)
6769

6870
# actual library definition
6971
add_library(rp_soundboard SHARED ${sources} "src/version/version.h")
70-
target_link_libraries(rp_soundboard
71-
Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Network
72-
${avcodec} ${avformat} ${avutil} ${swresample}
73-
)
72+
target_link_libraries(rp_soundboard Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Network)
73+
if (WIN32)
74+
# only link this way on Windows. Linux requires VERY special linker commands, see below
75+
target_link_libraries(rp_soundboard ${avcodec} ${avformat} ${avutil} ${swresample})
76+
endif()
77+
7478
target_include_directories(rp_soundboard PUBLIC "pluginsdk/include" ${ffmpegIncludeDir})
7579
set_target_properties(rp_soundboard PROPERTIES
7680
SUFFIX ${libSuffix}
7781
RUNTIME_OUTPUT_DIRECTORY_DEBUG debug
7882
RUNTIME_OUTPUT_DIRECTORY_RELEASE release
7983
)
8084

85+
# Special platform dependent compile options
8186
if (MSVC)
8287
target_sources(rp_soundboard PRIVATE "src/windows/resource.h" "src/windows/Resource.rc")
8388
target_link_libraries(rp_soundboard wsock32 ws2_32 secur32) # some windows stuff
8489
target_compile_options(rp_soundboard PRIVATE /MP) # multiprocessor compiling
85-
elseif(APPLE)
86-
target_compile_definitions(rp_soundboard PRIVATE "MACOS")
8790
else()
88-
target_compile_definitions(rp_soundboard PRIVATE "LINUX")
91+
if(APPLE)
92+
target_compile_definitions(rp_soundboard PRIVATE "MACOS")
93+
else()
94+
target_compile_definitions(rp_soundboard PRIVATE "LINUX")
95+
endif()
96+
# Compile options that are required to NOT get the "recompile with -fPIC"
97+
# error when linking the ffmpeg libs. Took me many hours to find this out...
98+
set_target_properties(rp_soundboard PROPERTIES LINK_FLAGS
99+
"-Wl,-Bsymbolic -Wl,--whole-archive \
100+
${avcodec} ${avformat} ${avutil} ${swresample} \
101+
-Wl,--no-whole-archive"
102+
)
89103
endif()
90104

105+
91106
# copy to teamspeak dir command stuff
92107
set(COPY_DLL_TO_TEAMSPEAK_DIR TRUE CACHE BOOL "Copy the soundboard DLL to teamspeaks directory")
93108
if (COPY_DLL_TO_TEAMSPEAK_DIR)

ffmpeg/build-scripts/build_ffmpeg.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fi
3030
echo "Machine =" $machine $arch
3131

3232
opts="\
33+
--enable-pic \
3334
--disable-programs \
3435
--disable-doc \
3536
--disable-avdevice \

ffmpeg/build-scripts/copy_binaries.sh

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,36 @@
44

55
os="$(uname -s)"
66
case "$os" in
7-
Linux*) prefix="lib_lin"
8-
postfix="a" ;;
9-
Darwin*) prefix="lib_mac"
10-
postfix="a" ;;
11-
*) prefix="lib_win"
12-
postfix="lib" ;;
7+
Linux*)
8+
prefix="lib_lin"
9+
postfix="a"
10+
infix="lib"
11+
;;
12+
Darwin*)
13+
prefix="lib_mac"
14+
postfix="a"
15+
infix="lib"
16+
;;
17+
*)
18+
prefix="lib_win"
19+
infix=""
20+
postfix="lib"
21+
;;
1322
esac
1423

1524
pushd ..
1625

1726
mkdir -p "${prefix}_x64"
1827
mkdir -p "${prefix}_x86"
1928

20-
cp ffmpeg/x64/lib/libavcodec.a ${prefix}_x64/avcodec.$postfix
21-
cp ffmpeg/x64/lib/libavformat.a ${prefix}_x64/avformat.$postfix
22-
cp ffmpeg/x64/lib/libavutil.a ${prefix}_x64/avutil.$postfix
23-
cp ffmpeg/x64/lib/libswresample.a ${prefix}_x64/swresample.$postfix
29+
cp ffmpeg/x64/lib/libavcodec.a ${prefix}_x64/${infix}avcodec.$postfix
30+
cp ffmpeg/x64/lib/libavformat.a ${prefix}_x64/${infix}avformat.$postfix
31+
cp ffmpeg/x64/lib/libavutil.a ${prefix}_x64/${infix}avutil.$postfix
32+
cp ffmpeg/x64/lib/libswresample.a ${prefix}_x64/${infix}swresample.$postfix
2433
cp -R ffmpeg/x64/include ${prefix}_x64/include
2534

26-
cp ffmpeg/x86/lib/libavcodec.a ${prefix}_x86/avcodec.$postfix
27-
cp ffmpeg/x86/lib/libavformat.a ${prefix}_x86/avformat.$postfix
28-
cp ffmpeg/x86/lib/libavutil.a ${prefix}_x86/avutil.$postfix
29-
cp ffmpeg/x86/lib/libswresample.a ${prefix}_x86/swresample.$postfix
35+
cp ffmpeg/x86/lib/libavcodec.a ${prefix}_x86/${infix}avcodec.$postfix
36+
cp ffmpeg/x86/lib/libavformat.a ${prefix}_x86/${infix}avformat.$postfix
37+
cp ffmpeg/x86/lib/libavutil.a ${prefix}_x86/${infix}avutil.$postfix
38+
cp ffmpeg/x86/lib/libswresample.a ${prefix}_x86/${infix}swresample.$postfix
3039
cp -R ffmpeg/x86/include ${prefix}_x86/include

0 commit comments

Comments
 (0)