Here's the summary for people already familiar with Eclipse:
- 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 .
/contrib - Back in Eclipse, excluded the contrib directory from the Release build configuration, and added
/contrib to the include directories (odd, I know) - Added a src directory and added a class named Foo (see below for the contents of Foo.h--I left Foo.cpp empty for now)
- Added a test directory in Eclipse, excluded it from the Release build configuration, added
/contrib to the include directories, and added new source files FooTest.cpp and AllTests.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 .
/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
/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 it Foo, then click OK (see above for contents of Foo.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
/contrib and/src as include directories to the test directory - Right-click on the test folder, then select New > Source File to add AllTests.cpp to the test folder, then repeat the same steps to add FooTest.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!