0%

c和cpp

前言

本文的内容按照下面的思路组织

语言基础

语言编译器一般由词法分析,语法分析,语义分析组成,这和学习一门语言的思路是类似的,一门语言的基本面包括:关键字和基本的函数库和类库,所以在编排结果的时候首先将这一部分放在前面

  • 关键字:c族的基本关键字
  • 基本输入输出:由于在算法题中基本的输入输出占据非常大的部分,所以这部分的函数库专门为1 part
  • stdstl,标准函数库和类库

编译和执行相关

了解到一门语言的文法,要让自己的代码活起来,需要一个东西来执行你写的代码,有的时候这个执行环境在IDE中开箱即用。但对于c++来说,往往不能忽视这部分。而用来进行编译的脚本配置,在c++中叫cmakelist

设计模式

在一门语言的实践过程中,由于抽象,规范等原因,形成了一些编码模板和套路,这种套路叫设计模式。

一些奇怪的需求

有的时候产生了一些奇奇怪怪的想法,比如对输入输出重定向,以方便自己的需要,将这些奇奇怪怪的需求的实现,特别地组织到一块。

IDE

使用一门语言进行开发和编程的时候,需要有一份编写代码的环境,毕竟用记事本来写代码不大现实,好的ide往往有促进学习的作用。

  • clion:jetbrains家族的ide,用起来很舒服
  • visual studio:据说是win平台下比较好用的ide,实际体验并不理想。

细碎知识

跟一门语言打交道,往往会因为一些小细节而磕磕绊绊,有的时候也会了解到这门语言的设计原则与特性,把这些小细节记录一下,方便回顾与总结

变量

初始化

全局变量会默认初始化

参考文献

条件编译

关键字

int

用int进行强制类型转换的时候,跟python的int()一样都是截断小数部分

{}初始化

c++ 11 新特性

1
int a{} // a = 0

参考文献

指针的大小

指针即为地址,$\color{red}{\text{指针几个字节跟语言无关,而是跟系统的寻址能力有关}}$。譬如以前是16为地址,指针即为2个字节,现在一般是32位系统,所以是4个字节,以后64位,则就为8个字节。

参考文献

#define宏

参考文献

条件编译

#ifdef

参考文献

作用域

当前文件

参考文献

extern

参考文献

基本输入输出

cin 和 cout

格式化输出

#include <iomanip>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// // 设置宽度为5
// cout << setw(5) << string

// // 不足5的部分往左边填充'0'字符
// cout << setw(5) << setfill('0') << string


#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n = 141;
//1) 分别以十六进制、十进制、八进制先后输出 n
cout << "1)" << hex << n << " " << dec << n << " " << oct << n << endl;
double x = 1234567.89, y = 12.34567;
//2)保留5位有效数字
cout << "2)" << setprecision(5) << x << " " << y << " " << endl;
//3)保留小数点后面5位
cout << "3)" << fixed << setprecision(5) << x << " " << y << endl;
//4)科学计数法输出,且保留小数点后面5位
cout << "4)" << scientific << setprecision(5) << x << " " << y << endl;
//5)非负数显示正号,输出宽度为12字符,宽度不足则用 * 填补
cout << "5)" << showpos << fixed << setw(12) << setfill('*') << 12.1 << endl;
//6)非负数不显示正号,输出宽度为12字符,宽度不足则右边用填充字符填充
cout << "6)" << noshowpos << setw(12) << left << 12.1 << endl;
//7)输出宽度为 12 字符,宽度不足则左边用填充字符填充
cout << "7)" << setw(12) << right << 12.1 << endl;
//8)宽度不足时,负号和数值分列左右,中间用填充字符填充
cout << "8)" << setw(12) << internal << -12.1 << endl;
cout << "9)" << 12.1 << endl;
return 0;
}

