cmake_examples

基本知识:

include(${CMAKE_SOURCE_DIR}/cmake/cmaketools.cmake)

include类似于C语言的include 展开

子工程add_subdirectory 比好用 include 

 

静态库:

//源文件

aux_source_directory(. DATASTREAMING_SRC)

//静态库可执行文件到工程中

add_library(${DATASTREAM_LIB_NAME} STATIC ${DATASTREAMING_SRC})

//头文件

target_include_directories(${DATASTREAM_LIB_NAME} PRIVATE ${ALL_INCLUDE_DIRS})

 

 

可执行文件:

set(TARGETS TnMapDataAccessRegex)

//依赖库路径声明在executable之前
link_directories(
    ${PROJECT_SOURCE_DIR}/../../lib/server
    ${PROJECT_SOURCE_DIR}/../../lib/someip
)

 //源文件

aux_source_directory(${CMAKE_SOURCE_DIR}/regex REGEX_SOURCE_FILES)

aux_source_directory(${CMAKE_SOURCE_DIR}/regex/dumper REGEX_DUNPER_SOURCE_FILES)

//添加可执行文件到工程中

add_executable( ${TARGETS}

                ${REGEX_SOURCE_FILES}

                ${REGEX_DUNPER_SOURCE_FILES}

)

//链接库,依赖顺序参照gcc     

target_link_libraries(${TARGETS}

    ${MAP_DATA_ACCESS_LIB_NAME}

    ${MAP_DATA_ACCESS_DEPEND_LIBS}

)

 //头文件目录

target_include_directories(${TARGETS}

                            PRIVATE

                            ${ALL_INCLUDE_DIRS}

                            )

//定义宏

target_compile_definitions(${TARGETS} PRIVATE ${MAP_DATA_ACCESS_COMMON_DEFINITION})

 

 

交叉编译:

    # aarch64
    if(COMPILE_AARCH64)
        set(CMAKE_SYSTEM_NAME Linux)
        
        set(TOOLCHAIN_PATH "/opt/poky/2.4.3/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux")
        set(CMAKE_C_COMPILER ${TOOLCHAIN_PATH}/aarch64-poky-linux-gcc)
        set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PATH}/aarch64-poky-linux-g++)
        set(CMAKE_AR ${TOOLCHAIN_PATH}/aarch64-poky-linux-ar)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --sysroot=/opt/poky/2.4.3/sysroots/aarch64-poky-linux -Wall -Wno-unused-but-set-variable -Werror")
        
        include_directories("/opt/poky/2.4.3/sysroots/aarch64-poky-linux/usr/include")

        link_directories(
            #"${KANZIDIR}/Engine/lib/linux_wsegl_aarch64_gcc72/ES3_Release"
            #"${KANZIDIR}/Engine/libraries/platforms/linux-aarch64/libunibreak/lib"
        )

    #linux x86_64
    else()
        #link_directories("${KANZIDIR}/Engine/lib/linux_x11_glx_cpp98/GL_Debug")
    endif()

 

 链接动态库

1.编译期:

  可执行程序/动态库A 需要链接动态库B.so的时候,需要的B的路径+库的名字。

  隐式链接:A需要在B.so的导出符号表里找到需要的符号是否存在,A会保存B的名字(这个ldd命令,elf文件,PE文件都能证明确实保存了)

2.运行期:

       A 根据环境变量或者/lib或者当前目录等措施知道B所在的路径,再用类似dlopen系列操作系统API加载B到虚拟地址空间,并且把地址为0的符号找到真实的符号地址。

      Windows 的坑:.lib不一定是静态库

 

 在线学习网站

Introduction to CMake by Example | derekmolloy.ie

Learn CMake's Scripting Language in 15 Minutes

 

Option 3:

set(CMAKE_C_COMPILER "/path/to/your/c/compiler/executable")
set(CMAKE_CXX_COMPILER "/path/to/your/cpp/compiler/executable")

Edit the CMakeLists.txt file of "Assimp": Add these lines at the top (must be added before you use project() or enable_language() command)


posted @ 2021-04-14 18:49  water_bear  阅读(128)  评论(0编辑  收藏  举报