<div dir="ltr">Split your monolithic executable into a library and its main: <div><br><div>add_library(core core.cpp)</div><div>add_executable(main main.cpp)</div><div>target_link_libraries(main core)</div><div><br></div><div>Then, you can create gtest file and link to the core lib: </div><div><br></div><div><pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(128,128,0)">add_executable</span>(test_core<span style="color:rgb(192,192,192)"> </span>test_core.cpp)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(128,128,0)">target_link_libraries</span>(test_core<span style="color:rgb(192,192,192)"> </span>gtest<span style="color:rgb(192,192,192)"> </span>gtest_main<span style="color:rgb(192,192,192)"> </span>core)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(128,128,0)">add_test</span>(NAME<span style="color:rgb(192,192,192)"> </span>test_core<span style="color:rgb(192,192,192)"> </span>COMMAND<span style="color:rgb(192,192,192)"> </span>test_core)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre></div><div>IMHO other tricks will make your build system hackish and error prone.</div><div><br></div><div>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.</div><div><br></div><div>Francis</div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le mer. 24 avr. 2019 à 17:38, cen <<a href="mailto:imbacen@gmail.com">imbacen@gmail.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi<br>
<br>
I am essentially trying to solve this problem: <br>
<a href="https://stackoverflow.com/questions/19886397/how-to-solve-the-error-lnk2019-unresolved-external-symbol-function/30667584#30667584" rel="noreferrer" target="_blank">https://stackoverflow.com/questions/19886397/how-to-solve-the-error-lnk2019-unresolved-external-symbol-function/30667584#30667584</a><br>
<br>
and I have hit a wall.<br>
<br>
Using CMake to generate solution with two VC projects, one is a <br>
monolithic .exe the other is a gtest project meant to test a few <br>
functions from the exe.<br>
<br>
I am having a problem linking to main project in gtest project because:<br>
<br>
1. The project being tested is an exe, not a lib.<br>
<br>
2. Ideally I don't want to have all h/cpp files pulled up in the gtest <br>
project, only have the actual test files in there.<br>
<br>
3. Adding the main project as a reference to gtest project didn't help <br>
(suggestion from SO thread).<br>
<br>
4. Manually adding main project .obj files in gtest as linker input <br>
solves the problem and is essentially the solution I would like to <br>
achieve with CMake.<br>
<br>
5. I hit another unpleasant snafu after #4 because main and gtest <br>
project implement a main() method and this fails to build. But I guess <br>
this can be avoided by renaming<br>
<br>
the gtest main and changing the entry point of the gtest project.<br>
<br>
<br>
So if 5 is solvable, what I really need is a CMake solution to #4.. to <br>
automatically build the tested project and link to it's object files in <br>
gtest project.<br>
<br>
<br>
This seems to me to be a really obvious case for testing so I am not <br>
sure whether it really is that complicated or I am doing things wrong.<br>
<br>
<br>
Bets regards, cen<br>
<br>
-- <br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="https://cmake.org/mailman/listinfo/cmake" rel="noreferrer" target="_blank">https://cmake.org/mailman/listinfo/cmake</a><br>
</blockquote></div>