// 输出结果
// 1)8d 141 215
// 2)1.2346e+06 12.346
// 3)1234567.89000 12.34567
// 4)1.23457e+06 1.23457e+01
// 5)***+12.10000
// 6)12.10000****
// 7)****12.10000
// 8)-***12.10000
// 9)12.10000

参考文献

std

string

string to int

int n = atoi(str.c_str());

参考文献

切片

s.substr(pos, n)

参考文献2
参考文献

替换

参考文献

替换指定字符串

参考文献

变量拷贝

直接就好

1
2
3
string a = "text";
string b = "image";
b = a;

to_string

头文件 #include <string>

参考文献

==

==就是值比较

参考文献

split

参考文献

vector join to string

top参考文献

参考文献

将字符串转为数组

参考文献

字符串相关

字符串与其他数据类型互转

字符串拼接

lower

代码详情
1
2
3
4
5
6
7
#include <algorithm>
#include <cctype>
#include <string>

std::string data = "Abc";
std::transform(data.begin(), data.end(), data.begin(),
[](unsigned char c){ return std::tolower(c); });

参考文献

algorithm.h

参考文献

sort 多属性排序

参考文献:重载运算符

参考文献

重载运算符

std::list::sort

时间复杂度O(n)

参考文献

sort的时间复杂度分析

参考文献

copy_if

相当于函数式语言的filter

参考文献

queue

queue的基本操作
queue的使用

Q:为什么pop没有返回值

list

c++遍历list

map

map的初始化

遍历map

map的[]运算没有这个key会生成这个key,at运算只查询不修改

map的排序

只遍历key:c++17

map按照key排序,默认就已经按照key排序了

按照key排序:最好用set作为存储结构,然后用pair

pair和map的区别

参考文献:返回两个值的时候可以用pair

What is the difference between std::listand std::map in C++ STL?

What is the difference between maps and pairs in STL C++, in simple terms? Is pairp[5] the same as a map of pairs map< pair> ?

排序

参考文献

vector

遍历

vector的使用

排序

由小到大排序

sort(list.begin(), list.end(), [](auto a, auto b){ return a.key < b.key; });

默认就是升序

参考文献

By default, the sort() function sorts the elements in ascending order

部分排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// C++ program to demonstrate the use of
// std::partial_sort
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v = { 1, 3, 1, 10, 3, 3, 7, 7, 8 }, i;

vector<int>::iterator ip;

// Using std::partial_sort
std::partial_sort(v.begin(), v.begin() + 3, v.end());

// Displaying the vector after applying
// std::partial_sort
for (ip = v.begin(); ip != v.end(); ++ip) {
cout << *ip << " ";
}

return 0;
}

// 1 1 3 10 3 3 7 7 8

参考文献

reverse

参考文献

remove

参考文献

erase and clear的区别

clear是删除全部元素

erease只删除特定的元素

参考文献

判断vector中的值是否相等

代码详情
1
2
3
4
5
//assuming v has at least 1 element
if ( std::equal(v.begin() + 1, v.end(), v.begin()) )
{
//all equal
}

参考文献

填充vector

参考文献

向量之间做减法

像数学中的减法一样

参考文献

string

格式化一个整型1变成字符串“01”

stdstring-formatting-like-sprintf

stringstream

ostringstream

c++20:format

stream

参考文献

stl容器的fitler

参考文献

对vector求和

参考文献

对比篇

vector和list

参考文献:表格

参考文献:文字和例子

list, vector, map, set

参考文献

vector vs 数组

参考文献

类和对象

友元函数

参考文献

参考文献

虚函数

参考文献

参考文献

c++的虚函数 vs java的抽象函数

参考文献

cmake

配置文件:cmakelist

makefile,和makelist的区别

参考文献

参考文献

makelist

添加h和cpp文件

参考文献

参考文献

执行shell命令

add_custom_command,add_exucatable,add_custom_target, execute_process, exec_program

add_exucatable

参考文献

execute_process

官方文档

