简体   繁体   中英

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.

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

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 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

// 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.

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.

Also (although not shown), you are linking with Matrix-Multiply on the link line.

You should not normally include a .cpp file. They are better bound by linking the file.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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