6

I am trying to link to FFmpeg built for android using android-ndk-r15c. I built this by downloading FFmpeg source that is latest ffmpeg-3.3.4.

Following are my linker list:

-lavformat -lavcodec -lswscale -lavutil -lavfilter -lswresample -lavdevice -lpostproc

I get the following errors complaining

libavformat/hls.c:783: error: undefined reference to 'atof'
libavcodec/ffv1enc.c:146: error: undefined reference to 'log2'
libavcodec/imc.c:428: error: undefined reference to 'log2f'

Following are my FFmpeg related includes:

#include <stdint.h>
#include <cstdlib>

#define __STDC_CONSTANT_MACROS

extern "C" {

#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/mathematics.h"
#include "libavcodec/version.h"
#include "libavutil/rational.h"
#include "libavutil/avstring.h"
#include "libswscale/swscale.h"

}

Following is my buildscript to cross-compile FFmpeg for android:

#!/bin/bash

cd ffmpeg-3.3.4

NDK=/path/to/ndk/android-ndk-r15c
SYSROOT=$NDK/platforms/android-21/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
CPREFIX=$TOOLCHAIN/bin/arm-linux-androideabi-ar
CC=$TOOLCHAIN/bin/arm-linux-androideabi-gcc
CXX=$TOOLCHAIN/bin/arm-linux-androideabi-g++
LD=$TOOLCHAIN/bin/arm-linux-androideabi-ld
RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib
STRIP=$TOOLCHAIN/bin/arm-linux-androideabi-strip

function build_ffmpeg_android {

./configure \
    --prefix=$PREFIX \
    --disable-stripping \
    --arch=arm \
    --cpu=cortex-a8 \
    --target-os=linux \
    --enable-cross-compile \
    --enable-debug \
    --enable-pic \
    --disable-programs \
    --enable-static \
    --disable-shared \
    --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
    --disable-doc \
    --enable-postproc \
    --enable-swscale \
    --enable-avfilter \
    --enable-avresample \
    --disable-opencl \
    --disable-securetransport \
    --sysroot=$SYSROOT \
    --enable-videotoolbox \
    --enable-avresample \
    --disable-symver \
    #--enable-gpl \
    #--enable-libx264
    $ADDITIONAL_CONFIGURE_FLAG
    make clean
    make -j9
    make install
}

CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"

build_ffmpeg_android

Question:
Which library am I missing to link to ?

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • 1
    The standard C++ math functions are in a separate library that needs to be linked, called (plain and simply) `m`. So add `-lm` to link with it. – Some programmer dude Oct 09 '17 at 14:15
  • 1
    just add `-lm`? seems like not enough. any other libraries I need ? I am working with C++14 – TheWaterProgrammer Oct 09 '17 at 14:22
  • No that's about it. That's the standard C++ (and C for that matter) math library. – Some programmer dude Oct 09 '17 at 14:26
  • it did not help but thanks anyways. what about the `log2` & `log2f` ? – TheWaterProgrammer Oct 09 '17 at 14:42
  • Try maybe adding `-lm` *after* your linker list. – Shmuel H. Oct 09 '17 at 15:55
  • adding `-lm` does not make a difference. I get the same list of errors. – TheWaterProgrammer Oct 09 '17 at 16:07
  • *Where* did you add it? Order may matter. If library A depends on library B, then A must usually come *before* B on the command line when linking. – Some programmer dude Oct 09 '17 at 16:18
  • I added it in the end. here is the linker list `-lavformat -lavcodec -lswscale -lavutil -lavfilter -lswresample -lavdevice -lpostproc -lm`. is this correct? – TheWaterProgrammer Oct 09 '17 at 16:30
  • Yes it is. That's strange. I think you might have a problem with your `glibc` libraries. Can you try to compile a little C program that uses `atof` and `log2`? BTW, in my system at least, `atof` is not in `libm.a` but in `libc.a`. – Shmuel H. Oct 09 '17 at 16:41
  • By the way, I used the following flags in `configure` to build FFmpeg. Is this correct ? `--extra-cflags="-O3 -Wall -pipe -std=c99 -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID -DNDEBUG-march=armv5te -mtune=arm9tdmi -msoft-float"` – TheWaterProgrammer Oct 09 '17 at 16:42
  • 1
    Try to add `--extra-ldflags=-L$SYSROOT/usr/lib/`. And check what actual link command looks like. It should pick up the system libraries from **platforms/android-21/arch-arm/usr/libs**. – Alex Cohn Oct 09 '17 at 17:38
  • 2
    @AlexCohn selecting anything below **platforms/android-21** works. You can please post an answer to this topic to close this question properly. thanks for that suggestion. building FFmpeg against latest ndk that is *15c* & android-26 was a really bad idea – TheWaterProgrammer Oct 09 '17 at 21:13
  • Wait a sec. The link error about **atof()**, etc. comes not from your **buildscript**, but from **ndk-build** for your app that uses the ffmpeg static libs? And what is your target platform? – Alex Cohn Oct 10 '17 at 05:52
  • 1
    exact `buildscript` run below **android-21** using ndk r10e works. Yes. no changes to script – TheWaterProgrammer Oct 10 '17 at 06:53
  • No, I am afraid we didn't understand each other. Let us continue in [chat](https://chat.stackoverflow.com/rooms/156590/room-for-programist-and-alex-cohn) – Alex Cohn Oct 12 '17 at 19:11
  • See https://stackoverflow.com/a/41079462/192373 – Alex Cohn Oct 16 '17 at 19:59
  • For a shorter, less rambling answer: https://android.googlesource.com/platform/ndk/+/master/docs/user/common_problems.md#target-api-set-higher-than-device-api (assuming I'm correctly understanding why that answer got linked) – Dan Albert Nov 09 '17 at 18:46

2 Answers2

1

I ran into this issue building ffmpeg-2.8.15 with x264 using ndk-r10e with platform android-15. I updated the code to use android-21 and it compiled the code without any issues. Our min sdk version is 21.

Ray Hunter
  • 15,137
  • 5
  • 53
  • 51
0

You also have the option to remove those math functions from FFMPEG configure file and rebuild. Your resoling lib-*.so files will no longer call those symbols.