添加子文件夹位source路径

参考文献

执行shell命令

参考文献:cmake官方文档

参考文献:csdn博客

$\color{red}{\text{Q}}$:能不能像python一样动态导入包

参考文献

C++设计模式

参考文献:形象简短的解释

参考文献:代码加解释

骚操作

想把long long int typedef 成 int


但是失败了,摊手

将字符串inject到cin

参考文献

子问题1

重定向输入输出

参考文献

重定向cout

参考文献

重定向std

参考文献

参考文献

clion的配置

参考文献

mingw64版本的区别

参考文献

Google Test的配置

github 主页

正解

直接add subdirectory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 添加头文件
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/googletest/include
)

# 添加下级目录,生成.a文件
add_subdirectory( ./googletest)

# 添加连接库
link_directories(
${CMAKE_CURRENT_SOURCE_DIR}/googletest
)


# 将目标文件与库文件进行链接
target_link_libraries(algorithms gtest)

去官网下载cmake

debug 显示容器的值

*(a._M_impl._M_start+1)这也太复杂了

正解

参考文献

bug:看不到嵌套stl容器的内容

参考文献

bug

make出错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
-- Building for: NMake Makefiles
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:10 (project):
The CMAKE_C_COMPILER:

cl

is not a full path and was not found in the PATH.

To use the NMake generator with Visual C++, cmake must be run from a shell
that can use the compiler cl from the command line. This environment is
unable to invoke the cl compiler. To fix this problem, run cmake from the
Visual Studio Command Prompt (vcvarsall.bat).

Tell CMake where to find the compiler by setting either the environment
variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
the compiler, or to the compiler name if it is in the PATH.


CMake Error at CMakeLists.txt:10 (project):
The CMAKE_CXX_COMPILER:

cl

is not a full path and was not found in the PATH.

To use the NMake generator with Visual C++, cmake must be run from a shell
that can use the compiler cl from the command line. This environment is
unable to invoke the cl compiler. To fix this problem, run cmake from the
Visual Studio Command Prompt (vcvarsall.bat).

Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.


-- Configuring incomplete, errors occurred!
See also "D:/Users/LND/Desktop/ereaseo/googletest/build/CMakeFiles/CMakeOutput.log".
See also "D:/Users/LND/Desktop/ereaseo/googletest/build/CMakeFiles/CMakeError.log

This file does not belong to any project target; code insight features might not work properly.

如果reload cmake file失败

则可能是idea的项目文件出问题了

违反规范?

1
2
3
4
5
6
7
8
9
10
11
CMake Warning (dev) at CMakeLists.txt:2 (project):
Policy CMP0048 is not set: project() command manages VERSION variables.
Run "cmake --help-policy CMP0048" for policy details. Use the cmake_policy
command to set the policy and suppress this warning.

The following variable(s) would be set to empty:

CMAKE_PROJECT_VERSION
CMAKE_PROJECT_VERSION_MAJOR
CMAKE_PROJECT_VERSION_MINOR
CMAKE_PROJECT_VERSION_PATCH

winmain

1
2
3
4
5
6
7
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\algorithms.dir\build.make:106: algorithms.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:156: CMakeFiles/algorithms.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:163: CMakeFiles/algorithms.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:182: algorithms] Error 2

缺少main

不是有效的win程序

1
CreateProcess error=193, %1 不是有效的 Win32 应用程序

重新build一下就好了

明天去回答一下这个问题

参考文献

csdn:CLion下的gtest测试
unknown

新的bug

找不到std

No member named ‘vector’ in namespace ‘std’

debugger

看不了对应idx的vector容器的值

图片详情找不到图片(Image not found)
图片详情找不到图片(Image not found)

不退出的方法

图片详情找不到图片(Image not found)

关于clion中cmake造成的问题

起因,我用ifsteam读不到文件

