[CMake] GTest confusion - linking to project being tested

Francis Giraldeau francis.giraldeau at gmail.com
Wed Apr 24 17:50:10 EDT 2019


Split your monolithic executable into a library and its main:

add_library(core core.cpp)
add_executable(main main.cpp)
target_link_libraries(main core)

Then, you can create gtest file and link to the core lib:

add_executable(test_core test_core.cpp)

target_link_libraries(test_core gtest gtest_main core)

add_test(NAME test_core COMMAND test_core)


IMHO other tricks will make your build system hackish and error prone.

You'd better keep the code and the tests in sync. The standard way is to
just put the tests beside the code that it is testing.

Francis


Le mer. 24 avr. 2019 à 17:38, cen <imbacen at gmail.com> a écrit :

> Hi
>
> I am essentially trying to solve this problem:
>
> https://stackoverflow.com/questions/19886397/how-to-solve-the-error-lnk2019-unresolved-external-symbol-function/30667584#30667584
>
> and I have hit a wall.
>
> Using CMake to generate solution with two VC projects, one is a
> monolithic .exe the other is a gtest project meant to test a few
> functions from the exe.
>
> I am having a problem linking to main project in gtest project because:
>
> 1. The project being tested is an exe, not a lib.
>
> 2. Ideally I don't want to have all h/cpp files pulled up in the gtest
> project, only have the actual test files in there.
>
> 3. Adding the main project as a reference to gtest project didn't help
> (suggestion from SO thread).
>
> 4. Manually adding main project .obj files in gtest as linker input
> solves the problem and is essentially the solution I would like to
> achieve with CMake.
>
> 5. I hit another unpleasant snafu after #4 because main and gtest
> project implement a main() method and this fails to build. But I guess
> this can be avoided by renaming
>
> the gtest main and changing the entry point of the gtest project.
>
>
> So if 5 is solvable, what I really need is a CMake solution to #4.. to
> automatically build the tested project and link to it's object files in
> gtest project.
>
>
> This seems to me to be a really obvious case for testing so I am not
> sure whether it really is that complicated or I am doing things wrong.
>
>
> Bets regards, cen
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190424/acfe5f53/attachment.html>


More information about the CMake mailing list