I've downloaded google test, but now I've no idea on how to link it to my project in eclipse. Should I add it as a source folder? Should include it as g++ included library? And how can I run test then?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- selenium+eclipse 打开网页时报错
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- Eclipse failing to open
- What exactly do pointers store? (C++)
You should not add it to your source folder, make separate folder instead. This is for avoidance of dependency of your production code from testing project. Do it like this
I usually use google test as two files, this is very handy. Use
scripts/fuse_gtest_files.py
from gtest distribution to extract them. Having only two files you can include their compilation in your test project compilation and have simple project structure.In your test projectspecify include directories
../contrib:../src:src
. Thus you can include headers like thistest/src/module1/class1Test.h:
test/src/mainTest.cpp:
I've tray your solution and it runs well. I can say that for compile gtest is not very clear in the README. txt.
I've run the makefile in the /make directory through a cygwin console. In my case the compiler advise me that don't findt the pthread library. So I modified the line
CXXFLAGS += -g -Wall -Wextra -pthread
and changed it to
CXXFLAGS += -g -Wall -Wextra -lpthread
The output I get is
gtest_main.a
. Then I have rename this file into libgtest.a and copy it toC:\cygwin\lib directory
.Then i've configure my eclipse project to use cygwin and added gtest and pthread as you say... and it works!
I hope it can help someone
Using Riga's excellent answer, here is a summary of how I got it to work:
./scripts/fuse_gtest_files.py . <project-dir>/contrib
contrib
directory from the Release build configuration, and added<project-dir>/contrib
to the include directories (odd, I know)src
directory and added a class namedFoo
(see below for the contents ofFoo.h
--I leftFoo.cpp
empty for now)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)Foo.h:
FooTest.cpp:
AllTests.cpp:
Here are the detailed steps:
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
contrib
folder, select Exclude from build...*, untick only the **Release box, and click OKcontrib
folder and select Properties > C/C++ Build > Settings > Tool Settings tab > GCC C++ Compiler > Directories<project-name>/contrib
and click OK to add the directorysrc
as its name, and click OKsrc
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)test
as its name, and click OK<project-name>/contrib
and<project-name>/src
as include directories to thetest
directorytest
folder, then select New > Source File to addAllTests.cpp
to thetest
folder, then repeat the same steps to addFooTest.cpp
(see above for contents)FooTest.cpp
and select Exclude from build..., click the Select All button, then OKHere is my solution for Eclipse 4.3 and CDT 8.2 I felt this was somewhat easier then described above.
Download gtest and install it as described in the readme.txt (using cmake and make in linux)
Go to "YourProject-> Properties-> C/C++ Build-> Settings-> GCC C++ Compiler-> Includes-> Include paths" and add the include folder in gtest
Go to "YourProject-> Properties-> C/C++ Build-> Settings-> GCC C++ Linker-> Libraries", add the gtest folder as search path and add libraries "gtest" and "pthread"
(4. If you have tests in the same project as sources exclude tests from release build)
Go to "Run-> Run Configurations..." and Create a new C/C++ Unit run configuration
Set project to your project and C/C++ Application to your Application in main tab. Set Tests Runner to Google Test Runner in C/C++ Testing tab.
(7. Error notifications may stick around in the eclipse gui, if this is the case re-indexing the project might help)
Step 1 Install Eclipse
If Eclipse is not already installed on the machine, then get the latest version of the Eclipse IDE for C/C++ Developers from the Eclipse downloads page (http://www.eclipse.org/downloads/).
If Eclipse is already installed but only for Java, download the C++ plug-in following these instructions.
a. Open Eclipse and click on Help->Install New Software
b. In the Work with: box, type in http://download.eclipse.org/tools/cdt/releases/kepler. After a few moments, the Name box will populate. Select the following components:
c. Click on Next, agree to the statements, and then Finish.
Step 2 Download Cygwin
Install Cygwin by clicking on the setup-x86_64.exe link on the Cygwin install page (http://www.cygwin.com/install.html). After running, click Next through the defaults until you get to the Select Packages window.
You will need to search for and install two packages: gcc and make.
The first search term is gcc. Search for gcc and then open the Devel folder. Mark the following packages for install by clicking on the word Skip (it will then change to the build number, which may by higher than the one depicted here): gcc-core, gcc-g++, and libgcc1.
The second search term is make. Here, we will only need the Devel package make.
Once these have been selected, click Next to install.
Step 3 Download and build Google Test project
Download the latest release of GoogleTest from https://code.google.com/p/googletest/downloads/list, and extract the zip file contents into a common directory. It is important that all users are able to access this directory.
To build the Google Test project:
cd c:/<<yourpath>>/gtest-1.7.0/make/
make
ar -rv libgtest.a gtest-all.o
Step 4 Add the Cygwin bin directory to the computers PATH variable
Follow the instructions on this page for your version of Windows: http://www.java.com/en/download/help/path.xml, to add Cygwins bin directory to the computers PATH environment variable. (typically by adding ;C:\cygwin64\bin to the end of the current value).
Step 5 Create a new project that uses GoogleTest
Start Eclipse and select File->New->C++ Project. Enter the values below and click Finish.
In the Project Explore, right-click the name of the project and select Properties. Under C/C++ Build, change the Builder type to Internal Builder.
Under C/C++ Build, select Settings, then click on the Includes folder under Cygwin C++ Compiler. Click on the Add button in the top box and then browse to the GoogleTest include folder.
Lastly, under the Cygwin C++ Linker folder, select Miscellaneous and then click the Add icon under Other objects. Find the libgtest.a file that you built back in step 3 (it should be in the make directory of the unzipped gtest folder).
Thats it! Now youre ready to try it out.
Step 6 Write some code that uses GoogleTest
Copy and paste the code below into the appropriate files:
Counter.h
Counter.cpp
Counter_Tests.cpp
In the Project menu select Build All. Now, to connect up the GoogleTest unit testing framework, select Run Configurations from the Run menu. From this dialog, select C/C++ Unit and click the New button.
It should fill in this project name automatically under C/C++ Application, if not click on Search Project to select this project. Next, click on the C/C++ Testing tab. In the Tests Runner drop-down, choose Google Tests Runner, and then click Run to watch the magic!
Below is a snapshot of the result. After writing more code/tests, you can click on the button highlighted in red to quickly recompile and re-run all of the tests.