Using Riga’s excellent answer, here is a summary of how I got it to work:
- Created a new C++ project in Eclipse (I chose Executable > Empty Project)
- Downloaded googletest 1.5.0, untarred, and ran
./scripts/fuse_gtest_files.py . <project-dir>/contrib
- Back in Eclipse, excluded the
contrib
directory from the Release build configuration, and added<project-dir>/contrib
to the include directories (odd, I know) - Added a
src
directory and added a class namedFoo
(see below for the contents ofFoo.h
–I leftFoo.cpp
empty for now) - Added a
test
directory in Eclipse, excluded it from the Release build configuration, added<project-dir>/contrib
to the include directories, and added new source filesFooTest.cpp
andAllTests.cpp
(see below for contents) - Built and ran the project!
Foo.h:
#ifndef FOO_H_
#define FOO_H_
class Foo {
public:
virtual ~Foo();
Foo();
bool foo(void) { return true; }
};
#endif /* FOO_H_ */
FooTest.cpp:
#include "gtest/gtest.h"
#include "Foo.h"
namespace {
class FooTest : public ::testing::Test {
protected:
Foo foo;
};
TEST_F(FooTest, Foo) {
ASSERT_TRUE(foo.foo());
}
}
AllTests.cpp:
#include "gtest/gtest.h"
#include "FooTest.cpp"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Here are the detailed steps:
- In Eclipse, open the File menu and select New > C++ Project
- Project Type: Executable > Empty Project
- Toolchain: Linux GCC
- Click Finish
- Open a terminal and
cd /tmp
wget http://googletest.googlecode.com/files/gtest-1.5.0.tar.bz2
cd gtest-1.5.0/
./scripts/fuse_gtest_files.py . <project-dir>/contrib
- Back in Eclipse, right-click on the project folder in the Project Explorer pane, then select Refresh
- In the Project Explorer pane, right-click on the
contrib
folder, select Exclude from build…*, untick only the **Release box, and click OK - Right-click on the
contrib
folder and select Properties > C/C++ Build > Settings > Tool Settings tab > GCC C++ Compiler > Directories - Click on the Add… button, then the Workspace… button, then select
<project-name>/contrib
and click OK to add the directory - Click OK once more to accept your changes to the build settings
- Right-click on the project in the Project Explorer pane and select New > Folder, enter
src
as its name, and click OK - Right-click on the
src
folder in the Project Explorer pane and select New > Class, name itFoo
, then click OK (see above for contents ofFoo.h
;Foo.cpp
can be left as is) - Right-click on the project in the Project Explorer pane and select New > Folder, enter
test
as its name, and click OK - Follow the steps above to add
<project-name>/contrib
and<project-name>/src
as include directories to thetest
directory - Right-click on the
test
folder, then select New > Source File to addAllTests.cpp
to thetest
folder, then repeat the same steps to addFooTest.cpp
(see above for contents) - Right-click on
FooTest.cpp
and select Exclude from build…, click the Select All button, then OK - Right-click on the project in the Project Explorer pane, and select Properties > C/C++ Build > Settings > Tool Settings tab > GCC C++ Linker > Libraries, then click the Add… button, enter pthread (required by googletest), click OK to add the library, then OK once more to accept the changes
- Hit Ctrl-b to build the project
- Hit Ctrl-F11 to run the project
- Victory!