多定义C ++ / C谷歌测试
multiple definition c++/c google test
所以我看到了类似的问题,我按照他们说的去做。我已经确保我的.h和.cpp文件在我的主测试文件中。
所以我真的不确定出了什么问题。我修复了这样的早期错误,但这是我抓住的。我感谢一些帮助。
矩阵乘法.h
Matrix-Multiply.h
#ifndef __MATRIX_MULTIPLY_H
#define __MATRIX_MULTIPLY_H
#include<stdio.h>
#include<stdlib.h>
float *expectedFinalMatrixOutput(int mA,int nA,int mB,int nB,float *matA,float *matB);
int readFiles(const char *matAFile,const char *matBFile);
#endif //__MATRIX_MULTIPLY_H
矩阵乘法.cpp
//.cpp file
#include<stdio.h>
#include<stdlib.h>
float *expectedFinalMatrixOutput(int mA,int nA,int mB,int nB,float *matA,float *matB)
{
int m = mA;
int n = nB;
int size = m * n;
float *finalMatrix[size];
//build both matrices
//for both the matA Column and matB Row need to be the
//same before even multiplying
//dot product matrix
//the end matrix needs the have the same number of rows as
//matA and same number of columns as matB
return *finalMatrix;
}
int readFiles(const char *matAFile,const char *matBFile)
{
int flag;
//read in file for matrixs
//set flag for whether true or false
//verify row and column being taken have actual values and
//that the sized are correct
return flag;
}
基质Multiply_unittests.cpp
// tests.cpp
#include "Matrix-Multiply.h"
#include "Matrix-Multiply.cpp"
#include<gtest/gtest.h>
#include<stdio.h>
TEST(matrixSize,emptyMatrix)
{
float *matA = NULL;
float *matB = NULL;
float *matrix =
expectedFinalMatrixOutput(0,0,0,0,matA,matB);
ASSERT_EQ(*(matrix),0);
}
TEST(dotTest,oneByoneMatrix)
{
float fMatrix[1] = {1};
float sMatrix[1] = {1};
float *matrix = expectedFinalMatrixOutput(1,1,1,1,fMatrix,sMatrix);
ASSERT_EQ(matrix[0],1);
}
TEST(dotTest,twoBytwoMatrix)
{
float fMatrix[4] = {1,1,1,1};
float sMatrix[4] = {1,1,1,1};
float *matrix = expectedFinalMatrixOutput(2,2,2,2,fMatrix,sMatrix);
for(int i =0;i<4;i++)
{
EXPECT_EQ(2,matrix[i]);
}
}
TEST(ReadFilesTest,filesExist)
{
const char *matA = "../src/Matrix1_3_3.txt";
const char *matB = "../src/Matrix2_3_3.txt";
ASSERT_EQ(0,readFiles(matA,matB));
}
TEST(ReadFilesTest,filesDontExist)
{
const char *matA = "../src/notReal.txt";
const char *matB = "../src/Matrix2_3_3.txt";
ASSERT_EQ(0,readFiles(matA,matB));
}
TEST(ReadFilesTest,matrixSizeNotCompatible)
{
const char *matA = "../src/Matrix1_3_3.txt";
const char *matB = "../src/Matrix2_2_2.txt";
ASSERT_EQ(0,readFiles(matA,matB));
}
int main(int argc,char **argv)
{
testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
抱歉,如果它不是全部在一行上。我试着这样想。但我得到的错误是:
obj/Matrix-Multiply.o: In function `expectedFinalMatrixOutput(int, int, int,
int, float*, float*)':
Matrix-Multiply.cpp:(.text+0x0): multiple definition of
`expectedFinalMatrixOutput(int, int, int, int, float*, float*)'
/tmp/ccUgZRUB.o:Matrix-Multiply_unittests.cpp:(.text+0x0): first defined
here
obj/Matrix-Multiply.o: In function `readFiles(char const*, char const*)':
Matrix-Multiply.cpp:(.text+0xbe): multiple definition of `readFiles(char
const*, char const*)'
/tmp/ccUgZRUB.o:Matrix-Multiply_unittests.cpp:(.text+0xbe): first defined
here
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
我正在使用谷歌测试,并且已经打电话进行gtest 但是,当我调用 make test 时会发生此错误。
任何帮助不胜感激
生成文件
CPP=g++
OBJ=obj
SRC=src
BIN=bin
CPPFLAGS=-I$(SRC)
GTEST_DIR=../googletest/googletest
gtest:
mkdir -p $(OBJ)
${CPP} -I${GTEST_DIR}/include -I${GTEST_DIR}
-pthread -c ${GTEST_DIR}/src/gtest-all.cc -o $(OBJ)/gtest-all.o
ar -rv $(OBJ)/libgtest.a $(OBJ)/gtest-all.o
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
$(OBJ)/gtest_main.o : $(GTEST_SRCS_)
$(CXX) $(CPPFLAGS) -I${GTEST_DIR}/include -I$(GTEST_DIR) $(CXXFLAGS) -c
$(GTEST_DIR)/src/gtest_main.cc -o $@
$(OBJ)/gtest_main.a : $(OBJ)/gtest-all.o $(OBJ)/gtest_main.o
$(AR) $(ARFLAGS) $@ $^
$(OBJ)/%.o: $(SRC)/%.cpp
$(CPP) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
.PHONY: test clean
test: $(OBJ)/Matrix-Multiply.o $(OBJ)/gtest_main.a
mkdir -p $(BIN)
$(CPP) -I${GTEST_DIR}/include $(SRC)/Matrix-Multiply_unittests.cpp
$(OBJ)/gtest_main.a $(OBJ)/Matrix-Multiply.o -o $(BIN)/Matrix-
Multiply_unittests -pthread
$(BIN)/Matrix-Multiply_unittests
clean:
rm -f $(BIN)/*
rm -f $(OBJ)/*
我认为通过#include "Matrix-Multiply.cpp"
,您将包含一次函数。
另外(虽然没有显示(,您正在链接行上与矩阵乘法链接。
通常不应包含.cpp
文件。 通过链接文件可以更好地绑定它们。
g++ -o my_awesome_app main.o Matrix-Multiply.o otherfile.o
相关文章:
- 谷歌测试中的期望值存储在哪里
- 更正 CMakeList.txt 用于谷歌测试的文件?
- 如何让谷歌测试正常运行。测试总是失败。(它不会编译)
- 谷歌测试方法与流
- 谷歌测试:模板模板的笛卡尔乘积的类型列表与模板
- C++ 谷歌测试除以零
- 多定义C ++ / C谷歌测试
- 使用谷歌测试进行测试时出现分段错误
- 如何使用谷歌测试检查两个枚举类元素的相等性?
- 谷歌测试集ArgReferee with vector<unique_ptr>
- 谷歌测试编译错误 Os X:函数式转换从"int"到"internal::FloatingEq2Matcher"的模糊转换<float>
- 谷歌测试发现在macOSX上添加并运行了我的所有测试两次
- 在谷歌测试中创建模拟类的向量
- 为什么在谷歌测试中调用"mkdtemp()"时失败
- 谷歌测试可执行文件中的单元测试未被vstest选中
- 谷歌测试:Android与Linux EXPECT_DEATH测试
- 谷歌测试比较指针数组的内容
- ptrdiff_t typedef 碰撞 - 谷歌测试和英特尔蟒蛇
- 我无法编译谷歌测试,告诉链接器输入未使用(macOS)
- EXPECT_CALL在谷歌测试中没有模拟