Pulling GTest as a third-party dependency with Conan.Using CMake’s fetch_content to pull GTest’s code.In this post, we look at three different ways to achieve this: Getting started with Google Tests in a CMake project is very easy. What’s even more interesting? Google Tests can be integrated quite easily into your CMake projects, using CMake’s test tool: CTest. In addition, it creates a very light-weight executable/program with beautiful pass/failure outputs based on your test outcomes. If you haven’t yet come across Google Test, it simply allows you to define test cases with its API. Figure 1: Building a GTest project and running the tests. Unsurprisingly, Google Test is the most popular testing framework for these purposes, and it can be easily integrated with CMake. For C++ software engineers, this means having many unit and functional tests that can tell you how your code behaves in certain situations. See my other post on symbol visibility if you want to dive deeper into this topic.During software development, it’s important to be able to test your code, so you can catch those pesky bugs as early as possible in the development process. Both GCC and Clang support this syntax.Īs a side note, with modern CMake versions > 3.0.2, you can auto generate a cross platform export definition using GenerateExportHeader 2 This is done by specifying _attribute_ ((visibility ("default")))įor the symbol in question. If you are hiding ELF symbols by default (which you should), this method has the drawback that symbols from private_impl.h you are testing need to be marked as exported, so the linker can find them when linking. Symbol Visibility (-fvisibility=hidden) 1 We furthermore explicitly express the link dependency between our and the corresponding. This way, we make sure that we use the content of private_impl.cpp in the right context. Get_target_property (private_link_libraries LINK_LIBRARIES) Get_target_property (private_include_dirs INCLUDE_DIRECTORIES) This can be done using CMakes property system, i.e., we can get the necessary properties from and reuse them for : add_executable( test.t.cpp) We simply need to access this information within our test environment. We can explicitly model the depency on the private implementation of, since we already specify all required information within the top level CMakeLists.txt. With the “patch” from before we now test private_impl.cpp in another context than it is normally used in. Second, oftentimes there exists some logic (within CMake or otherwise) that sets context (like macros) depending on the current component.First, the implementation in src/private_impl.cpp is used outside the original component, e.g., link dependencies may no longer be satisfied. This is a bad solution for several reasons. I have seen people do things, like including the relevant source file in their sources. When writing tests in test/test.t.cpp, how can we access private implementations from the private_include directory? The “pitfall” solution Target_include_directories ( PRIVATE include)Īnd the test/CMakeLists.txt like this: add_executable( test.t.cpp) Where the top-level CMakeLists.txt could look something like this: add_library( SHARED src/src.cpp src/private_impl.cpp) Consider a small example project with the following folder structure: When writing C++ code, I often find myself wanting to write tests for private implementation files.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |