codeblocks如何建同一source下cpp_如何在Linux上构建发布包含.so动态库的snap安装包

Linux上要发布源码包比较容易,只要做个压缩包.tar.gz就可以。但是如果要做一个包含库和可执行文件的安装包,就比较麻烦。Linux的版本很多,需要做不同格式的安装包,比如.deb和.rpm,同时还需要做很多测试。现在有了Snap这个包管理系统,就会方便很多。Snap支持所有主流的Linux系统。

1b6d3da36ed962277b13a6fee2fbc1a7.png

安装Snap和Snapcraft

按照官方指南进行安装。

这里是Debian安装的步骤:

$ sudo apt update
$ sudo apt install snapd
$ sudo snap install snapcraft –classic

注意,WSL是不支持snapcraft安装的:

Interacting with snapd is not yet supported on Windows Subsystem for Linux.
This command has been left available for documentation purposes only.

我用了Ubuntu 20.04。安装后可以装一个hello world测试下:

$ sudo snap install hello-world
hello-world 6.4 from Canonical✓ installed
$ hello-world
Hello World!

使用Snapcraft构建发布Snap安装包

生成一个初始工程:

$ snapcraft init
Created snap/snapcraft.yaml.

接下来的示例我会用到Dynamsoft Barcode Reader

下载 Dynamsoft Barcode Reader for Linux。从这个压缩包里取出DynamsoftBarcodeReader.h, libDynamsoftBarcodeReader.so,以及示例代码ReadBarcode.cpp

创建Makefile文件:

CC=gcc
CCFLAGS=-c
 
LDFLAGS=-L ./lib
DBRLIB=-lDynamsoftBarcodeReader
 
STDLIB=-lstdc++
 
TARGET=barcode-reader
OBJECT=ReadBarcode.o
SOURCE=ReadBarcode.cpp
 
# build rule for target.
$(TARGET): $(OBJECT)
    $(CC) -o $(TARGET) $(OBJECT) $(STDLIB) $(DBRLIB) $(LDFLAGS)
 
# target to build an object file
$(OBJECT): $(SOURCE)
    $(CC) $(CCFLAGS) $(SOURCE)
 
install:
    mkdir -p $(DESTDIR)/usr/bin/
    cp ./barcode-reader $(DESTDIR)/usr/bin/
    mkdir -p $(DESTDIR)/usr/lib
    cp ./lib/libDynamsoftBarcodeReader.so $(DESTDIR)/usr/lib
 
# the clean target
.PHONY : clean
clean: 
    rm -f $(OBJECT) $(TARGET)

这里面最重要的是install。要把库文件和生成的可执行文件拷贝到对应的目录里。

编辑snapcraft.yaml文件:

name: barcode-reader 
base: core18 
version: '7.4' 
summary: Barcode SDK for 1D barcode, QR Code, Data Matrix, PDF417, Aztec Code, MaxiCode 
description: |
  An enterprise-class barcode SDK that enables you to efficiently embed barcode reading functionality in your web, desktop or mobile applications with a few lines of code.
icon: snap/gui/logo.png
grade: stable 
confinement: strict  
 
parts:
  barcode-reader:
    plugin: make
    build-packages: [gcc, g++, make]
    stage-packages: [libstdc++6]
    source: src
 
apps:
  barcode-reader:
    command: barcode-reader
    plugs: [home]

Snap安装包是在沙盒环境里运行的,所以需要指定权限。默认情况是不能读取系统里的文件的。添加home插件之后就可以从$HOME路径下读取文件。

编译安装包:

snapcraft

在本地安装进行测试:

sudo snap install --dangerous barcode-reader_7.4_amd64.snap

执行程序

barcode-reader ~/images/AllSupportedBarcodeTypes.tif 

4e194be8c3cec88e3b36b0e6678dfc1b.png

确认没有问题后,把安装包发布到Snap商店:

$ snapcraft login
$ snapcraft push --release=stable barcode-reader_7.4_amd64.snap

这是我的发布页面:https://snapcraft.io/barcode-reader.

最后,通过在线编辑美化页面。

1020115e59501b9d7ced6a55fa9b4385.png

如何利用Snap包中的库文件创建Linux应用

安装Snap包:

$ sudo snap install barcode-reader

导出需要用的动态链接库:

$ export LD_LIBRARY_PATH=/snap/barcode-reader/current/usr/lib/

编译运行程序:

$ gcc -o app <source.cpp> -L /snap/barcode-reader/current/usr/lib/  -lDynamsoftBarcodeReader -lstdc++ 
$ ./app

也可以使用相对路径直接编译运行程序:

$ gcc -o app <source.cpp> -L /snap/barcode-reader/current/usr/lib/  -lDynamsoftBarcodeReader -lstdc++ -Wl,-rpath=/snap/barcode-reader/current/usr/lib/
$ ./app

源码

https://github.com/yushulx/snap-package

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值