前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux软件安装之源码编译安装详解

Linux软件安装之源码编译安装详解

原创
作者头像
手撕代码八百里
发布2023-12-13 22:32:30
2970
发布2023-12-13 22:32:30
举报
文章被收录于专栏:猿计划猿计划

前言

因为我是做Linux开发的,所以平时接触到的源码编译是必须的一个过程。因为编译环境不一样,所以编译的工具链也不一样,例如ARM架构的Linux平台和x86架构的Linux平台,编译的工具链就不一样,如果新手没有经验,一旦出问题就会一团乱麻。本篇文章我以ffmpeg的编译来介绍,编译过程中遇到了问题,如何解决。

ffmpeg的下载、编译与安装

下载

代码语言:shell
复制
zh@zh-lpc:~$ mkdir soft
zh@zh-lpc:~$ cd soft/
zh@zh-lpc:~/soft$ ls
zh@zh-lpc:~/soft$
zh@zh-lpc:~/soft$
zh@zh-lpc:~/soft$ git clone https://git.ffmpeg.org/ffmpeg.git
正克隆到 'ffmpeg'...
remote: Enumerating objects: 7535, done.
remote: Counting objects: 100% (7535/7535), done.
remote: Compressing objects: 100% (6563/6563), done.
remote: Total 634019 (delta 5120), reused 1190 (delta 967)
接收对象中: 100% (634019/634019), 155.59 MiB | 190.00 KiB/s, 完成.
处理 delta 中: 100% (509013/509013), 完成.
zh@zh-lpc:~/soft$
zh@zh-lpc:~/soft$
zh@zh-lpc:~/soft$ ls
ffmpeg
zh@zh-lpc:~/soft$
zh@zh-lpc:~/soft$
zh@zh-lpc:~/soft$ cd ffmpeg/
zh@zh-lpc:~/soft/ffmpeg$ ls
Changelog  CONTRIBUTING.md  COPYING.LGPLv2.1  doc      INSTALL.md   libavfilter  libpostproc    LICENSE.md   presets    tests
compat     COPYING.GPLv2    COPYING.LGPLv3    ffbuild  libavcodec   libavformat  libswresample  MAINTAINERS  README.md  tools
configure  COPYING.GPLv3    CREDITS           fftools  libavdevice  libavutil    libswscale     Makefile     RELEASE
zh@zh-lpc:~/soft/ffmpeg$

编译

代码语言:shell
复制
 ./configure --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex  --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --cc=gcc --host-cflags= --host-ldflags= --disable-x86asm

出现问题

ERROR: libfdk_aac not found

下载:

https://udomain.dl.sourceforge.net/project/opencore-amr/fdk-aac/fdk-aac-2.0.2.tar.gz

依次执行:

代码语言:shell
复制
./configure
make
make install

ERROR: speex not found using pkg-config

下载:https://www.speex.org/downloads/

代码语言:shell
复制
zh@zh-lpc:~$ ls ~/soft/speex-1.2.0.tar.gz
/home/zh/soft/speex-1.2.0.tar.gz
zh@zh-lpc:~$

依次执行:

代码语言:shell
复制
./configure
make
make install

ERROR: libx264 not found

libx264官网:https://www.videolan.org/developers/x264.html

代码语言:shell
复制
zh@zh-lpc:~/soft$ git clone https://code.videolan.org/videolan/x264.git
正克隆到 'x264'...
remote: Enumerating objects: 22884, done.
remote: Counting objects: 100% (416/416), done.
remote: Compressing objects: 100% (279/279), done.
remote: Total 22884 (delta 202), reused 330 (delta 137), pack-reused 22468
接收对象中: 100% (22884/22884), 6.12 MiB | 1.78 MiB/s, 完成.
处理 delta 中: 100% (18842/18842), 完成.
zh@zh-lpc:~/soft$

zh@zh-lpc:~/soft/x264$ ./configure
Found no assembler
Minimum version is nasm-2.13
If you really want to compile without asm, configure with --disable-asm.
zh@zh-lpc:~/soft/x264$
zh@zh-lpc:~/soft/x264$ ./configure  --disable-asm
platform:       X86_64
byte order:     little-endian
system:         LINUX
cli:            yes
libx264:        internal
shared:         no
static:         no
bashcompletion: yes
asm:            no
interlaced:     yes
avs:            avxsynth
lavf:           no
ffms:           no
mp4:            no
gpl:            yes
thread:         posix
opencl:         yes
filters:        crop select_every
lto:            no
debug:          no
gprof:          no
strip:          no
PIC:            no
bit depth:      all
chroma format:  all

