

CMake test sources from different folders
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.

CMake test sources from different folders
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
-
5
Adversarial cross-validation for train and test sets from different distributions Nov 6, 2018 Recently, I was working on the
-
5
John Fremlin's blog: Costs of energy from different sources by kWhWaiting for updates: connectedPosted 2014-04-24 15:01:00 GMTHow to compare the cost of energy? Each...
-
8
The WDD test with different pre/post time periods Eric Piza asked the other day if
-
15
Files Permalink Latest commit message Commit time
-
8
PHPStorm多项目下Test Sources Root设置最近用Lumen来做单元测试的时候遇到一个问题,就是用PHPStorm开了多个项目(用户侧和管理侧),这时候设置了Test Sources Root,会发现测试用例的命...
-
7
Chartbrew is an open-source web application that can connect directly to databases and APIs and use the data to create beautiful charts. It features a chart builder, editable dashboards, em...
-
11
One Exposure, which is the central storage for SAP Cash Management, storing the data (named cash flows) which forming the figures in Cash Position and Liquidity Analysis. It is quite a common and basic requirement to compare the figures fro...
-
8
Which sources of energy require the least amount of land? One part of the total land use is the space that a power plant takes up: the area of a coal power plant, or the...
-
2
Seamlessly Load Data from Different Data Sources into WPF ChartsThe Syncfusion WPF Charts control is optimize...
-
9
Meta open sources ImageBind AI model that combines six different data types...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK