其他分享
首页 > 其他分享> > ClionLLT搭建

ClionLLT搭建

作者:互联网

1. 准备工作

a.下载googletest源码

gtest源码下载地址:

下载地址:https://github.com/google/googletest

git 仓库地址:https://github.com/google/googletest.git

b.clion工程准备 

2. 目录结构

 

 3.Cmake脚本

cmake_minimum_required(VERSION 3.20)
project(CppLLT)

set(CMAKE_CXX_STANDARD 17)
add_compile_options(-Wall)

#googletest 编译脚本
set(GOOGLETEST_VERSION 3.15)
set(PATH_GTEST_MAIN ${CMAKE_SOURCE_DIR}/googletest/googletest)
set(PATH_GTEST_MOCK ${CMAKE_SOURCE_DIR}/googletest/googlemock)
#包含googletest头文件目录
set(PATH_GTEST_INC_LIST
        ${PATH_GTEST_MAIN}/include
        ${PATH_GTEST_MAIN}/include/gtest
        ${PATH_GTEST_MOCK}/include
        ${PATH_GTEST_MOCK}/include/gmock)
include_directories(${PATH_GTEST_INC_LIST})
#添加googletest需要编译的目录
add_subdirectory(${CMAKE_SOURCE_DIR}/googletest)
#googletest库
set(gtestlibs gtest gtest_main gmock gmock_main)

#添加用例目录
aux_source_directory(${CMAKE_SOURCE_DIR}/src SRCLIST_PATH_SRC)

add_executable(CppLLT main.cpp ${SRCLIST_PATH_SRC})
#链接gtest库
target_link_libraries(CppLLT ${gtestlibs})

单独放一个cmake文件也可以

buildgtest.cmake

#googletest 编译脚本
set(GOOGLETEST_VERSION 3.15)
set(PATH_GTEST_MAIN ${CMAKE_SOURCE_DIR}/googletest/googletest)
set(PATH_GTEST_MOCK ${CMAKE_SOURCE_DIR}/googletest/googlemock)
#包含googletest头文件目录
set(PATH_GTEST_INC_LIST
        ${PATH_GTEST_MAIN}/include
        ${PATH_GTEST_MAIN}/include/gtest
        ${PATH_GTEST_MOCK}/include
        ${PATH_GTEST_MOCK}/include/gmock)
include_directories(${PATH_GTEST_INC_LIST})
#添加googletest需要编译的目录
add_subdirectory(${CMAKE_SOURCE_DIR}/googletest)
#googletest库
set(gtestlibs gtest gtest_main gmock gmock_main)

#链接gtest库,下面这个脚本放在 add_executable
#target_link_libraries(CppLLT ${gtestlibs})

主CMakeList

cmake_minimum_required(VERSION 3.20)
project(CppLLT)

set(CMAKE_CXX_STANDARD 17)
add_compile_options(-Wall)

include(${CMAKE_SOURCE_DIR}/buildgtest.cmake)

#添加用例目录
aux_source_directory(${CMAKE_SOURCE_DIR}/src SRCLIST_PATH_SRC)

add_executable(CppLLT main.cpp ${SRCLIST_PATH_SRC})
#链接gtest库
target_link_libraries(CppLLT ${gtestlibs})

 

 

4. 测试用例

#include "gtest.h"

int AddTest(int a, int b)
{
    return a + b;
}

TEST(AddTest, test_t0)
{
    EXPECT_EQ(AddTest(1, 2), 3);
}

5. 主函数

#include "gtest.h"
#include "gmock.h"

GTEST_API_ int main(int argc, char **argv)
{
    testing::InitGoogleMock(&argc, argv);
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

6. 执行结果

 

 

常见问题:

1. googletest提示cmake版本要求较低冲突

CMake Deprecation Warning at googletest/googletest/CMakeLists.txt:56 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.

 

 

 这种在主目录已经指定了cmake的最低版本要求,删除googletest里面对cmake最低版本的请求即可。

 

标签:set,gtest,GTEST,googletest,ClionLLT,PATH,include,搭建
来源: https://www.cnblogs.com/hicpp/p/16308842.html