跳到主要内容

库的编译

阅读量

0

阅读人次

0

Vcpkg

vcpkg 可帮助我们在 Windows、 Linux 和 MacOS 上管理 C 和 C++ 库。 这个工具和生态链正在不断发展。

# 安装 vcpkg
git clone https://github.com/microsoft/vcpkg
.\vcpkg\bootstrap-vcpkg.bat
# 安装项目所需要的库
.\vcpkg\vcpkg install [package name]:x64-windows

在 CMake 中使用 vcpkg,需要使用以下工具链文件:

cmake -B [build directory] -S . "-DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake"
cmake --build [build directory]

Boost

Linux GCC编译

sudo apt install libpython3.11-dev
tar -xvf boost_1_86_0.tar.gz
cd boost_1_86_0/
./bootstrap.sh --prefix=/opt/Libraries/boost_1_86_0
./b2 install -q --prefix=/opt/Libraries/boost_1_86_0 threading=multi link=shared runtime-link=shared variant=release cxxstd=17 cxxflags=-fPIC cflags=-fPIC

#PIC可加上 cxxflags=-fPIC cflags=-fPIC
#静态编译 link=static (应用程序动态库开发时,不建议静态构建,因为boost库大多数都是header-only,会导致每个动态库存在一个实例的情况)

ARM-GCC交叉编译

tar -xvf boost_1_86_0.tar.gz
cd boost_1_86_0/
./bootstrap.sh --prefix=/opt/RaspberryPi/boost_1_86_0

然后会在boost_1_86_0/目录下生成一个project-config.jam文件。打开该文件,作如下修改。

# Compiler configuration. This definition will be used unless
# you already have defined some toolsets in your user-config.jam
# file.
if ! gcc in [ feature.values <toolset> ]
{
using gcc : : /opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc :
<linkflags>-lstdc++
;
}

然后执行:

./b2 install -q --prefix=/opt/RaspberryPi/boost_1_86_0 link=shared threading=multi runtime-link=shared variant=release cxxstd=17 
#PIC可加上 cxxflags=-fPIC cflags=-fPIC

有的交叉编译器不支持pch,如果编译失败,可尝试加上 pch=off

NDK

编译libffi

下载源码libffi,在MSYS2下编译。

pacman -S diffutils
./configure --prefix=/d/Android/libffi-3.4.4 --host=aarch64-linux-android30     CFLAGS="-march=armv8-a -DANDROID" --enable-static --disable-shared  --disable-multi-os-directory --target=aarch64-linux-android30

编译python

(目前编译完成后,编译应用无法正常运行,直接crash。)下载Python源码。

pacman -S pkg-config python3

export NDK_HOME=/d/Android/Sdk/ndk/25.1.8937393
export COMPILER_ROOT=${NDK_HOME}/toolchains/llvm/prebuilt/windows-x86_64
export CC=${COMPILER_ROOT}/bin/aarch64-linux-android30-clang
export LD=${COMPILER_ROOT}/bin/ld.ldd.exe
export AR=${COMPILER_ROOT}/bin/llvm-ar.exe
export READELF=${COMPILER_ROOT}/bin/llvm-readelf.exe
export ac_cv_file__dev_ptmx=no
export ac_cv_file__dev_ptc=no
./configure CFLAGS="-fPIC -I/d/Android/libffi-3.4.4/include" LDFLAGS="-L/d/Android/libffi-3.4.4/lib -lffi" --prefix=/d/Android/python-3.11.4 --enable-shared --host=aarch64-linux-android --target=aarch64-linux-android30  --build=x86_64-linux-gnu --with-build-python --disable-ipv6

在生成的Makefile中,将INSTSONAME= libpython$(LDVERSION).so.1.0修改为INSTSONAME= libpython$(LDVERSION).so,NDK中只能识别以.so为后缀名的动态库。

boostcpp.jam找到如下内容,注释掉,使得动态库的扩展名为.so

if $(type) = SHARED_LIB &&
! [ $(property-set).get <target-os> ] in windows cygwin darwin aix &&
! [ $(property-set).get <toolset> ] in pgi
{
result = $(result).$(BOOST_VERSION) ;
}

tools/build/src/tools/python.jam中,注释掉pthread库的链接(否则编译Boost.Python时会提示找到不到-lpthread):

case * : return  <library>dl
<toolset>gcc:<library>util <toolset-intel:platform>linux:<library>util ;

