简体   繁体   English

多重定义 c++/c 谷歌测试

[英]multiple definition c++/c google test

So ive looked at similar issues and I followed what they said.所以我看了类似的问题,我按照他们说的去做。 I have made sure that my .h and .cpp file are in my main test file.我已经确保我的 .h 和 .cpp 文件在我的主测试文件中。

So I'm not really sure whats wrong.所以我不太确定出了什么问题。 I fixed an earlier error like this but it was something I caught.我修复了一个像这样的早期错误,但它是我发现的。 Id appreciate some help.我很感激一些帮助。

Matrix-Multiply.h矩阵乘法.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

Matrix-Multiply.cpp矩阵乘法.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;
}

Matrix-Multiply_unittests.cpp矩阵乘法_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();

}

Sorry if its not all on one line.对不起,如果它不是全部在一行上。 I tried to get it that way.我试图以这种方式得到它。 But the error i get is:但我得到的错误是:

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

Im using googletest and already make a call to make gtest However this error occurs when i call make test.我正在使用 googletest 并且已经调用 make gtest 但是当我调用 make test 时会发生这个错误。

Any help is appreciated任何帮助表示赞赏

Makefile生成文件

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)/*

I think by #include "Matrix-Multiply.cpp" you are including the functions once.我认为通过#include "Matrix-Multiply.cpp"您将函数包含一次。

Also (although not shown), you are linking with Matrix-Multiply on the link line.此外(虽然未显示),您正在链接线上与矩阵乘法链接。

You should not normally include a .cpp file.通常不应包含.cpp文件。 They are better bound by linking the file.通过链接文件可以更好地绑定它们。

g++ -o my_awesome_app main.o Matrix-Multiply.o otherfile.o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM