Linking my static C++ library with libavutil cause to undefined reference

219 Views Asked by At

I'm facing some strange problem. I have a static library that uses the Ffmpeg libraries internally. For some reason I get the error: undefined reference to `av_log_set_level(int)

the code is as follows:

#ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#ifdef __cplusplus
}
#endif
int some_func()
{
    av_log_set_level(AV_LOG_QUIET);        
}

As you can see the Ffmpeg libraries are enclosed into extern "C". I use CMake file, that looks like follows:

cmake_minimum_required (VERSION 3.23)

add_library(MyLib STATIC lib.cpp)
target_link_libraries(MyLib  -lavformat -lavcodec -lswscale -lavutil)

add_executable(MyApp main.cpp)
target_link_libraries(MyApp  MyLib -lavformat -lavcodec -lswscale -lavutil)

But nothing helps, I still get the same error.

I've tried to check the symbols of my library with nm:

nm .MyLib.a | grep av_log_set_level
U _Z16av_log_set_leveli

the same function in libavutil:

nm /usr/lib/x86_64-linux-gnu/libavutil.a | grep av_log_set_level
0000000000000ff0 T av_log_set_level

I guess that for some reason my static library uses c++ mangling and so it looks for the incorrect function, _Z16av_log_set_leveli instead of av_log_set_level that causes for undefined reference error. What can I do, how can I fix that?

The environment:

  • Ubuntu 22.04
  • g++ 9.4.0
  • libavutil-dev:amd64 7:4.2.7-0ubuntu0.1
0

There are 0 best solutions below