Code Monkey home page Code Monkey logo

gtest-cmake-example's Introduction

What is this?

This is an example setup of cmake with google test. I got it working after consulting the google test primer, among other things. Hopefully it'll help someone get set up with C++ and google test.

Where are things?

build/ is where code is built - like where executables are.
lib/ includes gtest-1.6.0.
Rest of code in root:
-CMakeLists.txt must be in each subdirectory of the project
-main.cpp is just a driver file - the common place to run the normal application
-project1.cpp and project1.h have code for the class 'Project1'
-test_project1.cpp has code to test Project1

What do I do?

Use cmake and make

If you want to test it all out through the common gtest procedure, first delete build/ (if present). Then...

In the project root:

mkdir build
cd build
cmake ..

By now Makefiles should be created. Then, to build executables and do all that linking stuff,

make

To prepare all your tests, run this:

cmake -Dtest=ON ..

To run all tests easily,

make test

Run executables

Then you can do ./myexecutable for the generated executable, e.g.:

./project1

and if you did cmake with test=ON:

./runUnitTests

Details

Command things

cmake -Dtest=ON turns on the variable 'test', which is specified in the root CMakeLists.txt file. This is handy if you want to build in certain ways. Clear description here.

My experience

I spent a lot of time figuring out how I wanted to reference the gtest library.

On StackOverflow there are ways to download the svn repo when you cmake, but that's internet connection dependent. There is also a way to set a variable for where gtest is installed on your system, but that got a bit tedious.

In the end I opted to just have a copy of gtest locally in whatever application I have that uses gtest.
This user's edited solution and my own question's response helped me out.

gtest-cmake-example's People

Contributors

dmonopoly avatar feiwofeifeixiaowo avatar weliveindetail avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gtest-cmake-example's Issues

How to add files from subdirectories ?

I was trying to extend this, by adding a directory and a cpp file in that directory and a test_*.cpp file in that directory and add it.

I used the add_subdirectory(another_dir) directive, and inside that, I added lines

1 include_directories(${CMAKE_SOURCE_DIR})
2 include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
3 add_library(another_dir test_project2.cpp)

It compiles fine.

On the toplevel CMakeLists.txt I added, target_link_library(.... , another_dir).

This was not linking the library from the subdirectory.

Can you update this git-repo on how to handle subdirectories.

can't find the test

hi,

following the steps you outline i end up with

โžœ  build git:(master) make test
Running tests...
Test project /Users/florian.oswald/git/gtest-cmake-example/build
    Start 1: that-test-I-made
Could not find executable /Users/florian.oswald/git/gtest-cmake-example/build/runUnitTests
Looked in the following places:
/Users/florian.oswald/git/gtest-cmake-example/build/runUnitTests
/Users/florian.oswald/git/gtest-cmake-example/build/runUnitTests
/Users/florian.oswald/git/gtest-cmake-example/build/Release/runUnitTests
/Users/florian.oswald/git/gtest-cmake-example/build/Release/runUnitTests
/Users/florian.oswald/git/gtest-cmake-example/build/Debug/runUnitTests
/Users/florian.oswald/git/gtest-cmake-example/build/Debug/runUnitTests
/Users/florian.oswald/git/gtest-cmake-example/build/MinSizeRel/runUnitTests
/Users/florian.oswald/git/gtest-cmake-example/build/MinSizeRel/runUnitTests
/Users/florian.oswald/git/gtest-cmake-example/build/RelWithDebInfo/runUnitTests
/Users/florian.oswald/git/gtest-cmake-example/build/RelWithDebInfo/runUnitTests
/Users/florian.oswald/git/gtest-cmake-example/build/Deployment/runUnitTests
/Users/florian.oswald/git/gtest-cmake-example/build/Deployment/runUnitTests
/Users/florian.oswald/git/gtest-cmake-example/build/Development/runUnitTests
/Users/florian.oswald/git/gtest-cmake-example/build/Development/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/Release/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/Release/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/Debug/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/Debug/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/MinSizeRel/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/MinSizeRel/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/RelWithDebInfo/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/RelWithDebInfo/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/Deployment/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/Deployment/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/Development/runUnitTests
Users/florian.oswald/git/gtest-cmake-example/build/Development/runUnitTests
Unable to find executable: /Users/florian.oswald/git/gtest-cmake-example/build/runUnitTests
1/2 Test #1: that-test-I-made .................***Not Run   0.00 sec
    Start 2: that-other-test-I-made
Could not find executable runUnitTests
Looked in the following places:
runUnitTests
runUnitTests
Release/runUnitTests
Release/runUnitTests
Debug/runUnitTests
Debug/runUnitTests
MinSizeRel/runUnitTests
MinSizeRel/runUnitTests
RelWithDebInfo/runUnitTests
RelWithDebInfo/runUnitTests
Deployment/runUnitTests
Deployment/runUnitTests
Development/runUnitTests
Development/runUnitTests
Unable to find executable: runUnitTests
2/2 Test #2: that-other-test-I-made ...........***Not Run   0.00 sec

0% tests passed, 2 tests failed out of 2

Total Test time (real) =   0.00 sec

The following tests FAILED:
      1 - that-test-I-made (Not Run)
      2 - that-other-test-I-made (Not Run)
Errors while running CTest
make: *** [test] Error 8

any ideas?

Makefile is not created

I was trying to run this sample. But after cmake .. command, no makefile is created thus unable to run make command. Please help?
I am new to this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.