编辑 project-config.jam 文件

using clang : : D:/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe :
<compileflags>--target=aarch64-linux-android29
<compileflags>-fPIC
<compileflags>-frtti
<compileflags>-fexceptions
<linkflags>-target
<linkflags>aarch64-linux-android29
<archiver>D:/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/windows-x86_64/bin/llvm-ar.exe
;

using python
: 3.11
: /d/Android/python-3.11.3
: /d/Android/python-3.11.3/include/python3.11
: /d/Android/python-3.11.3/lib
:
;

然后在命令行(MSYS2)中输入:

./b2.exe  install --prefix=/d/Android/boost_1_84_0 toolset=clang cxxstd=11 link=shared threading=multi runtime-link=shared --build-dir=build/ndk target-os=android binary-format=elf address-model=64 architecture=arm abi=aapcs define=BOOST_FILESYSTEM_DISABLE_STATX=1 --without-python -q

Windows MSVC编译

进入如下所示命令行。

执行以下命令:

.\bootstrap.bat
.\b2.exe -q install --prefix=E:\Projects\Libraries\boost_1_86_0_msvc2022_64bit link=shared threading=multi runtime-link=shared variant=debug,release address-model=64 cxxstd=17 define=BOOST_USE_WINAPI_VERSION=BOOST_WINAPI_VERSION_WIN10

boost_1_83_0 是最后一个兼容 Windows7 的版本,如果需要在 Visual Studio 2022 上成功编译,需要修改文件:(搜索 if [ MATCH "(14.3)" : $(version) ],然后对比加入下面的内容)

tools/build/src/tools/msvc.jam
if [ MATCH "(14.4)" : $(version) ]
{
if $(.debug-configuration)
{
ECHO "notice: [generate-setup-cmd] $(version) is 14.4x" ;
}
parent = [ path.native [ path.join $(parent) "..\\..\\..\\..\\..\\Auxiliary\\Build" ] ] ;
}
else if [ MATCH "(14.3)" : $(version) ]
{
if $(.debug-configuration)
{
ECHO "notice: [generate-setup-cmd] $(version) is 14.3" ;
}
parent = [ path.native [ path.join $(parent) "..\\..\\..\\..\\..\\Auxiliary\\Build" ] ] ;
}

OpenSSL

下载源码。

./Configure linux-aarch64 --cross-compile-prefix=aarch64-v01c01-linux-gnu- --prefix=/opt/aarch64-v01c01-linux-gnu-gcc/lib/openssl-1.1.1w

./Configure linux-armv4 --cross-compile-prefix=arm-linux-gnueabihf- --prefix=/opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/lib/openssl-3.3.1 no-tests no-example

make && make install

FFmpeg

参考这里

NNG

mkdir build
cd build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/opt/Libraries/nng-1.6.0_debug ..
ninja
ninja install

libdatachannel

toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)

set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_C_COMPILER   aarch64-v01c01-linux-gnu-gcc)

set(CMAKE_CXX_COMPILER aarch64-v01c01-linux-gnu-g++)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)

set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake \
-DNO_TESTS=ON \
-DNO_EXAMPLES=ON \
-DOPENSSL_ROOT_DIR=/opt/aarch64-v01c01-linux-gnu-gcc/lib/openssl-1.1.1w \
-DOPENSSL_CRYPTO_LIBRARY=/opt/aarch64-v01c01-linux-gnu-gcc/lib/openssl-1.1.1w/lib/libcrypto.so \
-DOPENSSL_SSL_LIBRARY=/opt/aarch64-v01c01-linux-gnu-gcc/lib/openssl-1.1.1w/lib/libssl.so \
-DCMAKE_INSTALL_PREFIX=/opt/aarch64-v01c01-linux-gnu-gcc/lib/libdatachannel-0.19.5 \
-DCMAKE_BUILD_TYPE=Debug ..

VTK

VTK_GROUP_ENABLE_Qt YES
VTK_MODULE_ENABLE_VTK_GUISupportQt YES
VTK_MODULE_ENABLE_VTK_GUISupportQtQuick YES
VTK_MODULE_ENABLE_VTK_GUISupportQtSQL YES
VTK_MODULE_ENABLE_VTK_RenderingQt YES
VTK_MODULE_ENABLE_VTK_ViewsQt YES
VTK_QT_VERSION 6
VTK_MODULE_ENABLE_VTK_RenderingExternal YES

mongo c

