5

CMake test sources from different folders

 3 years ago
source link: https://www.codesd.com/item/cmake-test-sources-from-different-folders.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

CMake test sources from different folders

advertisements

I started playing around with CMake to create a project with Qt and test it with Google Test. At the moment, I succesfully found a way to compile and link all the required libraries. However, I couldn't find a way to link sources to test files with following project structure:

root
|
+-- CMakeLists.txt
+-- src
| |
| +-- CMakeLists.txt
| +-- MyClass.h
| +-- MyClass.cpp
|
+-- test
| |
| +-- CMakeLists.txt
| +-- MyClassTest.cpp
|
+-- lib
  |
  +-- gtest-1.6.0
    |
    +-- CMakeLists.txt

Root CMakeLists.txt contains add_subdirectory for gtest, src and test folders. I have succesfully compiled and run "Hello world" app and simple EXPECT_TRUE(true) test in order to check that each part compiles correctly. Unfortunately, I couldn't find a way to include my source file to tests. Is it possible with the following project structure?

PS I know that it is possible to compile my sources as a library and link it to tests, but I dislike that approach, since it is more appropriate for integration testing, rather then unit testing...

EDIT: Added class names to the tree


You can add a global variable at the level of your root CMakeLists.txt:

set(ALL_SRCS CACHE INTERNAL "mydescription" FORCE)

In the first add_subdirectory(src), you can do:

set(ALL_SRCS ${ALL_SRCS} blabla.cpp CACHE INTERNAL "description")

And in the add_subdirectory(test), you continue with:

set(ALL_SRCS ${ALL_SRCS} bla_test.cpp CACHE INTERNAL "description")

You can then do, add_executable, or library or whatever, with all your sources files.

EDIT: add trick for global variables in CMake.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK