Multiple Definition

4,254 views
Skip to first unread message

Scott

unread,
Jan 29, 2011, 8:32:29 PM1/29/11
to Google C++ Testing Framework
This has got to be something simple, but I don't how to fix the
"multiple definition of ..." linking error I am getting. Perhaps I
just don't know how to use GoogleTest correctly yet. Here is a simple
project that exhibits the problem:

====main.cpp:
#include "gtest/gtest.h"

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

====Test1.cpp:
#include "gtest/gtest.h"
#include "Included.h"

namespace {
TEST(Awesome, awesome) {
SUCCEED();
}
}

====Test2.cpp:
#include "gtest/gtest.h"
#include "Included.h"

namespace {
TEST(Rawr, rawr) {
SUCCEED();
}
}

====Included.h:
#ifndef INCLUDED_H_
#define INCLUDED_H_

bool yes() {
return true;
}

#endif


This is a pretty vanilla project done in Eclipse to show the problem,
so Eclipse is handling the compiling. Here is the output:
make all
Building file: ../source/Test1.cpp
Invoking: GCC C++ Compiler
g++ -I"<cut>/gtestConflict/library/include" -I"<cut>/gtestConflict/
headers" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"source/
Test1.d" -MT"source/Test1.d" -o"source/Test1.o" "../source/Test1.cpp"
Finished building: ../source/Test1.cpp

Building file: ../source/Test2.cpp
Invoking: GCC C++ Compiler
g++ -I"<cut>/gtestConflict/library/include" -I"<cut>/gtestConflict/
headers" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"source/
Test2.d" -MT"source/Test2.d" -o"source/Test2.o" "../source/Test2.cpp"
Finished building: ../source/Test2.cpp

Building file: ../source/main.cpp
Invoking: GCC C++ Compiler
g++ -I"<cut>/gtestConflict/library/include" -I"<cut>/gtestConflict/
headers" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"source/
main.d" -MT"source/main.d" -o"source/main.o" "../source/main.cpp"
Finished building: ../source/main.cpp

Building target: gtestConflict
Invoking: GCC C++ Linker
g++ -L"<cut>/gtestConflict/library" -o"gtestConflict" ./source/
Test1.o ./source/Test2.o ./source/main.o -lgtest -lpthread
./source/Test2.o: In function `yes()':
/usr/include/c++/4.4/bits/ios_base.h:129: multiple definition of
`yes()'
./source/Test1.o:<cut>/gtestConflict/headers/Included.h:11: first
defined here
collect2: ld returned 1 exit status
make: *** [gtestConflict] Error 1

Joey Oravec

unread,
Jan 29, 2011, 11:35:14 PM1/29/11
to Scott, Google C++ Testing Framework
That's not a google test thing, it's a C/C++ thing. You defined the function in the *.h, which got included in two *.cpp files, which compiled into two instances of "bool yes()". When the compiler tried to link everything together it saw that function multiple times. That's a problem.

The solution is to declare the function in a *.h, but define it only once in a *.cpp file. Like this:

====Included.h
#ifndef INCLUDED_H_
#define INCLUDED_H_

// This declaration tells anybody who included this header
// that the function exists
bool yes();

#endif

====Included.cpp
#include <Included.h>

// This definition provides the code for that function
bool yes() {
return true;

Steve Jaffe

unread,
Jan 30, 2011, 12:43:47 AM1/30/11
to Joey Oravec, Scott, Google C++ Testing Framework
Or define it in the .h file as "inline"

Scott

unread,
Feb 1, 2011, 6:25:53 PM2/1/11
to Steve Jaffe, Joey Oravec, Google C++ Testing Framework
Ok, thanks guys. I got it working. Because I haven't read how gtest finds the tests without #include'ing them I didn't know if it was doing some magic that messed things up.

Scott

Josh Kelley

unread,
Feb 2, 2011, 10:05:23 PM2/2/11
to Scott, Google C++ Testing Framework
On Tue, Feb 1, 2011 at 6:25 PM, Scott <theer...@gmail.com> wrote:
Ok, thanks guys. I got it working. Because I haven't read how gtest finds the tests without #include'ing them I didn't know if it was doing some magic that messed things up.

How gtest finds tests without #include'ing them:  Under the hood, Google Test's TEST... macros create static objects whose constructors register themselves with the global list of tests.

--
Josh Kelley

Sissroh Coulibaly

unread,
May 11, 2018, 6:50:05 AM5/11/18
to Google C++ Testing Framework

Hello all,

Firts time with Gmock framework, I am trying to use simple example of google mock.
Is someone can tell me what I am doing wrong here ?

Thanks.

Here is my code in one file Test.h

class myInterface
{
public:
virtual ~myInterface(){}



virtual bool isReady(void) = 0;
};

class ConcreteImpl : public myInterface
{
public:
bool isReady(){return true;};
};

class MockInterface : public myInterface
{
public:
MOCK_METHOD0
(isReady, bool());
};


Here my Test.cpp file using BOOST LIB

class TestDriver
{
public:
TestDriver()
{
BOOST_TEST_MESSAGE
("execute TestSetup constructor");
};



~TestDriver()
{
BOOST_TEST_MESSAGE
("execute TestSetup destructor");
};

MockInterface mxDriver; // if I comment out this line, it is compiling.
};


when I compile, I am getting an error :

tests\test_src\build/googletest-src/googletest/src/gtest-port.cc:275: multiple definition of `testing::internal::Mutex::Lock()'



CMakeFiles/Test.dir/test_src/DriverTest.cpp.obj:C:tests\test_src\build/googletest-src/googletest/include/gtest/internal/gtest-port.h:2262: first defined here

collect2.exe: error: ld returned 1 exit status
Reply all
Reply to author
Forward
0 new messages