Code Monkey home page Code Monkey logo

modern-cmake-practice's Introduction

[TOC]

现代CMake实践

环境要求

  • CMake 3.15
  • Visual Studio 2017及以上

目录结构

目录 说明
external 第三方库,采用cmake的FetchContent模块处理第三方库下载及配置
examples 示例程序

模板

第三方库

提供CMake配置的二进制包(以Qt为例)

Qt虽然目前不是以CMake构建的,但是提供了CMake使用支持,在lib\cmake目录下,由此可以通过find_package使用.这里需要处理以下两种情况:

  1. 使用安装包安装Qt并配置好环境变量QTDIR
  2. Qt二进制包打包成zip存储于服务器某位置

这里采用的方式是首先判断环境变量中是否有QTDIR,如果有则直接配置,如果没有则使用FetchContent模块下载解压二进制包,配置好QTDIR:

#如果在环境变量中找不到QTDIR则下载
if(NOT DEFINED ENV{QTDIR})
    message(STATUS "PATH = $ENV{PATH}")    
    FetchContent_Declare(
        qt 
        URL "ftp://yourhost/qt5.12.9.zip"
        URL_HASH SHA512=2a5655420c7b5a9300a2d973029abada7654ccc9de77e31adfd2670d2818e006bf85ccb619231aa392061162869c70e29df6c0a32fc2815944eb10ce5fde0664
        DOWNLOAD_NAME "qt5.12.9.zip"
        DOWNLOAD_DIR ${CMAKE_SOURCE_DIR}/external/downloads/qt  
    )
    FetchContent_GetProperties(qt)
    if(NOT qt_POPULATED)
        #下载并解压Qt二进制包
        FetchContent_Populate(qt)

        #确定当前使用的QT路径
        if(CMAKE_SIZEOF_VOID_P EQUAL 8)
            set(__QTDIR ${qt_SOURCE_DIR}/msvc2017_64)
        else()
            set(__QTDIR ${qt_SOURCE_DIR}/msvc2017)
        endif()
    endif()
else()
    message(STATUS "QTDIR = $ENV{QTDIR}")
    set(__QTDIR  $ENV{QTDIR})
endif()

#设置QTDIR为全局变量
set(QTDIR ${__QTDIR} CACHE INTERNAL "Qt Install Directory")

#追加到CMAKE_PREFIX_PATH确保find_package能够找到
list(APPEND CMAKE_PREFIX_PATH ${QTDIR}) 

通过在CMakeLists.txt中通使用add_subdirectory加载external\qt下的CMakeLists.txt即可使用Qt,例如examples中的QHelloWorld示例:

add_executable(QHelloWorld)
target_sources(QHelloWorld
    PRIVATE QHelloWorld.cpp
)

##找到Qt库并链接
find_package(Qt5 COMPONENTS Widgets REQUIRED)
target_link_libraries(QHelloWorld
    PRIVATE Qt5::Widgets
)

使用CMake构建的开源库

SDK构建

其它

如何制作Qt离线二进制压缩包

本地安装完Qt后将安装目录直接压缩即可,目前Qt离线安装包均需账户登录,可以通过设置代理地址为0.0.0.0:123来跳过.

modern-cmake-practice's People

Contributors

liff-engineer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

rpzhu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.