googletest 无法 link 到 DUT
googletest can't link to DUT
当我尝试 link 应测试我的测试的代码时,我得到 undefined refrence
。
当我在同一个源文件中使用同一个函数时没有问题。
这里是link错误
g++ -o "VW_Test" ./tests/scaleSignalTests.o ./src/gtest-death-test.o ./src/gtest-filepath.o ./src/gtest-matchers.o ./src/gtest-port.o ./src/gtest-printers.o ./src/gtest-test-part.o ./src/gtest-typed-test.o ./src/gtest.o ./src/gtest_main.o ./VirtualWall/PointCalc.o ./VirtualWall/checks.o ./VirtualWall/mathHelper.o
./tests/scaleSignalTests.o: In function `TestBody':
C:\projects\VIRTUAL_WALL_TEST\Default/../tests/scaleSignalTests.cc:16: undefined reference to `multiplyInteger(int, int, int*)'
collect2.exe: error: ld returned 1 exit status
make: *** [VW_Test] Error 1
"make -j4 all" terminated with exit code 2. Build might be incomplete.
这是测试文件
#include "PointCalc.h"
#include "gtest/gtest.h"
#include <math.h>
namespace {
class scaledIntTest : public ::testing::Test {
};
TEST_F(scaledIntTest, Multiplication)
{
EXPECT_EQ(INT_FACTOR_TO_METER, 1000);
int result = 0;
// EXPECT_EQ(referencePoint, resultPoint);
EXPECT_EQ(RTN_NO_ERR,
multiplyInteger(INT_FACTOR_TO_METER * 1,
INT_FACTOR_TO_METER * 1,
&result));
EXPECT_EQ(INT_FACTOR_TO_METER, result);
}
int foo(void)
{
int result = 0;
multiplyInteger(INT_FACTOR_TO_METER * 1, INT_FACTOR_TO_METER * 1, &result);
return 1;
}
}
PointCalc.h
#ifndef POINT_CALC_H
#define POINT_CALC_H
#include "checks.h"
#define INT_FACTOR_TO_METER 1000
enum Return_codes {
RTN_NO_ERR = 0,
RTN_UNKNOWN,
RTN_OUTPUT_ERR,
RTN_MEMORY_ERR,
RTN_INPUT_ERR,
RTN_RANGE_ERR,
RTN_VALIDATION_ERR
};
typedef struct sPoint {
int x;
int y;
int z;
} sPoint;
int multiplyInteger(int a, int b, int *result);
int addPoints(sPoint const *a, sPoint const *b, sPoint *sum);
int subPoints(sPoint *minuend, sPoint *subtrahend, sPoint *diff);
int mulPoints(int factor, sPoint *Point, sPoint *product);
#endif
PointCalc.c
#include "PointCalc.h"
#include "checks.h"
#include "mathHelper.h"
#include <stdlib.h>
int multiplyInteger(int a, int b, int *result)
{
if (!result) return RTN_OUTPUT_ERR;
const long product = (long)a * b / INT_FACTOR_TO_METER;
if (abs(product) < 32000) {
*result = (int)product;
return RTN_NO_ERR;
}
else {
*result = sign(product) * 32000;
}
return RTN_RANGE_ERR;
}
我不知道这里出了什么问题。关于如何解决这个问题有什么建议吗?
您在具有默认 extern "C"
命名约定的 C 文件中定义函数。 .h中的函数必须是
extern "C" int multiplyInteger(int, int, int*);
或者整个头文件声明必须包含在
#ifdef __cpluplus
extern "C" {
#endif
...
#ifdef __cpluplus
}
#endif
当我尝试 link 应测试我的测试的代码时,我得到 undefined refrence
。
当我在同一个源文件中使用同一个函数时没有问题。
这里是link错误
g++ -o "VW_Test" ./tests/scaleSignalTests.o ./src/gtest-death-test.o ./src/gtest-filepath.o ./src/gtest-matchers.o ./src/gtest-port.o ./src/gtest-printers.o ./src/gtest-test-part.o ./src/gtest-typed-test.o ./src/gtest.o ./src/gtest_main.o ./VirtualWall/PointCalc.o ./VirtualWall/checks.o ./VirtualWall/mathHelper.o
./tests/scaleSignalTests.o: In function `TestBody':
C:\projects\VIRTUAL_WALL_TEST\Default/../tests/scaleSignalTests.cc:16: undefined reference to `multiplyInteger(int, int, int*)'
collect2.exe: error: ld returned 1 exit status
make: *** [VW_Test] Error 1
"make -j4 all" terminated with exit code 2. Build might be incomplete.
这是测试文件
#include "PointCalc.h"
#include "gtest/gtest.h"
#include <math.h>
namespace {
class scaledIntTest : public ::testing::Test {
};
TEST_F(scaledIntTest, Multiplication)
{
EXPECT_EQ(INT_FACTOR_TO_METER, 1000);
int result = 0;
// EXPECT_EQ(referencePoint, resultPoint);
EXPECT_EQ(RTN_NO_ERR,
multiplyInteger(INT_FACTOR_TO_METER * 1,
INT_FACTOR_TO_METER * 1,
&result));
EXPECT_EQ(INT_FACTOR_TO_METER, result);
}
int foo(void)
{
int result = 0;
multiplyInteger(INT_FACTOR_TO_METER * 1, INT_FACTOR_TO_METER * 1, &result);
return 1;
}
}
PointCalc.h
#ifndef POINT_CALC_H
#define POINT_CALC_H
#include "checks.h"
#define INT_FACTOR_TO_METER 1000
enum Return_codes {
RTN_NO_ERR = 0,
RTN_UNKNOWN,
RTN_OUTPUT_ERR,
RTN_MEMORY_ERR,
RTN_INPUT_ERR,
RTN_RANGE_ERR,
RTN_VALIDATION_ERR
};
typedef struct sPoint {
int x;
int y;
int z;
} sPoint;
int multiplyInteger(int a, int b, int *result);
int addPoints(sPoint const *a, sPoint const *b, sPoint *sum);
int subPoints(sPoint *minuend, sPoint *subtrahend, sPoint *diff);
int mulPoints(int factor, sPoint *Point, sPoint *product);
#endif
PointCalc.c
#include "PointCalc.h"
#include "checks.h"
#include "mathHelper.h"
#include <stdlib.h>
int multiplyInteger(int a, int b, int *result)
{
if (!result) return RTN_OUTPUT_ERR;
const long product = (long)a * b / INT_FACTOR_TO_METER;
if (abs(product) < 32000) {
*result = (int)product;
return RTN_NO_ERR;
}
else {
*result = sign(product) * 32000;
}
return RTN_RANGE_ERR;
}
我不知道这里出了什么问题。关于如何解决这个问题有什么建议吗?
您在具有默认 extern "C"
命名约定的 C 文件中定义函数。 .h中的函数必须是
extern "C" int multiplyInteger(int, int, int*);
或者整个头文件声明必须包含在
#ifdef __cpluplus
extern "C" {
#endif
...
#ifdef __cpluplus
}
#endif