I'm trying to integrate Google Test into my Qt project. I have basic (EXPECT_EQ(1, 1))
tests running with Google Test, but am confused how I can integrate my Qt project (which uses qmake
) into the test's CMake build.
Google Test is currently added using the recommended CMakeLists.txt
from their README. I would like to add my local src and build to the test build. My src is located (relative to the test folder) ../src/
and I've built the src into (relative to the test folder) build/project-build/
but I believe it won't integrate because the project has .pro
's. The error when I try to integrate it as a subdirectory:
CMake Error at CMakeLists.txt:31 (add_subdirectory):
The source directory
/home/project/src
does not contain a CMakeLists.txt file.
This is how I am adding it as a subdirectory and project to my CMakeLists.txt
:
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/../../src
${CMAKE_CURRENT_BINARY_DIR}/project-build
EXCLUDE_FROM_ALL)
In a separate CMakeLists.txt.in
(similar to how google test does it):
include(ExternalProject)
ExternalProject_Add(project
GIT_REPOSITORY "${CMAKE_CURRENT_BINARY_DIR}/../.."
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/../../src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/project-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
Also, please let me know if this is a difficult way to test a project. I was hoping to use google test, but if this doesn't work will probably use Qt Test. I am fairly new to C++ and new to C++ unit testing (am familiar with Java and JUnit). Would appreciate any help, thank you!
When using add_subdirectory()
, you must have a CMakeLists.txt
file in the added source sub-directory (the first argument to the call). This directory typically contains the code files to be built, but they will be built by CMake, not qmake
. Because you are trying to mix two build approaches, there are a few options to try to simplify things:
qmake
afterwards. There is a decent example here documenting some of the steps in more detail.cmake
, instead of qmake
. There are some helpful answers here and here to demonstrate how convert qmake
projects to CMake projects. This way, CMake can handle the Google Test and local src builds.