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
contribdirectory from the Release build configuration, and added<project-dir>/contribto the include directories (odd, I know) - Added a
srcdirectory and added a class namedFoo(see below for the contents ofFoo.h–I leftFoo.cppempty for now) - Added a
testdirectory in Eclipse, excluded it from the Release build configuration, added<project-dir>/contribto the include directories, and added new source filesFooTest.cppandAllTests.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.bz2cd 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
contribfolder, select Exclude from build…*, untick only the **Release box, and click OK - Right-click on the
contribfolder 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>/contriband 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
srcas its name, and click OK - Right-click on the
srcfolder in the Project Explorer pane and select New > Class, name itFoo, then click OK (see above for contents ofFoo.h;Foo.cppcan be left as is) - Right-click on the project in the Project Explorer pane and select New > Folder, enter
testas its name, and click OK - Follow the steps above to add
<project-name>/contriband<project-name>/srcas include directories to thetestdirectory - Right-click on the
testfolder, then select New > Source File to addAllTests.cppto thetestfolder, then repeat the same steps to addFooTest.cpp(see above for contents) - Right-click on
FooTest.cppand 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!