You can run 'make' or 'make fprofiled' now.
zh@zh-lpc:~/soft/x264$
代码语言:shell
复制
make
make install

几个比较好的解决办法:

代码语言:shell
复制
# libfdk-aac
sudo apt-get install libfdk-aac-dev

# speex
sudo apt-get install libspeex-dev

# x264
sudo apt-get install libx264-dev

# x265
sudo apt-get install libx265-dev libnuma-dev

合集:

代码语言:shell
复制
sudo apt-get install libfdk-aac-dev  libspeex-dev libx264-dev libx265-dev libnuma-dev

Max OS:

代码语言:shell
复制
brew install fdk-aac

出现类似如下内容说明成功了:

然后编译:

如果你的计算机够强的话,可以使用多线程编译

代码语言:shell
复制
make -j8

安装:

代码语言:shell
复制
sudo make install

查看安装的内容

一下就是所安装的所有东西:

代码语言:shell
复制
zh@zh-lpc:~/soft/ffmpeg$ cd /usr/local/ffmpeg/
zh@zh-lpc:/usr/local/ffmpeg$ ls
bin  include  lib  share
zh@zh-lpc:/usr/local/ffmpeg$
zh@zh-lpc:/usr/local/ffmpeg$
zh@zh-lpc:/usr/local/ffmpeg$ tree
.
├── bin
│   ├── ffmpeg
│   └── ffprobe
├── include
│   ├── libavcodec
│   │   ├── ac3_parser.h
│   │   ├── adts_parser.h
│   │   ├── avcodec.h
│   │   ├── avdct.h
│   │   ├── avfft.h
│   │   ├── bsf.h
│   │   ├── codec_desc.h
│   │   ├── codec.h
│   │   ├── codec_id.h
│   │   ├── codec_par.h
│   │   ├── d3d11va.h
│   │   ├── defs.h
│   │   ├── dirac.h
│   │   ├── dv_profile.h
│   │   ├── dxva2.h
│   │   ├── jni.h
│   │   ├── mediacodec.h
│   │   ├── packet.h
│   │   ├── qsv.h
│   │   ├── vdpau.h
│   │   ├── version.h
│   │   ├── videotoolbox.h
│   │   ├── vorbis_parser.h
│   │   └── xvmc.h
│   ├── libavdevice
│   │   ├── avdevice.h
│   │   └── version.h
│   ├── libavfilter
│   │   ├── avfilter.h
│   │   ├── buffersink.h
│   │   ├── buffersrc.h
│   │   └── version.h
│   ├── libavformat
│   │   ├── avformat.h
│   │   ├── avio.h
│   │   └── version.h
│   ├── libavutil
│   │   ├── adler32.h
│   │   ├── aes_ctr.h
│   │   ├── aes.h
│   │   ├── attributes.h
│   │   ├── audio_fifo.h
│   │   ├── avassert.h
│   │   ├── avconfig.h
│   │   ├── avstring.h
│   │   ├── avutil.h
│   │   ├── base64.h
│   │   ├── blowfish.h
│   │   ├── bprint.h
│   │   ├── bswap.h
│   │   ├── buffer.h
│   │   ├── camellia.h
│   │   ├── cast5.h
│   │   ├── channel_layout.h
│   │   ├── common.h
│   │   ├── cpu.h
│   │   ├── crc.h
│   │   ├── des.h
│   │   ├── detection_bbox.h
│   │   ├── dict.h
│   │   ├── display.h
│   │   ├── dovi_meta.h
│   │   ├── downmix_info.h
│   │   ├── encryption_info.h
│   │   ├── error.h
│   │   ├── eval.h
│   │   ├── ffversion.h
│   │   ├── fifo.h
│   │   ├── file.h
│   │   ├── film_grain_params.h
│   │   ├── frame.h
│   │   ├── hash.h
│   │   ├── hdr_dynamic_metadata.h
│   │   ├── hmac.h
│   │   ├── hwcontext_cuda.h
│   │   ├── hwcontext_d3d11va.h
│   │   ├── hwcontext_drm.h
│   │   ├── hwcontext_dxva2.h
│   │   ├── hwcontext.h
│   │   ├── hwcontext_mediacodec.h
│   │   ├── hwcontext_opencl.h
│   │   ├── hwcontext_qsv.h
│   │   ├── hwcontext_vaapi.h
│   │   ├── hwcontext_vdpau.h
│   │   ├── hwcontext_videotoolbox.h
│   │   ├── hwcontext_vulkan.h
│   │   ├── imgutils.h
│   │   ├── intfloat.h
│   │   ├── intreadwrite.h
│   │   ├── lfg.h
│   │   ├── log.h
│   │   ├── lzo.h
│   │   ├── macros.h
│   │   ├── mastering_display_metadata.h
│   │   ├── mathematics.h
│   │   ├── md5.h
│   │   ├── mem.h
│   │   ├── motion_vector.h
│   │   ├── murmur3.h
│   │   ├── opt.h
│   │   ├── parseutils.h
│   │   ├── pixdesc.h
│   │   ├── pixelutils.h
│   │   ├── pixfmt.h
│   │   ├── random_seed.h
│   │   ├── rational.h
│   │   ├── rc4.h
│   │   ├── replaygain.h
│   │   ├── ripemd.h
│   │   ├── samplefmt.h
│   │   ├── sha512.h
│   │   ├── sha.h
│   │   ├── spherical.h
│   │   ├── stereo3d.h
│   │   ├── tea.h
│   │   ├── threadmessage.h
│   │   ├── timecode.h
│   │   ├── time.h
│   │   ├── timestamp.h
│   │   ├── tree.h
│   │   ├── twofish.h
│   │   ├── tx.h
│   │   ├── version.h
│   │   ├── video_enc_params.h
│   │   └── xtea.h
│   ├── libpostproc
│   │   ├── postprocess.h
│   │   └── version.h
│   ├── libswresample
│   │   ├── swresample.h
│   │   └── version.h
│   └── libswscale
│       ├── swscale.h
│       └── version.h
├── lib
│   ├── libavcodec.a
│   ├── libavcodec.so -> libavcodec.so.59.7.102
│   ├── libavcodec.so.59 -> libavcodec.so.59.7.102
│   ├── libavcodec.so.59.7.102
│   ├── libavdevice.a
│   ├── libavdevice.so -> libavdevice.so.59.0.101
│   ├── libavdevice.so.59 -> libavdevice.so.59.0.101
│   ├── libavdevice.so.59.0.101
│   ├── libavfilter.a
│   ├── libavfilter.so -> libavfilter.so.8.7.101
│   ├── libavfilter.so.8 -> libavfilter.so.8.7.101
│   ├── libavfilter.so.8.7.101
│   ├── libavformat.a
│   ├── libavformat.so -> libavformat.so.59.5.100
│   ├── libavformat.so.59 -> libavformat.so.59.5.100
│   ├── libavformat.so.59.5.100
│   ├── libavutil.a
│   ├── libavutil.so -> libavutil.so.57.4.101
│   ├── libavutil.so.57 -> libavutil.so.57.4.101
│   ├── libavutil.so.57.4.101
│   ├── libpostproc.a
│   ├── libpostproc.so -> libpostproc.so.56.0.100
│   ├── libpostproc.so.56 -> libpostproc.so.56.0.100
│   ├── libpostproc.so.56.0.100
│   ├── libswresample.a
│   ├── libswresample.so -> libswresample.so.4.0.100
│   ├── libswresample.so.4 -> libswresample.so.4.0.100
│   ├── libswresample.so.4.0.100
│   ├── libswscale.a
│   ├── libswscale.so -> libswscale.so.6.1.100
│   ├── libswscale.so.6 -> libswscale.so.6.1.100
│   ├── libswscale.so.6.1.100
│   └── pkgconfig
│       ├── libavcodec.pc
│       ├── libavdevice.pc
│       ├── libavfilter.pc
│       ├── libavformat.pc
│       ├── libavutil.pc
│       ├── libpostproc.pc
│       ├── libswresample.pc
│       └── libswscale.pc
└── share
    ├── ffmpeg
    │   ├── examples
    │   │   ├── avio_list_dir.c
    │   │   ├── avio_reading.c
    │   │   ├── decode_audio.c
    │   │   ├── decode_video.c
    │   │   ├── demuxing_decoding.c
    │   │   ├── encode_audio.c
    │   │   ├── encode_video.c
    │   │   ├── extract_mvs.c
    │   │   ├── filter_audio.c
    │   │   ├── filtering_audio.c
    │   │   ├── filtering_video.c
    │   │   ├── http_multiclient.c
    │   │   ├── hw_decode.c
    │   │   ├── Makefile
    │   │   ├── metadata.c
    │   │   ├── muxing.c
    │   │   ├── qsvdec.c
    │   │   ├── README
    │   │   ├── remuxing.c
    │   │   ├── resampling_audio.c
    │   │   ├── scaling_video.c
    │   │   ├── transcode_aac.c
    │   │   ├── transcoding.c
    │   │   ├── vaapi_encode.c
    │   │   └── vaapi_transcode.c
    │   ├── ffprobe.xsd
    │   ├── libvpx-1080p50_60.ffpreset
    │   ├── libvpx-1080p.ffpreset
    │   ├── libvpx-360p.ffpreset
    │   ├── libvpx-720p50_60.ffpreset
    │   └── libvpx-720p.ffpreset
    └── man
        ├── man1
        │   ├── ffmpeg.1
        │   ├── ffmpeg-all.1
        │   ├── ffmpeg-bitstream-filters.1
        │   ├── ffmpeg-codecs.1
        │   ├── ffmpeg-devices.1
        │   ├── ffmpeg-filters.1
        │   ├── ffmpeg-formats.1
        │   ├── ffmpeg-protocols.1
        │   ├── ffmpeg-resampler.1
        │   ├── ffmpeg-scaler.1
        │   ├── ffmpeg-utils.1
        │   ├── ffprobe.1
        │   └── ffprobe-all.1
        └── man3
            ├── libavcodec.3
            ├── libavdevice.3
            ├── libavfilter.3
            ├── libavformat.3
            ├── libavutil.3
            ├── libswresample.3
            └── libswscale.3

