0
votes

I am trying to use the libavformat from ffmpeg in a C++ project. I have ffmpeg installed using homebrew.

My CMakeLists.txt :

cmake_minimum_required(VERSION 3.14)
project(av_test)

set(CMAKE_CXX_STANDARD 11)

INCLUDE_DIRECTORIES(/usr/local/Cellar/ffmpeg/4.2.1_2/include)
LINK_DIRECTORIES(/usr/local/Cellar/ffmpeg/4.2.1_2/lib)

add_executable(av_test main.cpp)

TARGET_LINK_LIBRARIES(av_test libavformat)

When running cmake I get this error:

ld: library not found for -llibavformat
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [av_test] Error 1
make[2]: *** [CMakeFiles/av_test.dir/all] Error 2
make[1]: *** [CMakeFiles/av_test.dir/rule] Error 2
make: *** [av_test] Error 2

A quick find libavformat* into /usr/local/Cellar/ffmpeg/4.2.1_2/lib returns:

libavformat.58.29.100.dylib
libavformat.58.dylib
libavformat.a
libavformat.dylib

Also in /usr/local/Cellar/ffmpeg/4.2.1_2/include/libavformat there is avformat.h

My main.cpp:

#include <libavformat/avformat.h>

int main() {

    AVFormatContext *pFormatContext;

    return 0;
}

I am running on mac os 10.14 with cmake version 3.15.5 , ffmpeg version 4.2.1

1
Try writing TARGET_LINK_LIBRARIES(av_test avformat) or Try writing TARGET_LINK_LIBRARIES(av_test -lavformat)Saisai3396
@Saisai3396 Thanks, this works! Can you please write an answer so I can mark it as the solution?Alexandru Cancescu
Glad to help! :)Saisai3396

1 Answers

1
votes

The problem here is noted from the error:

ld: library not found for -llibavformat

As you can see, it has both '-l' and 'lib' as prefix even though '-l' should replace 'lib' before the library name. It seems the TARGET_LINK_LIBRARIES function parses library names with a prefix -l (or lib) automatically attached. So, you need to write either of the following:

TARGET_LINK_LIBRARIES(av_test avformat) TARGET_LINK_LIBRARIES(av_test -lavformat)

Adding -l at start doesn't make a difference, but is still accepted by cmake.