You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

包含<gtest/gtest.h>头文件时出现编译错误求助

编译Google Test时的std::mutex错误解决及测试链接方案

一、编译错误信息

In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\gtest-message.h:55:0,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\gtest-assertion-result.h:46,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\gtest.h:59,
                 from traingle_test.cpp:2:
c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\internal\gtest-port.h:1238:8: error: 'mutex' in namespace 'std' does not name a type
   std::mutex mu_;
        ^~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\internal\gtest-port.h:1239:8: error: 'condition_variable' in namespace 'std' does not name a type
   std::condition_variable cv_;
        ^~~~~~~~~~~~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\internal\gtest-port.h: In member function 'void testing::internal::Notification::Notify()':
c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\internal\gtest-port.h:1225:21: error: 'mutex' is not a member of 'std'
     std::lock_guard<std::mutex> lock(mu_);
                     ^~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\internal\gtest-port.h:1225:21: error: 'mutex' is not a member of 'std'
c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\internal\gtest-port.h:1225:31: error: template argument 1 is invalid
     std::lock_guard<std::mutex> lock(mu_);

二、相关文件内容

1. triangle_test.cpp

#include "traingle.h"
#include <gtest/gtest.h>
namespace {
TEST (TraingleTest, InvalidSides){
    EXPECT_EQ(-1, TypeOfTraingle(-10, 20, 30));
    EXPECT_EQ(-1, TypeOfTraingle(10, -20, 30));
    EXPECT_EQ(-1, TypeOfTraingle(3, 4, -8));
}
}

2. triangle.cpp

#include "traingle.h"
/* Return values
1 for equilateral, 2 for isosceles, 3 for scalan
0 for traingle can't be formed with given sides
-1 if any side value is invalid, say -ve
*/
int TypeOfTraingle(int a, int b, int c){
    if(a<0 || b<0 || c<0)
     return -1;
    if(!(a+b > c && b + c > a && a + c > b))
     return 0;
    else if (a==b && b==c)
     return 2;
    else
     return 3;
}

3. triangle.h

#ifndef __TRIANGLE_H
#define __TRIANGLE_H

int TypeOfTraingle(int, int, int);

#endif

三、错误原因及解决办法

错误原因

MinGW 6.3.0默认采用C03标准,而std::mutex、std::condition_variable是C11新增的线程库特性,Google Test内部依赖这些特性,因此编译时必须显式启用C++11或更高标准。

解决步骤

  1. 编译时添加-std=c++11参数,开启C++11支持;
  2. 链接Google Test相关库(gtest和gtest_main),同时链接pthread库以支持线程特性。

四、完整编译链接命令

假设所有文件在同一目录下,可使用以下命令完成编译链接:

# 分步编译
g++ -std=c++11 -c triangle.cpp -o triangle.o
g++ -std=c++11 -c triangle_test.cpp -o triangle_test.o
g++ triangle.o triangle_test.o -o triangle_test -lgtest -lgtest_main -lpthread

# 一步编译
g++ -std=c++11 triangle.cpp triangle_test.cpp -o triangle_test -lgtest -lgtest_main -lpthread

五、测试优化建议

当前测试仅覆盖了无效边长场景,且triangle.cpp中等边三角形的返回值(注释标注为1,代码返回2)存在不一致,建议修正并补充测试用例:

修正triangle.cpp的返回值逻辑

else if (a==b && b==c)
 return 1; // 与注释的返回值定义对齐

补充测试用例

TEST(TraingleTest, Equilateral) {
    EXPECT_EQ(1, TypeOfTraingle(5,5,5));
}

TEST(TraingleTest, Isosceles) {
    EXPECT_EQ(2, TypeOfTraingle(5,5,6));
    EXPECT_EQ(2, TypeOfTraingle(5,6,5));
    EXPECT_EQ(2, TypeOfTraingle(6,5,5));
}

TEST(TraingleTest, Scalene) {
    EXPECT_EQ(3, TypeOfTraingle(3,4,5));
}

TEST(TraingleTest, NotTriangle) {
    EXPECT_EQ(0, TypeOfTraingle(1,2,3));
    EXPECT_EQ(0, TypeOfTraingle(10,1,2));
}

运行编译后的triangle_test程序,即可看到所有测试用例的通过情况及对应三角形类型的验证结果。

内容的提问来源于stack exchange,提问作者Kiran M K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 12:50:29