18 directories, 220 files
zh@zh-lpc:/usr/local/ffmpeg$

测试是否成功

代码语言:shell
复制
zh@zh-lpc:/usr/local/ffmpeg$ cd bin/
zh@zh-lpc:/usr/local/ffmpeg/bin$ ls
ffmpeg  ffprobe
zh@zh-lpc:/usr/local/ffmpeg/bin$
zh@zh-lpc:/usr/local/ffmpeg/bin$ ./ffmpeg -version
./ffmpeg: error while loading shared libraries: libavdevice.so.59: cannot open shared object file: No such file or directory
zh@zh-lpc:/usr/local/ffmpeg/bin$

配置动态库:

代码语言:shell
复制
sudo vim /etc/ld.so.conf.d/libc.conf

把自己的安装目录下的lib目录写进去

代码语言:shell
复制
zh@zh-lpc:/usr/local/ffmpeg$ cat /etc/ld.so.conf.d/libc.conf
# libc default configuration
/usr/local/lib
/usr/local/ffmpeg/lib

然后加载一下:

代码语言:shell
复制
sudo ldconfig

配置环境变量

代码语言:shell
复制
vim ~/.bashrc

尾部新增:

代码语言:shell
复制
#FFMPEG
export PATH=$PATH:/usr/local/ffmpeg/bin

