一、环境配置
1.安装MSYS,官网安装地址
安装完成之后配置需要的插件(配置mingW64的插件)
cmake,make,gcc,gdb,gtest
然后在cmd命令输入[cmd] -v(example: gcc -v),查看环境变量配置是否成功(一般没问题)
常用下载命令如下:
pacman -S xx 安装软件xx
pacman -R xx 删除软件xx
2. Gest学习可以参考 Google Test(GTEST)使用入门(1)- 下载编译安装执行_wdcyf15的博客-CSDN博客_gtest下载安装
3.安装VScode,官网 VSCode中文网 - Visual Studio Code中文官网 VS Code编辑器中文网站
然后按需安装各种插件,安装popular排行榜前面的就行
4.建立VScode工程目录结构可以这样建立:
4.1 使用VScode可视化调试要配置launch.json,调试googletest或者main文件进行注释可选操作即可
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": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/exe/test.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/exe",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin/gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
// {
// "name": "(gdb) 启动",
// "type": "cppdbg",
// "request": "launch",
// "program": "${workspaceFolder}/exe/main.exe",
// "args": [],
// "stopAtEntry": false,
// "cwd": "${workspaceFolder}/exe",
// "environment": [],
// "externalConsole": false,
// "MIMode": "gdb",
// "miDebuggerPath": "C:\\msys64\\mingw64\\bin/gdb.exe",
// "setupCommands": [
// {
// "description": "为 gdb 启用整齐打印",
// "text": "-enable-pretty-printing",
// "ignoreFailures": true
// }
// ],
// }
]
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
set(CMAKE_CXX_STANDARD 11)
include_directories(include)
add_definitions("-Wall -g")
project(main)
project(test)
add_executable(main src/main.c src/func.c)
add_executable(test gtest/gtest.cc src/func.c)
target_link_libraries(test libgtest libgtest_main)
build.sh
#!/bin/bash
echo "please enter you choice:(1,2,3,4)"
echo "1. make all"
echo "2. make modify code"
echo "3. make clean"
echo "4. make run main"
echo "5. make run test"
read -p "enter a number: " choice
if [ $choice -eq 1 ];then
rm -rf build
mkdir build
cmake -G "MinGW Makefiles" -S . -B build
cd build
make
mv *.exe ../exe
exit
fi
if [ $choice -eq 2 ];then
rm -rf exe
mkdir exe
make
mv *.exe ../exe
exit
fi
if [ $choice -eq 3 ];then
rm -rf build
mkdir build
rm -rf exe
mkdir exe
exit
fi
if [ $choice -eq 4 ];then
cd exe
./main.exe
exit
fi
if [ $choice -eq 5 ];then
cd exe
./test.exe
exit
fi
main.c
#include <stdio.h>
#include "func.h"
int main(int argc,char**argv)
{
printf("Hello World! %d\n", add(2,3));
return 0;
}
gtest.cc
#include <stdio.h>
#include <gtest/gtest.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "func.h"
#ifdef __cplusplus
}
#endif
TEST(testCase, test0)
{
EXPECT_EQ(add(2,3),5);
}
int main(int argc,char**argv)
{
testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
func.c
#include "func.h"
int add(int a,int b)
{
return a+b;
}
func.h
#ifndef FUNC_H
#define FUNC_H
int add(int a,int b);
#endif
5.运行效果如下:
6.调试直接点VScode的bebug或者在对应文件按F5(注意launch.json文件要配置统一)
这里调试使用就是gdb的可视化调试(要在代码调试启动之前打断点或者while(1)之类的操作)