c++ - 错误 : 'int main(int, char**)' previously defined here in C++

标签 c++ compiler-errors program-entry-point redefinition googletest

我现在正在实现 gtest,它给了我一个错误:main previously defined here。

这里是utest.cpp

// Bring in my package's API, which is what I'm testing
#include "../src/test.cpp"
// Bring in gtest
#include <gtest/gtest.h>

// Declare a test
TEST(TestSuite, testCase1)
{
     EXPECT_EQ(5,getX(5));
}

// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

这是我正在测试的代码 测试.cpp

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <Project2Sample/R_ID.h>
#include <geometry_msgs/Twist.h>
#include <nav_msgs/Odometry.h>
#include <sensor_msgs/LaserScan.h>


#include <sstream>
#include "math.h"

int getX(int x)
{
    return x;
}

int main(int argc, char **argv)
{
    return 0;
}

test.cpp main 中没有任何内容,但实际代码在 main 中会有一些代码。

我没有utest和test cpp文件的头文件

我试过了

#ifndef UTEST_H
#define UTEST_H

并没有解决错误。

最佳答案

错误消息说明了问题所在,您有两个 main() 函数。我相信您想从 test.cpp 中删除重复的 main()

关于c++ - 错误 : 'int main(int, char**)' previously defined here in C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7564064/

相关文章:

c++ - sizeof(some pointer) 是否总是等于四?

Visual Studio 中的 C 程序。具有常量值的数组被视为错误

C 函数中出现错误 `main' : undefined reference to `WinMain'

java - 无法执行 jar 文件 : "no main manifest attribute"

c++ - 切换表示为字符的位的 3 种方法

c++ - 使用数据库值填充 QTableWidget

c++ - 这段代码是做什么的 : static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}};

c++ - 多维数组类错误

java - 该代码原本应该反转第一个参数给出的字符串,但是它存在编译错误

c - 用常量参数定义 main (const int argc, const char * const argv[])?