立即生效:

代码语言:shell
复制
source ~/.bashrc

随后就可以执行了:

代码语言:shell
复制
zh@zh-lpc:~$ ffmpeg -h
ffmpeg version N-103553-g5e7e2e5031 Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 9 (Ubuntu 9.3.0-17ubuntu1~20.04)
  configuration: --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --cc=gcc --host-cflags= --host-ldflags= --disable-x86asm
  libavutil      57.  4.101 / 57.  4.101
  libavcodec     59.  7.102 / 59.  7.102
  libavformat    59.  5.100 / 59.  5.100
  libavdevice    59.  0.101 / 59.  0.101
  libavfilter     8.  7.101 /  8.  7.101
  libswscale      6.  1.100 /  6.  1.100
  libswresample   4.  0.100 /  4.  0.100
  libpostproc    56.  0.100 / 56.  0.100

没有ffplay的解决办法

可以看下生成的配置:

有一个!符号

代码语言:shell
复制
zh@zh-lpc:~/soft/ffmpeg$ cat ffbuild/config.mak |grep "CONFIG_FFPLAY"
!CONFIG_FFPLAY=yes
zh@zh-lpc:~/soft/ffmpeg$

而且也没有编译成ffplay可执行程序

代码语言:shell
复制
zh@zh-lpc:/usr/local/ffmpeg$ tree
.
├── bin
│   ├── ffmpeg
│   └── ffprobe