sudo apt install libssl-dev

mongocxx

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


if(CMAKE_BUILD_TYPE STREQUAL "Release")
set(libbson-1.0_DIR /opt/Libraries/mongo-c-driver-1.17.5_release/lib/cmake/libbson-1.0)
set(libmongoc-1.0_DIR /opt/Libraries/mongo-c-driver-1.17.5_release/lib/cmake/libmongoc-1.0)
set(libbson-static-1.0_DIR /opt/Libraries/mongo-c-driver-1.17.5_release/lib/cmake/libbson-static-1.0)
set(libmongoc-static-1.0_DIR /opt/Libraries/mongo-c-driver-1.17.5_release/lib/cmake/libmongoc-static-1.0)
else()
set(libbson-1.0_DIR /opt/Libraries/mongo-c-driver-1.17.5_debug/lib/cmake/libbson-1.0)
set(libmongoc-1.0_DIR /opt/Libraries/mongo-c-driver-1.17.5_debug/lib/cmake/libmongoc-1.0)
set(libbson-static-1.0_DIR /opt/Libraries/mongo-c-driver-1.17.5_debug/lib/cmake/libbson-static-1.0)
set(libmongoc-static-1.0_DIR /opt/Libraries/mongo-c-driver-1.17.5_debug/lib/cmake/libmongoc-static-1.0)
endif()

include_directories(
/opt/Libraries/mongo-c-driver-1.17.5_debug/include/libmongoc-1.0
/opt/Libraries/mongo-c-driver-1.17.5_debug/include/libbson-1.0
)

如果编译不过:

CMAKE_CXX_FLAGS="/Zc:__cplusplus"

PCL

使用vcpkg安装flann:

.\vcpkg\vcpkg.exe install flann:x64-windows

设置CMake变量(Qt Creator在Initial Configuration中添加):

CMAKE_TOOLCHAIN_FILE [path to vcpkg]/scripts/buildsystems/vcpkg.cmake
PCL_BUILD_WITH_BOOST_DYNAMIC_LINKING_WIN32 ON
BUILD_visualization ON
BUILD_tools OFF #如果编译失败的话,或者想减少编译时长

在CMakeLists.txt中添加:

set(Libraries_ROOT E:/Projects/Libraries)
set(BOOST_ROOT ${Libraries_ROOT}/boost_1_82_0_msvc2022_64bit)
set(EIGEN_INCLUDE_DIR ${Libraries_ROOT}/eigen-3.4.0/include/eigen3)
if(CMAKE_BUILD_TYPE STREQUAL "Release")
set(VTK_DIR ${Libraries_ROOT}/VTK-9.2.6_msvc2022_64bit_release/lib/cmake/vtk-9.2)
else()
set(VTK_DIR ${Libraries_ROOT}/VTK-9.2.6_msvc2022_64bit_debug/lib/cmake/vtk-9.2)
endif()

编译应用程序时,提示如下错误时

KittiVisualizer.cpp.obj:-1: error: LNK2019: 无法解析的外部符号 "void __cdecl vtkRenderingGL2PSOpenGL2_AutoInit_Construct(void)" (?vtkRenderingGL2PSOpenGL2_AutoInit_Construct@@YAXXZ),该符号在函数 "public: __cdecl vtkRenderingOpenGL2_AutoInit::vtkRenderingOpenGL2_AutoInit(void)" (??0vtkRenderingOpenGL2_AutoInit@@QEAA@XZ) 中被引用

KittiVisualizer.cpp.obj:-1: error: LNK2019: 无法解析的外部符号 "void __cdecl vtkRenderingGL2PSOpenGL2_AutoInit_Destruct(void)" (?vtkRenderingGL2PSOpenGL2_AutoInit_Destruct@@YAXXZ),该符号在函数 "public: __cdecl vtkRenderingOpenGL2_AutoInit::~vtkRenderingOpenGL2_AutoInit(void)" (??1vtkRenderingOpenGL2_AutoInit@@QEAA@XZ) 中被引用

将相应库加入其中

target_link_libraries(KittiVisualizer  PRIVATE
Qt5::Gui
Qt5::Widgets
${VTK_LIBRARIES}
${PCL_LIBRARIES}
${Boost_LIBRARIES}
${OPENGL_LIBRARIES}
vtkRenderingGL2PSOpenGL2-8.2
)

OpenCV

