CSerialPort教程4.3.x (6) - CSerialPort作为第三方库的使用

CSerialPort教程4.3.x (6) - CSerialPort作为第三方库的使用

环境:

系统:windows 10/CentOS 7
cmake: 3.22.1

前言

CSerialPort项目是一个基于C/C++的轻量级开源跨平台串口类库,可以轻松实现跨平台多操作系统的串口读写,同时还支持C#, Java, Python, Node.js等。

CSerialPort项目的开源协议自 V3.0.0.171216 版本后采用GNU Lesser General Public License v3.0

为了让开发者更好的使用CSerialPort进行开发,特编写基于4.3.x版本的CSerialPort教程系列。

CSerialPort项目地址:

注意事项:

  • x64动态库只能用于x64的程序调用,x86同理
  • debug的动态库只能用于动态库程序调用,release同理

1. 使用cmake生成CSerialPort动态库

  • x64
  • 动态库 静态库
  • debug release
$ git clone https://github.com/itas109/CSerialPort
$ cd CSerialPort
$ mkdir bin
$ cd bin
$ cmake .. -DCMAKE_INSTALL_PREFIX=install -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=ON
$ cmake --build . --config Debug
$ cmake --install . --config Debug

CSerialPort安装目录结构:

$ tree
.
├── include
│   └── CSerialPort
│       ├── SerialPort_global.h
│       ├── SerialPort.h
│       ├── SerialPortInfo.h
│       ├── SerialPortListener.h
│       └── SerialPort_version.h
└── lib
    ├── cmake
    │   └── CSerialPort
    │       └── cserialport-config.cmake
    └── libcserialport.so

2. 以cmake方式引用CSerialPort的动态库【推荐】

注意: 
cmake方式同样可用于控制台项目、MFC项目和QT项目等。

2.1 通过find_package自动搜索CSerialPort头文件及动态库【推荐】

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)

project(CommConsole LANGUAGES CXX)

find_package(CSerialPort)
if (CSerialPort_FOUND)
	include_directories(${CSerialPort_INCLUDE_DIR})
	add_executable( ${PROJECT_NAME} main.cpp)
	target_link_libraries (${PROJECT_NAME} ${CSerialPort_LIBRARY})
else()
	message(STATUS "Not found system CSerialPort")
endif ()

注意:
出现如下错误,可设置CMAKE_PREFIX_PATH指定搜索路径,如cmake .. -DCMAKE_PREFIX_PATH="D:/CommConsole/CSerialPort/bin/install"

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

    CSerialPortConfig.cmake
    cserialport-config.cmake

2.2 手动指定头文件及动态库

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)

project(CommConsole LANGUAGES CXX)

set(CSerialPortRootPath "${PROJECT_SOURCE_DIR}/CSerialPort/bin/install")
include_directories(${CSerialPortRootPath}/include)
link_directories(${CSerialPortRootPath}/lib)

add_executable(${PROJECT_NAME}
    main.cpp
)

if (WIN32)
	target_link_libraries( ${PROJECT_NAME} libcserialport)
elseif(UNIX)
	target_link_libraries( ${PROJECT_NAME} cserialport)
endif ()

注意:
CSerialPortRootPath表示CSerialPort安装目录

3. QT中使用CSerialPort的动态库

注意:
QT的cmake方式与第二节类似

CommQT.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# add by itas109
# 1. headers
INCLUDEPATH += "$$PWD/CSerialPort/bin/install/include"

# 2. add cserialport libs
win32:LIBS += "-L$$PWD/CSerialPort/bin/install/lib"  -lcserialport
unix:LIBS += "-L$$PWD/CSerialPort/bin/install/lib" -lcserialport

# 3. define UNICODE
DEFINES += _UNICODE
# end by itas109

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

4. MFC中使用CSerialPort的动态库

注意:
MFC的cmake方式与第三节类似

4.1 添加头文件路径

右键【CommMFC根命名空间】-【属性】-【C/C++】-【常规】-【附加包含目录】-添加CSerialPort的头文件目录

D:\CommMFC\CSerialPort\bin\install\include

$(ProjectDir)\..\CSerialPort\bin\install\include

4.2 添加库文件路径

  • 添加库文件目录

右键【CommMFC根命名空间】-【属性】-【链接器】-【常规】-【附加库目录】-添加CSerialPort的库文件目录

D:\CommMFC\CSerialPort\bin\install\lib

$(ProjectDir)\..\CSerialPort\bin\install\lib
  • 添加库附加依赖项

右键【CommMFC根命名空间】-【属性】-【链接器】-【输入】-【附加依赖项】-添加libcserialport.lib


License

License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎


Reference:

  1. https://github.com/itas109/CSerialPort
  2. https://gitee.com/itas109/CSerialPort
  3. https://blog.csdn.net/itas109

猜你喜欢

转载自blog.csdn.net/itas109/article/details/132389591