且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Cmake中链接libavcodec,find_library不适用于任何库

更新时间:2023-02-03 12:33:10

这是缺少的内容(即使 libavcodec 出现在我系统的某些文件夹中)

  sudo apt install -y libavcodec- dev libavformat-dev libavdevice-dev libavfilter-dev 


I'm trying to compile a cpp code which uses libavcodec:

 #include <libavcodec/avcodec.h>

I tried all variations of

find_package()

with names like ffmpeg for which I get

CMake Warning at CMakeLists.txt:17 (find_package):
  By not providing "Findffmpeg.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "ffmpeg", but
  CMake did not find one.

  Could not find a package configuration file provided by "ffmpeg" with any
  of the following names:

    ffmpegConfig.cmake
    ffmpeg-config.cmake

  Add the installation prefix of "ffmpeg" to CMAKE_PREFIX_PATH or set
  "ffmpeg_DIR" to a directory containing one of the above files.  If "ffmpeg"
  provides a separate development package or SDK, be sure it has been
  installed.

By the way, I did sudo apt install -y ffmpeg before all that.

I'm compiling with

add_executable(my_executable ${sources})

Minimal working example

CMakeLists.txt

cmake_minimum_required(VERSION 3.1.0)

project(hello)

find_library(AVCODEC_LIBRARY avcodec)

add_executable(hello main.cpp)

target_link_libraries( hello PRIVATE ${AVCODEC_LIBRARY})

main.cpp

#include <iostream>
using namespace std;

int main() 
{
    cout << "Hello, World!";
    return 0;
}

Output:

lz@vm:~/mcve$ cmake .
-- The C compiler identification is GNU 7.2.0
-- The CXX compiler identification is GNU 7.2.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
AVCODEC_LIBRARY
    linked by target "hello" in directory /home/lz/mcve

-- Configuring incomplete, errors occurred!
See also "/home/lz/mcve/CMakeFiles/CMakeOutput.log".

This is what was missing (even though libavcodec appeared in some folders of my system)

sudo apt install -y libavcodec-dev libavformat-dev libavdevice-dev libavfilter-dev