set(Board_HOME /home/$ENV{USER}/nvidia)
set(CMAKE_SYSROOT ${Board_HOME}/sysroot)
set(CMAKE_SYSROOT_LINK ${Board_HOME}/sysroot)

link_libraries(
png
pcre
harfbuzz
z
icui18n
icuuc
icudata
gthread-2.0
glib-2.0
GLESv2
GLdispatch
freetype
graphite2
)

找到3rdparty/libpng/pngpriv.h文件,第129行将

# if (defined(ARM_NEON) || defined(__ARM_NEON)) && \

修改为

# if defined(PNG_ARM_NEON) && (defined(ARM_NEON) || defined(__ARM_NEON)) && \

然后在选项卡里设置如下:

BUILD_SHARED_LIBS OFF
BUILD_TESTS OFF
BUILD_opencv_world ON
WITH_GTK OFF
WITH_VTK OFF
WITH_QT ON
CMAKE_INSTALL_PREFIX /home/optimus/nvidia/lib/opencv-4.5.0

libyuv

git clone https://github.com/lemenkov/libyuv.git
cd libyuv
cmake -S $(pwd) -B $(pwd)/build -DCMAKE_TOOLCHAIN_FILE=resources/cmake/toolchain.cmake -DCMAKE_INSTALL_PREFIX=/opt/toolchains/Libraries/libyuv
cd build
make
make install

speex

./configure -prefix=/opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/lib/speexdsp-1.2.1 -enable-static -disable-shared --build=arm-linux-gnueabihf --host=arm-linux CC=arm-linux-gnueabihf-gcc

libjpeg-turbo

cmake -G Ninja -B build -S . -DCMAKE_BUILD_TYPE=Debug -DCMAKE_POSITION_INDEPENDENT_CODE=true -DCMAKE_INSTALL_PREFIX="E:\Projects\Libraries\libjpeg-turbo-3.0.3_msvc2022_64bit_debug" 
cmake --build build --target all
cmake --install build

Wt

set(Projects_ROOT E:/Projects)
set(Libraries_ROOT ${Projects_ROOT}/Libraries)
set(BOOST_ROOT ${Libraries_ROOT}/boost_1_80_0_msvc2022_64bit)

set(OPENSSL_ROOT_DIR D:/Qt/Tools/OpenSSL/Win_x64)
set(OPENSSL_INCLUDE_DIR ${OPENSSL_ROOT_DIR}/include)
set(OPENSSL_LIBRARIES
${OPENSSL_ROOT_DIR}/lib/libcrypto.lib
${OPENSSL_ROOT_DIR}/lib/libssl.lib
)

OPTION(BOOST_DYNAMIC "Link to boost DLLs (OFF means static link)" ON)


# set(OPENSSL_ROOT_DIR ${SSL_PREFIX})
# find_package(OpenSSL)


# IF(ENABLE_SSL AND OPENSSL_FOUND)
SET(HAVE_SSL ON)
SET(WT_WITH_SSL true)
# ENDIF(ENABLE_SSL AND OPENSSL_FOUND)

jenkins

FROM registry.cn-shenzhen.aliyuncs.com/amass_toolset/jl_develop:latest
LABEL maintainer 168062547@qq.com

ARG user=jenkins
ARG group=jenkins
ARG uid=1000
ARG gid=1000
# ARG http_port=8080
# ARG agent_port=50000

ENV JENKINS_HOME /var/jenkins_home
ENV JENKINS_SLAVE_AGENT_PORT ${agent_port}


RUN groupadd -g ${gid} ${group} \
&& useradd -d "$JENKINS_HOME" -u ${uid} -g ${gid} -m -s /bin/bash ${user}
RUN chown -R ${user} "$JENKINS_HOME"

RUN curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
RUN echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null

RUN mkdir -p /etc/apt/keyrings
RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null

RUN apt-get update
RUN apt-get install -y jenkins openjdk-11-jre docker-ce-cli

ENV TINI_VERSION 0.14.0
ENV TINI_SHA 6c41ec7d33e857d4779f14d9c74924cab0c7973485d2972419a3b7c7620ff5fd

# Use tini as subreaper in Docker container to adopt zombie processes
RUN curl -fsSL https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini-static-amd64 -o /bin/tini && chmod +x /bin/tini \
&& echo "$TINI_SHA /bin/tini" | sha256sum -c -


VOLUME /var/jenkins_home
USER ${user}
ENTRYPOINT ["/bin/tini", "--", "jenkins"]