1
2
3
4
5
6
7
8
char buffer[256];
if (! in.is_open())
{ cout << "Error opening file"; exit (1); }
while (!in.eof() )
{
in.getline (buffer,100);
cout << buffer << endl;
}

当我用上面的代码查看文件的打开状态的时候发现文件是没有打开的

又用了,下面的代码查看当前的工作路径

1
2
3
4
5
6
7
8
9
10
11
char *buffer;
//也可以将buffer作为输出参数
if((buffer = getcwd(NULL, 0)) == NULL)
{
perror("getcwd error");
}
else
{
printf("%s\n", buffer);
free(buffer);
}

发现当前的工作路径

不是该c++文件的路径

无法run

图片详情找不到图片(Image not found)

虽然不知道为什么,但是解决了问题

图片详情找不到图片(Image not found)

新建c++ source时的模板

参考文献

防止跳入源码

参考文献

GDB

debug的时候gdb还是很常用的。。还是想办法总结一下吧,都忘了

参考文献

visual studio

快捷键

键位用处
F9快速打断点
alt+enter快速修复问题,加分号

使用最新的c++标准

打开属性

使用最新的标准

查看visual studio的版本

参考文献

Q:新建的文件找不到

visual studio快捷注释

批量注释: Ctrl+K,Ctrl+C
取消注释: Ctrl+K,Ctrl+U
参考文献

引入别的库

参考文献

同样的cmake配置clion通过,vs报错

像idea一样evaluate expresion

(Debug -> QuickWatch or Shift + F9)

参考文献

但是好像不能执行函数


参考文献

debug:变量已被优化掉,因而不可用。

打开项目设置

关闭优化

参考文献

debug: 快速加分号

alt+enter

参考文献

参考文献

同一个解决方案下建立多个项目

文件->新建项目->添加到现有解决方案

切换项目

当前路径

添加依赖

将依赖文件添加到库文件的存放位置
D:\vs\IDE\VC\Tools\MSVC\14.29.30037\include

然后再include即可

Q & A

c++的理解,语言特性

参考文献

cpp的编译过程

参考文献

c++引用和指针的区别

特性引用指针
是否需要初始化$\color{green}{\checkmark}$$\color{red}{\times}$
指向能否修改$\color{red}{\times}$$\color{green}{\checkmark}$
1
2
3
4
5
6
7
int a,b,*p,&r=a;//正确,p是指针,r是引用
r=3;//正确:等价于a=3
int &rr;//出错:引用必须初始化
p=&a;//正确:p中存储a的地址,即p指向a
*p=4;//正确:p中存的是a的地址,对a所对应的存储空间存入值4
p=&b//正确:p可以多次赋值,p存储b的地址

参考文献

$\color{red}{\text{ Q:c++:[]写在前面}}$

这是c++的lambda表达式

Q:引用传递和指针传递的区别

Q:c++的抽象类,虚函数,java的指针之间的区别

c++的抽象类

Q:using name space std和#include的区别

using name space 是物理替换

参考文献

Q:

参考文献

Q:cbegin

参考文献

Q:c++匿名函数

参考文献

Q:foreach

参考文献

Q:为什么const之后会报错

aka char* 是啥东西?

参考文献

c++ string 会自动初始为空串

c++ 的初始化

c++

Q:循环

for (auto & testcase :j)for (auto testcase :j)的区别

Q:c++中类似java的max value

#include <limits.h>

float.h

参考文献

new和malloc的区别

分析两段代码

C的初始动态分配语句为
L.data= (ElemType* ) malloc (sizeof (ElemType ) *Initsize) ;
C++的初始动态分配语句为
L.data=new ElemType [ Initsize];

