使用CMake可以很方便的实现跨平台编译。如果要Link一个第三方库,需要针对平台进行设置。这里分享下如何创建一个简单的CMake工程实现Windows, Linux和macOS上的自动编译。
SDK下载
CMake下载安装
Windows
Linux
sudo apt-get install cmake
macOS
brew install cmake
注意不要在brew前面加sudo。最新的brew不再支持,用了会发生错误:
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
工程结构
project
|-- platforms
|-- win
|-- DBRx86.lib
|-- DynamsoftBarcodeReaderx86.dll
|-- linux
|-- libDynamsoftBarcodeReader.so
|-- macos
|-- libDynamsoftBarcodeReader.dylib
|-- include
|-- DynamsoftBarcodeReader.h
|-- BarcodeReader.cxx
|-- BarcodeReaderConfig.h.in
|-- CMakeLists.txt
C++代码
写了一个简单的命令行barcode reader。这里的重点是如何配置CMake,代码不解释。
可以浏览BarcodeReader.cxx
CMakeLists.txt
平台区分
if (CMAKE_HOST_WIN32)
set(WINDOWS 1)
elseif(CMAKE_HOST_APPLE)
set(MACOS 1)
elseif(CMAKE_HOST_UNIX)
set(LINUX 1)
endif()
设置动态链接库路径
if(WINDOWS)
link_directories("${PROJECT_SOURCE_DIR}/platforms/win")
elseif(