下载SDL2:

http://www.libsdl.org/download-2.0.php

然后解压并安装

代码语言:shell
复制
./configure
make
make install

然后重新执行:

代码语言:shell
复制
 ./configure --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex  --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --cc=gcc --host-cflags= --host-ldflags= --disable-x86asm

重新执行后发现!没了:

代码语言:shell
复制
zh@zh-lpc:~/soft/ffmpeg$ cat ffbuild/config.mak |grep "CONFIG_FFPLAY"
CONFIG_FFPLAY=yes
zh@zh-lpc:~/soft/ffmpeg$

然后进行编译安装:

代码语言:shell
复制
make -j8

然后安装:

代码语言:shell
复制
sudo make install

可以看到已经安装好了,也可以执行:

代码语言:shell
复制
zh@zh-lpc:~$ ls /usr/local/ffmpeg/bin/
ffmpeg  ffplay  ffprobe
zh@zh-lpc:~$

zh@zh-lpc:~$
zh@zh-lpc:~$ ffplay
ffplay version N-103553-g5e7e2e5031 Copyright (c) 2003-2021 the FFmpeg developers
  built with gcc 9 (Ubuntu 9.3.0-17ubuntu1~20.04)
  configuration: --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --cc=gcc --host-cflags= --host-ldflags= --disable-x86asm
  libavutil      57.  4.101 / 57.  4.101
  libavcodec     59.  7.102 / 59.  7.102
  libavformat    59.  5.100 / 59.  5.100
  libavdevice    59.  0.101 / 59.  0.101
  libavfilter     8.  7.101 /  8.  7.101
  libswscale      6.  1.100 /  6.  1.100
  libswresample   4.  0.100 /  4.  0.100
  libpostproc    56.  0.100 / 56.  0.100
Simple media player
usage: ffplay [options] input_file

缺少sdl2

Ubuntu使用ffplay播放器时:

代码语言:shell
复制
解决方案:

https://www.libsdl.org/download-2.0.php 下载好

1. 执行如下安装

sudo apt-get install libsdl2-dev

sudo apt-get install libsdl2-2.0-0

sudo apt-get install libsdl2-image-dev

sudo apt-get install libsdl2-image-2.0-0

sudo apt-get install libsdl2-mixer-dev

sudo apt-get install libsdl2-mixer-2.0-0

sudo apt-get install libsdl2-net-dev

sudo apt-get install libsdl2-net-2.0-0

2.进入SDL安装源文件目录:

cd ~/Documents/SDL2-2.0.12

3.卸载SDL

sudo make uninstall

4. 清理:

make clean && make distclean

5.重新配置编译安装SDL2:

./configure --prefix=/usr/local --enable-shared --disable-static && make -j 4 && sudo make install

6. 重新编译ffmpeg即可
代码语言:shell
复制
sudo apt-get install libsdl2-dev libsdl2-2.0-0 libsdl2-image-dev libsdl2-image-2.0-0 libsdl2-mixer-dev libsdl2-mixer-2.0-0 libsdl2-net-dev libsdl2-net-2.0-0

总结

只要牵扯到编译的,都必须经过:configure, make,make install 这几个流程。遇到问题了只有一步一的去解决,遇到什么问题解决什么问题才可以。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • ffmpeg的下载、编译与安装
  • 下载
  • 编译
  • 出现问题
  • 查看安装的内容
  • 测试是否成功
  • 没有ffplay的解决办法
  • 缺少sdl2
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档