From 7bb1bc8896109a5927a362eb2aa9a9e1fc8cf04c Mon Sep 17 00:00:00 2001 From: sz Date: Thu, 17 Feb 2022 22:53:35 -0600 Subject: [PATCH] -DBUILD_PORTABLE_LINUX for static linking against opencv and libc++ This gets us back to glibc 2.27 (ubuntu 18.04) -- debian stretch would be nicer, but the default build tools (c++1x, cmake) gets slightly more interesting. The output binaries seem like they could be smaller... --- CMakeLists.txt | 11 ++++++++++- package-portable-linux.sh | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 package-portable-linux.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 7264092..2b3d603 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,8 +33,17 @@ else() # if not wasm, go find opencv. 3 or 4 should both work include_directories(${OpenCV_INCLUDE_DIRS}) endif() +if(DEFINED BUILD_PORTABLE_LINUX) + find_package(PkgConfig REQUIRED) + pkg_check_modules(OPENCV4 REQUIRED opencv4) + link_directories(${OPENCV4_STATIC_LIBRARY_DIRS}) + + # statically link c++/gcc (not libc!!) + set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -static-libstdc++ -static-libgcc") +endif() + if(NOT DEFINED OPENCV_LIBS) - set(OPENCV_LIBS "opencv_calib3d" "opencv_imgcodecs" "opencv_imgproc" "opencv_photo" "opencv_core") + set(OPENCV_LIBS "opencv_calib3d" "opencv_imgcodecs" "opencv_imgproc" "opencv_photo" "opencv_core" ${OPENCV4_STATIC_LIBRARIES}) endif() if(NOT DEFINED CPPFILESYSTEM) diff --git a/package-portable-linux.sh b/package-portable-linux.sh new file mode 100644 index 0000000..ab56d35 --- /dev/null +++ b/package-portable-linux.sh @@ -0,0 +1,17 @@ +#!/bin/sh +# docker run --mount type=bind,source="$(pwd)",target="/usr/src/app" -it ubuntu:18.04 + +cd /usr/src/app +apt update +apt install -y pkgconf g++ cmake +apt install -y libgles2-mesa-dev libglfw3-dev + +cd opencv4/ +mkdir build-portable/ && cd build-portable/ +cmake .. -DCMAKE_BUILD_TYPE=RELEASE -DBUILD_SHARED_LIBS=OFF -DOPENCV_GENERATE_PKGCONFIG=YES -DOPENCV_FORCE_3RDPARTY_BUILD=YES +make -j5 install + +cd /usr/src/app +mkdir build-portable/ && cd build-portable/ +cmake .. -DBUILD_PORTABLE_LINUX=1 +make -j5 install