博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CMake与动态链接库(dll, so, dylib)
阅读量:5971 次
发布时间:2019-06-19

本文共 5757 字,大约阅读时间需要 19 分钟。

hot3.png

使用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 allbuild 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,代码不解释。 可以浏览

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(LINUX)    link_directories("${PROJECT_SOURCE_DIR}/platforms/linux") elseif(MACOS)    link_directories("${PROJECT_SOURCE_DIR}/platforms/macos") endif()

Windows上拷贝dll文件到输出目录

if(WINDOWS)    # Copy DLL files to output directory    if(CMAKE_CL_64)        add_custom_command(TARGET BarcodeReader POST_BUILD         COMMAND ${CMAKE_COMMAND} -E copy_if_different        "${PROJECT_SOURCE_DIR}/platforms/win/DynamsoftBarcodeReaderx64.dll"                      $
) else() add_custom_command(TARGET BarcodeReader POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${PROJECT_SOURCE_DIR}/platforms/win/DynamsoftBarcodeReaderx86.dll" $
) endif()endif()

设置安装路径

if(WINDOWS)    set(CMAKE_INSTALL_PREFIX "e:/${PROJECT_NAME}")    if(CMAKE_CL_64)    install (FILES "${PROJECT_SOURCE_DIR}/platforms/win/DynamsoftBarcodeReaderx64.dll" DESTINATION bin)    else()    install (FILES "${PROJECT_SOURCE_DIR}/platforms/win/DynamsoftBarcodeReaderx86.dll" DESTINATION bin)    endif()elseif(LINUX)    install (FILES "${PROJECT_SOURCE_DIR}/platforms/linux/libDynamsoftBarcodeReader.so" DESTINATION lib)elseif(MACOS)    install (FILES "${PROJECT_SOURCE_DIR}/platforms/macos/libDynamsoftBarcodeReader.dylib" DESTINATION lib)endif()

在Windows上默认会安装到C盘,如果命令行工具没有管理员权限会安装失败。所以可以更改默认安装路径。

设置RPATH

# Set RPATHif(WINDOWS)else()    set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")    MESSAGE( STATUS "CMAKE_INSTALL_RPATH: " "${CMAKE_INSTALL_PREFIX}/lib" )    set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)endif()

Linux和macOS上的默认路径是/usr/local。安装之后执行程序会找不到动态链接库。设置了RPATH之后就不会有问题了。

编译安装工程

生成工程文件:

mkdir buildcd buildcmake ..

Windows上默认x86。如果要用x64,需要指定generator:

cmake -G"Visual Studio 14 2015 Win64" ..

编译安装:

cmake --build . --target install

在Linux上使用命令的时候前面要加上sudo

完整CMakeLists.txt

cmake_minimum_required (VERSION 2.6)project (BarcodeReader)MESSAGE( STATUS "PROJECT_NAME: " ${PROJECT_NAME} )# The version number.set(BarcodeReader_VERSION_MAJOR 1)set(BarcodeReader_VERSION_MINOR 0)# Check platformsif (CMAKE_HOST_WIN32)    set(WINDOWS 1)elseif(CMAKE_HOST_APPLE)    set(MACOS 1)elseif(CMAKE_HOST_UNIX)    set(LINUX 1)endif()# Set RPATHif(WINDOWS)else()    set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")    MESSAGE( STATUS "CMAKE_INSTALL_RPATH: " "${CMAKE_INSTALL_PREFIX}/lib" )    set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)endif()# Configure a header file to pass some of the CMake settings# to the source codeconfigure_file (    "${PROJECT_SOURCE_DIR}/BarcodeReaderConfig.h.in"    "${PROJECT_BINARY_DIR}/BarcodeReaderConfig.h")# Add search path for include and lib filesif(WINDOWS)    link_directories("${PROJECT_SOURCE_DIR}/platforms/win") elseif(LINUX)    link_directories("${PROJECT_SOURCE_DIR}/platforms/linux") elseif(MACOS)    link_directories("${PROJECT_SOURCE_DIR}/platforms/macos") endif()include_directories("${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/include")# Add the executableadd_executable(BarcodeReader BarcodeReader.cxx)if(WINDOWS)    if(CMAKE_CL_64)        target_link_libraries (BarcodeReader "DBRx64")    else()        target_link_libraries (BarcodeReader "DBRx86")    endif()else()    target_link_libraries (BarcodeReader "DynamsoftBarcodeReader")endif()if(WINDOWS)    # Copy DLL files to output directory    if(CMAKE_CL_64)        add_custom_command(TARGET BarcodeReader POST_BUILD         COMMAND ${CMAKE_COMMAND} -E copy_if_different        "${PROJECT_SOURCE_DIR}/platforms/win/DynamsoftBarcodeReaderx64.dll"                      $
) else() add_custom_command(TARGET BarcodeReader POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${PROJECT_SOURCE_DIR}/platforms/win/DynamsoftBarcodeReaderx86.dll" $
) endif()endif()# Set installation directoryif(WINDOWS) set(CMAKE_INSTALL_PREFIX "e:/${PROJECT_NAME}") if(CMAKE_CL_64) install (FILES "${PROJECT_SOURCE_DIR}/platforms/win/DynamsoftBarcodeReaderx64.dll" DESTINATION bin) else() install (FILES "${PROJECT_SOURCE_DIR}/platforms/win/DynamsoftBarcodeReaderx86.dll" DESTINATION bin) endif()elseif(LINUX) install (FILES "${PROJECT_SOURCE_DIR}/platforms/linux/libDynamsoftBarcodeReader.so" DESTINATION lib)elseif(MACOS) install (FILES "${PROJECT_SOURCE_DIR}/platforms/macos/libDynamsoftBarcodeReader.dylib" DESTINATION lib)endif()install (TARGETS BarcodeReader DESTINATION bin)install (FILES "${PROJECT_BINARY_DIR}/BarcodeReaderConfig.h" DESTINATION include)install (DIRECTORY "${PROJECT_SOURCE_DIR}/include" DESTINATION include)# Use CTestinclude(CTest)add_test (BarcodeReaderRuns BarcodeReader)

源码

转载于:https://my.oschina.net/yushulx/blog/1573312

你可能感兴趣的文章
从Win32过渡到MFC
查看>>
基础才是重中之重~如何整理BLL与DAL层的文件
查看>>
地理信息系统专业考研 GIS专业考研 名词解释大全[转]
查看>>
2013第10周六项目中用到的前端技术学习1
查看>>
pig grunt shell详解
查看>>
Eclipse and My Eclipse 快捷键
查看>>
Standard System Parameter
查看>>
vs2010驱动开发环境配置
查看>>
javascript练习:8-2对象的引用
查看>>
《算法精解:C语言描述》勘误
查看>>
【转载】解决Ubuntu 12.04重启清空/etc/resolv.conf里nameserver的设置
查看>>
让整个网页(LOGO图片)色调全部变灰的方法(CSS写法)
查看>>
博客新窝CSDN站
查看>>
微软正式发布了Microsoft.Bcl.Async
查看>>
方法定制iOS学习笔记8-UITableView的定制
查看>>
hdu 1571(模拟)
查看>>
CF 55D Beautiful numbers(数位DP)
查看>>
Console-算法[for,if]-一打印出如下图案(菱形)
查看>>
Sharepoint学习笔记—ECM系列—Content Type Syndication
查看>>
HDU-4462 Scaring the Birds 状态压缩枚举
查看>>