性质newmalloc
属性new/delete:这两个是C++中的$\color{green}{\text{关键字}}$,若要使用,需要编译器支持;malloc/free:这两个是$\color{green}{\text{库函数}}$,若要使用则需要引入相应的头文件才可以正常使用。
使用无需显式填入申请的内存大小,new会根据new的类型分配内存。申请空间需要显式填入申请内存的大小;
内存位置此操作符分配的内存空间是在自由存储区申请的内存是在堆空间
返回类型new操作符内存分配成功时,返回的是对象类型的指针,类型严格与对象匹配,无须进行类型转换,故new是符合类型安全性的操作符。而malloc内存分配成功则是返回void ,需要通过强制类型转换将void指针转换成我们需要的类型。
分配失败情况new内存分配失败时,会抛出bac_alloc异常,它不会返回NULL,分配失败时如果不捕捉异常,那么程序就会异常退出,我们可以通过异常捕捉的方式获取该异常。分配内存失败时返回NULL,我们可以通过判断返回值可以得知是否分配成功;
定义对象系统调度过程使用new操作符来分配对象内存时会经历三个步骤:
1.调用operator new 函数(对于数组是operator new[])分配一块足够的内存空间(通常底层默认使用malloc实现,除非程序员重载new符号)以便存储特定类型的对象;
2.编译器运行相应的构造函数以构造对象,并为其传入初值。
3.对象构造完成后,返回一个指向该对象的指针。
使用delete操作符来释放对象内存时会经历两个步骤:
1.调用对象的析构函数。
2.编译器调用operator delete(或operator delete[])函数释放内存空间(通常底层默认使用free实现,除非程序员重载delete符号)。
扩张内存大小没有扩张内存的机制使用malloc分配内存后,发现内存不够用,那我们可以通过realloc函数来扩张内存大小,realloc会先判断当前申请的内存后面是否还有足够的内存空间进行扩张,如果有足够的空间,那么就会往后面继续申请空间,并返回原来的地址指针;否则realloc会在另外有足够大小的内存申请一块空间,并将当前内存空间里的内容拷贝到新的内存空间里,最后返回新的地址指针。
是否可以重载$\color{green}{\checkmark}$$\color{red}{\times}$
构造函数与析构函数$\color{green}{\checkmark}$$\color{red}{\times}$
处理数组有处理数组的new版本,new[]需要用户计算数组的大小后进行内存分配

堆和自由存储区

是C语言和操作系统的术语,堆是操作系统所维护的一块特殊内存,它提供了动态分配的功能,当运行程序调用malloc()时就会从中分配,调用free()归还内存。那什么是自由存储区呢?

自由存储区 是C++中动态分配和释放对象的一个概念,通过new分配的内存区域可以称为自由存储区,通过delete释放归还内存。自由存储区可以是堆、全局/静态存储区等,具体是在哪个区,主要还是要看new的实现以及C++编译器默认new申请的内存是在哪里。但是基本上,很多C++编译器默认使用堆来实现自由存储,运算符new和delete内部默认是使用malloc和free的方式来被实现,说它在堆上也对,说它在自由存储区上也正确。因为在C++中new和delete符号是可以重载的,我们可以重新实现new的实现代码,可以让其分配的内存位置在静态存储区等。而malloc和free是C里的库函数,无法对其进行重载。

参考文献

Q:引用传递和指针传递

TODO:在参数传递中c++的引用传递和c语言的指针传递的区别

Q:c++和不可以重载+,-运算符

Q:定义结构体的时候*有什么用

问题来源

Q:指针数组vs数组指针

Q: 双引号引入头文件和<>引入的区别

1.双引号:引用非标准库的头文件,编译器首先在程序源文件所在目录查找,如果未找到,则去系统默认目录查找,通常用于引用用户自定义的头文件。

2.尖扩号:只在系统默认目录(在Linux系统中通常为/usr/include目录)或者尖括号内的路径查找,通常用于引用标准库中自带的头文件。

综上,标准库自带的头文件既可以用双引号也可以用尖括号,不过习惯使用尖括号,用户自定义的头文件只能用双引号。

参考文献

Q: 模板类和抽象类

Q:STL和algorithm

