VSCode下c++工程环境搭建并配置调试工具

VSCode版本:1.32.3

  • 使用CMake作为编译系统;
  • 使用cpplint作为代码静态扫描工具,编程规范遵循《Google C++ Style Guide》;
  • 使用googletest作为测试框架;

目录结构:

/template
   /.vscode
   /build
   /src
   /test
   .gitignore
   CMakeList.txt
   CMakeList.txt.in
   README.md

集成googletest

添加CMakeList.txt.in文件:

cmake_minimum_required(VERSION 2.8.2)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
  GIT_REPOSITORY    https://github.com/google/googletest.git
  GIT_TAG           master
  SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
  BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
  CONFIGURE_COMMAND ""
  BUILD_COMMAND     ""
  INSTALL_COMMAND   ""
  TEST_COMMAND      ""
)

配置CMakeList.txt:

# 设置cmake版本
cmake_minimum_required(VERSION 3.0)
# 设置项目名称
project(template)

# 使用c++11标准编译
set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ ${CMAKE_CXX_FLAGS}")

# 在配置阶段下载和解压googletest
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

# windwos下避免覆盖编译器配置
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# gtest配置在build目录下
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
                 ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
                 EXCLUDE_FROM_ALL)

# 兼容CMake2.8.11以前的版本
if (CMAKE_VERSION VERSION_LESS 2.8.11)
  include_directories("${gtest_SOURCE_DIR}/include")
endif()

set(LIB_SRC
  src/example.cc
)

include_directories(src)
add_library(${PROJECT_NAME}_lib ${LIB_SRC})
add_executable(${PROJECT_NAME} src/main.cc)
target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}_lib)

# 添加测试程序
set(TEST_SOURCE test/test_example)
add_executable(${PROJECT_NAME}-test ${LIB_SRC} ${TEST_SOURCE})
target_link_libraries(${PROJECT_NAME}-test gtest_main)

安装cpplint

cpplint是Google的编码规范lint工具,基于python实现,实际上就是cpplint.py驱动的扫描。

  1. 安装python/pip
  2. 使用pip下载安装cpplint:
$ sudo pip install cpplint
  1. 确认安装完成:
$ ls -l /usr/local/bin/cpplint*
  1. 在VSCode中下载cpplint的插件并启用。

参考:cpplint使用技巧

配置launch.json以启用断点调试功能

launch.json:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(lldb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/build/template",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "lldb"
    }
  ]
}

其它

搭建好的工程环境可以从我的Github上直接clone:cpp-template-workspace

你可能感兴趣的:(VSCode下c++工程环境搭建并配置调试工具)