编程语言
首页 > 编程语言> > 配置工程及引入测试--针对C++

配置工程及引入测试--针对C++

作者:互联网

 书写CMakeList同时支持windows、Linux、Mac,将头文件,三方库,源文件,测试代码目录进行分离

使用CMAKE区分Debug版本和Release版本;工程中同时生成动态库和测试用例;动态库方便日后的调用,测试用例用来检查程序的正确性

 

C++这一块使用 googleTest作为测试工具

 

对应的CMakeList如下

cmake_minimum_required(VERSION 3.0)
project(DS)

set(CMAKE_CXX_STANDARD 14)
set(GOOGLETEST_VERSION 1.9.0)

#判断系统环境
if (CMAKE_HOST_WIN32)
    set(win 1)
elseif (CMAKE_HOST_APPLE)
    set(MACOS 1)
elseif (CMAKE_HOST_UNIX)
    set(LINUX 1)
endif ()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(win)
    set(CMAKE_PREFIX_PATH C:\\Qt\\Qt5.14.2\\5.14.2\\msvc2017_64)
elseif(MACOS)
    #set(CMAKE_PREFIX_PATH /home/bitcraze/Qt5.14.2/5.14.2/gcc_64)
    #set(CMAKE_PREFIX_PATH /opt/Qt5.14.2/5.14.2/gcc_64)
#    set(CMAKE_PREFIX_PATH /home/liujg/Qt5.14.2/5.14.2/gcc_64)
    set(CMAKE_PREFIX_PATH /Users/liujg/Qt5.14.2/5.14.2/clang_64)
else()
    set(CMAKE_PREFIX_PATH /opt/Qt5.14.2/5.14.2/gcc_64)
endif()

find_package(Qt5 COMPONENTS
        Core
        REQUIRED
        )

add_subdirectory(external/googletest)
include_directories(include
        external/googletest/include)

aux_source_directory(src DIR_SRC)
aux_source_directory(test DIR_TEST)


#加载所有的.h头文件
#file(GLOB_RECURSE DIR_INC_H   include/*.h)

set(mode "CMAKE_BUILD_TYPE")
if (CMAKE_HOST_WIN32)
    add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
    add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
endif()
#生成动态库
if(${mode} AND (CMAKE_BUILD_TYPE STREQUAL "Debug")) # 修改点
    add_library(libDS-d  SHARED ${DIR_SRC}
            ${DIR_INC_H} )
    target_link_libraries(libDS-d
            Qt5::Core
            )
elseif(${mode} AND (CMAKE_BUILD_TYPE STREQUAL "Release")) # 修改点
    add_library(libDS  SHARED ${DIR_SRC}  ${DIR_INC_H}  )
    target_link_libraries(libDS
            Qt5::Core
            )
endif()


#生成测试程序
if(${mode} AND (CMAKE_BUILD_TYPE STREQUAL "Debug"))
    add_executable(MyDS-d-test  ${DIR_SRC}  ${DIR_TEST}
            ${DIR_INC_H} external/googletest)
    target_link_libraries(MyDS-d-test
            Qt5::Core  gtest_main
            )
elseif(${mode} AND (CMAKE_BUILD_TYPE STREQUAL "Release"))
    add_executable(MyDS-test  ${DIR_SRC}  ${DIR_TEST}
            ${DIR_INC_H}  external/googletest)
    target_link_libraries(MyDS-test
            Qt5::Core gtest_main
            )
endif()

 

 

 

 

标签:set,CMAKE,--,C++,add,Qt5.14,64,测试,DIR
来源: https://www.cnblogs.com/logmagic/p/16600665.html