Reputation: 2080
When I try to link the code that shall be tested to my tests I get undefined refrence
.
When I use the same function in the same sourcefile there is no problem.
Here the linker error
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.
this is the test file
#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;
}
I am at a loss what is going wrong here. Any suggestions on how to fix this?
Upvotes: 0
Views: 52
Reputation: 38425
You define the function in a C file, that has default extern "C"
naming convention. The function in .h must be
extern "C" int multiplyInteger(int, int, int*);
Or the entire header file declarations must be enclosed in
#ifdef __cpluplus
extern "C" {
#endif
...
#ifdef __cpluplus
}
#endif
Upvotes: 1