Q:const + & 的好处是什么

Q:直接声明和new的区别

Q:cast 和 conversion的区别

参考文献:c++和java

参考文献:syntactic 与semantic 的区别

参考文献:C#

about: nullptr

nullptr的地址是0x00000000

拓展:记忆进制缩写

告诉你个好办法,叫BODH法,你可以记成body,依次是2,8,10,16进制的缩写字母

参考文献

c++导库的方式

c++读取json格式

参考文献

lambda表达式

c++中的lambda表达式不能使用外部变量[]中为空的话

Details![](https://raw.githubusercontent.com/ednow/cloudimg/main/githubio/20210709232303.png)

参考文献

bug

自己传错了参数

C++ exception with description “[json.exception.type_error.305] cannot use operator[] with a string argument with array” thrown in the test body.

Details![](https://raw.githubusercontent.com/ednow/cloudimg/main/githubio/20210709232605.png)

cin vs scanf

cin有缓冲区

优化方法:

1
std::ios::sync_with_stdio(false);

zhihu讨论
效率对比

cin 和 cout

标准输入输出流对象

参考文献

cpp中的数据结构和算法库相关

1
2
3
4
5
6
7
8
9
10
11
12
#include <cstring>
#include <deque>
#include <string>
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <vector>
#include <map>
#include <list>
#include <string.h>

cstdiostdio.h 的区别

iostreamstdio.h 的区别

vector的解析

动态数组和变长数组的区别

algorithm头文件中的函数

拓展:java

1
2
3
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;

std和stl的区别

参考文献

struct和class的区别

c++

参考文献

c实现class

图片详情找不到图片(Image not found)

参考文献:redis源码
csdn

类型转换

unsigned long long to int?

不要用

参考文献

null和nullptr

智能指针

参考文献

参考文献

使用析构函数实现智能指针

参考文献

c++中的and和or关键字

参考文献

成员名,变量名,结构体名可以一致

参考文献

remove if,删除容器中的元素

参考文献

clang tidy

参考文献

gtest + nlohmann/json.hpp 测试环境搭建

引入nlohmann/json.hpp

通过别的库找到include的位置,nlohmann文件夹copy过去

图片详情找不到图片(Image not found)

引入gtest

将include引入

图片详情找不到图片(Image not found)

bug

代码详情
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
====================[ Build | untitled1 | Debug ]===============================
"C:\Program Files\JetBrains\CLion 2021.1.2\bin\cmake\win\bin\cmake.exe" --build C:\Users\lnd\CLionProjects\untitled1\cmake-build-debug --target untitled1 -- -j 9
[ 33%] Linking CXX executable untitled1.exe
CMakeFiles\untitled1.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/lnd/CLionProjects/untitled1/main.cpp:7: undefined reference to `testing::InitGoogleTest(int*, char**)'
CMakeFiles\untitled1.dir/objects.a(main.cpp.obj): In function `RUN_ALL_TESTS()':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/gtest/gtest.h:2490: undefined reference to `testing::UnitTest::GetInstance()'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/gtest/gtest.h:2490: undefined reference to `testing::UnitTest::Run()'
CMakeFiles\untitled1.dir/objects.a(MaximalClique.cpp.obj): In function `PTA_1142_Test::TestBody()':
C:/Users/lnd/CLionProjects/untitled1/PTA/PTA1142/MaximalClique.cpp:135: undefined reference to `testing::Message::Message()'
C:/Users/lnd/CLionProjects/untitled1/PTA/PTA1142/MaximalClique.cpp:135: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
C:/Users/lnd/CLionProjects/untitled1/PTA/PTA1142/MaximalClique.cpp:135: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
C:/Users/lnd/CLionProjects/untitled1/PTA/PTA1142/MaximalClique.cpp:135: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
C:/Users/lnd/CLionProjects/untitled1/PTA/PTA1142/MaximalClique.cpp:135: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
CMakeFiles\untitled1.dir/objects.a(MaximalClique.cpp.obj): In function `__static_initialization_and_destruction_0':
C:/Users/lnd/CLionProjects/untitled1/PTA/PTA1142/MaximalClique.cpp:134: undefined reference to `testing::internal::GetTestTypeId()'
C:/Users/lnd/CLionProjects/untitled1/PTA/PTA1142/MaximalClique.cpp:134: undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
CMakeFiles\untitled1.dir/objects.a(MaximalClique.cpp.obj): In function `testing::internal::PrintTo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::ostream*)':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/gtest/gtest-printers.h:550: undefined reference to `testing::internal::PrintStringTo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::ostream*)'
CMakeFiles\untitled1.dir/objects.a(MaximalClique.cpp.obj): In function `testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int)':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/gtest/internal/gtest-internal.h:529: undefined reference to `testing::internal::IsTrue(bool)'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/gtest/internal/gtest-internal.h:529: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/gtest/internal/gtest-internal.h:529: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/gtest/internal/gtest-internal.h:529: undefined reference to `testing::internal::GTestLog::~GTestLog()'
CMakeFiles\untitled1.dir/objects.a(MaximalClique.cpp.obj): In function `testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int)':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/gtest/internal/gtest-internal.h:550: undefined reference to `testing::internal::IsTrue(bool)'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/gtest/internal/gtest-internal.h:550: undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/gtest/internal/gtest-internal.h:550: undefined reference to `testing::internal::GTestLog::~GTestLog()'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/gtest/internal/gtest-internal.h:550: undefined reference to `testing::internal::GTestLog::~GTestLog()'
CMakeFiles\untitled1.dir/objects.a(MaximalClique.cpp.obj): In function `testing::AssertionResult testing::internal::CmpHelperEQ<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/gtest/gtest.h:1546: undefined reference to `testing::AssertionSuccess()'
CMakeFiles\untitled1.dir/objects.a(MaximalClique.cpp.obj): In function `testing::AssertionResult testing::internal::CmpHelperEQFailure<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/gtest/gtest.h:1529: undefined reference to `testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
CMakeFiles\untitled1.dir/objects.a(MaximalClique.cpp.obj):MaximalClique.cpp:(.rdata$_ZTV13PTA_1142_Test[_ZTV13PTA_1142_Test]+0x20): undefined reference to `testing::Test::SetUp()'
CMakeFiles\untitled1.dir/objects.a(MaximalClique.cpp.obj):MaximalClique.cpp:(.rdata$_ZTV13PTA_1142_Test[_ZTV13PTA_1142_Test]+0x28): undefined reference to `testing::Test::TearDown()'
CMakeFiles\untitled1.dir/objects.a(MaximalClique.cpp.obj): In function `PTA_1142_Test::~PTA_1142_Test()':
C:/Users/lnd/CLionProjects/untitled1/PTA/PTA1142/MaximalClique.cpp:134: undefined reference to `testing::Test::~Test()'
CMakeFiles\untitled1.dir/objects.a(MaximalClique.cpp.obj): In function `PTA_1142_Test::PTA_1142_Test()':
C:/Users/lnd/CLionProjects/untitled1/PTA/PTA1142/MaximalClique.cpp:134: undefined reference to `testing::Test::Test()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\untitled1.dir\build.make:119: untitled1.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:94: CMakeFiles/untitled1.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:101: CMakeFiles/untitled1.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:136: untitled1] Error 2

参考文献

解决方案

此时不需要main

代码详情
1
2
3
4
SET(GCC_COVERAGE_COMPILE_FLAGS "-lgtest -lgtest_main")
SET(GCC_COVERAGE_LINK_FLAGS "-lgtest -lgtest_main")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
两行版本
1
2
SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -lgtest -lgtest_main")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lgtest -lgtest_main")

参考文献
参考文献

参考文献

参考文献

参考文献