Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to start working with GTest and CMake
    primarykey
    data
    text
    <p>I have recently been sold on using CMake for compiling my C++ projects, and would now like to start writing some unit tests for my code. I have decided to use the Google Test utility to help with this, but require some help in getting started.</p> <p>All day I have been reading various guides and examples include the <a href="https://github.com/google/googletest/blob/master/googletest/docs/primer.md" rel="nofollow noreferrer">Primer</a>, an <a href="http://www.ibm.com/developerworks/aix/library/au-googletestingframework.html?ca=drs-" rel="nofollow noreferrer">introduction at IBM</a> and some questions on SO (<a href="https://stackoverflow.com/a/4222169/769220">here</a> and <a href="https://stackoverflow.com/a/5901489/769220">here</a>) as well as other sources I've lost track of. I realise there's plenty out there but somehow I am still having difficulties.</p> <p>I'm currently trying to implement the most basic test, to confirm I've compiled/installed gtest right and it's not working. The only source file (testgtest.cpp) is taken almost exactly from <a href="https://stackoverflow.com/a/4222169/769220">this</a> previous answer:</p> <pre><code>#include &lt;iostream&gt; #include "gtest/gtest.h" TEST(sample_test_case, sample_test) { EXPECT_EQ(1, 1); } </code></pre> <p>and my associated CMakeLists.txt is as follows:</p> <pre><code>cmake_minimum_required(VERSION 2.6) project(basic_test) # Setup testing enable_testing() find_package(GTest REQUIRED) include_directories(${GTEST_INCLUDE_DIR}) # Add test cpp file add_executable(runUnitTests testgtest.cpp ) # Link test executable against gtest &amp; gtest_main target_link_libraries(runUnitTests ${GTEST_LIBRARY_DEBUG} ${GTEST_MAIN_LIBRARY_DEBUG}) add_test( NAME runUnitTests COMMAND runUnitTests ) </code></pre> <p>Note that I have chosen to link against gtest_main instead of providing the main at the end of the cpp file as I believe this will allow me to scale testing up more easily to multiple files.</p> <p>When building the generated .sln file (in Visual C++ 2010 Express) I unfortunately get a long list of errors of the form</p> <pre><code>2&gt;msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: virtual __thiscall std::basic_iostream&lt;char,struct std::char_traits&lt;char&gt; &gt;::~basic_iostream&lt;char,struct std::char_traits&lt;char&gt; &gt;(void)" (??1?$basic_iostream@DU?$char_traits@D@std@@@std@@UAE@XZ) already defined in gtestd.lib(gtest-all.obj) </code></pre> <p>which I think means that I'm not successfully linking to the gtest libraries. I have made sure that when linking against the debug libraries, I have then tried to build in debug mode.</p> <p><strong>EDIT</strong></p> <p>Having done some more digging, I think my issue is something to do with the type of library I am building gtest into. When building gtest with CMake, if <code>BUILD_SHARED_LIBS</code> is un-checked, and I link my program against these .lib files I get the errors mentioned above. However, if <code>BUILD_SHARED_LIBS</code> is checked then I produce a set of .lib and .dll files. When now linking against these .lib files the program compiles, but when run complains that it can't find gtest.dll.</p> <p>What are the differences between a <code>SHARED</code> and a not <code>SHARED</code> library, and if I choose not shared, why doesn't it work? Is there an option in the CMakeLists.txt for my project that I am missing?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload