Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode . Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

Hi there..
I tried to implement GTest into my current project. I installed GTest via msys2 (windows) under mingw64 mode.

therefore, i created this CmakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(TileGameStudio_Editor LANGUAGES CXX)
set(EDITOR_PROJECT ${PROJECT_NAME})
project(Tester LANGUAGES CXX)
set(TEST_PROJECT ${PROJECT_NAME})
#=============================================================== Setup CMake
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#=============================================================== Setup Paths
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(PROJ_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(PROJ_INC ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(PROJ_UI ${CMAKE_CURRENT_SOURCE_DIR}/ui)
set(PROJ_LIBS ${CMAKE_CURRENT_SOURCE_DIR}/libs)
set(PROJ_TESTS ${CMAKE_CURRENT_SOURCE_DIR}/tests)
list(APPEND CMAKE_AUTOUIC_SEARCH_PATHS ${PROJ_UI})
#=============================================================== Include Packages
find_package(QT NAMES Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Gui REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS OpenGL REQUIRED)
set(ANGLE_INCLUDE_PATH "D:/Angle/angle/include")
set(ANGLE_LIBRARY_PATH "${PROJECT_SOURCE_DIR}/libs")
INCLUDE(FindPkgConfig)
find_package(Freetype REQUIRED)
find_package(LuaJIT REQUIRED)
find_package(PNG REQUIRED)
find_package(FLAC REQUIRED)
find_package(OGG REQUIRED)
find_package(THEORA REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system filesystem json REQUIRED)
find_package(GTest REQUIRED)
find_library(GTest gtest)
#=============================================================== OUTPUT <LIB>_FOUND
if(NOT GTEST_FOUND)
  message(STATUS "GoogleTest:   NO")
else()
  message(STATUS "GoogleTest:   YES - under: ${GTEST_INCLUDE_DIRS}\n                 at Root: ${GTEST_ROOT}")
endif()
if(NOT FREETYPE_FOUND)
  message(STATUS "freetype2:   NO")
else()
  message(STATUS "freetype2:   YES - under: ${FREETYPE_INCLUDE_DIR_ft2build}")
endif()
if(NOT LUAJIT_FOUND)
  message(STATUS "LuaJit:   NO")
else()
  message(STATUS "LuaJit:   YES - under: ${LUA_INCLUDE_DIR}")
endif()
if(NOT PNG_FOUND)
  message(STATUS "libPNG:   NO")
else()
  message(STATUS "libPNG:   YES - under: ${PNG_INCLUDE_DIR}")
endif()
if(NOT FLAC_FOUND)
  message(STATUS "FLAC:   NO")
else()
  message(STATUS "FLAC:   YES - under: ${FLAC_INCLUDE_DIR}")
endif()
if(NOT OGG_FOUND)
  message(STATUS "OGG:   NO")
else()
  message(STATUS "OGG:   YES - under: ${OGG_INCLUDE_DIR}")
endif()
if(NOT THEORA_FOUND)
  message(STATUS "THEORA:   NO")
else()
  message(STATUS "THEORA:   YES - under: ${THEORA_INCLUDE_DIR}")
endif()
if(NOT OPENSSL_FOUND)
  message(STATUS "OpenSSL:   NO")
else()
  message(STATUS "OpenSSL:   YES - under: ${OPENSSL_INCLUDE_DIR}")
endif()
#=============================================================== Include Directories
include_directories(
    ${PROJ_LIBS}
    ${PROJ_INC}
    ${PROJ_SRC}
    ${PROJ_UI}
    ${OPENGL_INCLUDE_DIR}
    ${FREETYPE_INCLUDE_DIR_ft2build}
    ${LUA_INCLUDE_DIR}
    ${ANGLE_INCLUDE_PATH}
    ${ANGLE_LIBRARY_PATH}
    ${PNG_INCLUDE_DIR}
    ${FLAC_INCLUDE_DIR}
    ${OGG_INCLUDE_DIR}
    ${THEORA_INCLUDE_DIR}
    ${OPENSSL_INCLUDE_DIR}
    ${Boost_INCLUDE_DIRS}
    ${GTEST_INCLUDE_DIRS}
link_directories (
    ${PROJ_LIBS}
    ${PROJ_INC}
    ${PROJ_SRC}
    ${PROJ_UI}
    ${OPENGL_LIBRARY_DIRS}
    ${FREETYPE_INCLUDE_DIR_ft2build}
    ${LUA_INCLUDE_DIR}
    ${ANGLE_INCLUDE_PATH}
    ${ANGLE_LIBRARY_PATH}
    ${PNG_INCLUDE_DIR}
    ${FLAC_INCLUDE_DIR}
    ${OGG_INCLUDE_DIR}
    ${THEORA_INCLUDE_DIR}
    ${OPENSSL_INCLUDE_DIR}
    ${Boost_LIBRARY_DIRS}
    ${GTEST_INCLUDE_DIRS}
#=============================================================== Find GLES Libraries
find_library(GLES2_Lib NAMES d3dcompiler_47 REQUIRED)
find_library(D3D_Lib NAMES libGLESv2 REQUIRED)
#find_library(EGL_Lib NAMES libEGL EGL libegl REQUIRED)
#=============================================================== Collect Sources, Hearders and UI
file(GLOB PROJECT_SOURCES CONFIGURE_DEPENDS
    ${PROJ_SRC}/*.cpp
file(GLOB PROJECT_HEADERS CONFIGURE_DEPENDS
    ${PROJ_INC}/*.h
file(GLOB PROJECT_TESTS CONFIGURE_DEPENDS
    ${PROJ_TESTS}/*.cpp
file(GLOB PROJECT_UI CONFIGURE_DEPENDS
    ${PROJ_UI}/*.ui
file(GLOB PROJECT_RES CONFIGURE_DEPENDS
    ${PROJ_SRC}/*.qrc
#=============================================================== Build App
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(${EDITOR_PROJECT}
        ${PROJECT_SOURCES}
        ${PROJECT_HEADERS}
        ${PROJECT_UI}
        ${PROJECT_RES}
        ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
else()
    if(ANDROID)
        add_library(${EDITOR_PROJECT} SHARED
            ${PROJECT_SOURCES}
            ${PROJECT_HEADERS}
            ${PROJECT_UI}
            ${PROJECT_RES}
            ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
    else()
        add_executable(${EDITOR_PROJECT}
            ${PROJECT_SOURCES}
            ${PROJECT_HEADERS}
            ${PROJECT_UI}
            ${PROJECT_RES}
            ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
    endif()
endif()
add_executable(${TEST_PROJECT}
    ${PROJECT_SOURCES}
    ${PROJECT_HEADERS}
    ${PROJECT_TESTS}
enable_testing()
#=============================================================== Link Libraries
target_link_libraries(${EDITOR_PROJECT}
    PRIVATE Qt${QT_VERSION_MAJOR}::Core
    PRIVATE Qt${QT_VERSION_MAJOR}::Widgets
    PRIVATE Qt${QT_VERSION_MAJOR}::Gui
    PRIVATE Qt${QT_VERSION_MAJOR}::OpenGL
    ${OPENGL_gl_LIBRARY}
    ${FREETYPE_LIBRARIES}
    ${LUA_LIBRARY}
    ${GLES2_Lib}
    ${D3D_Lib}
    #${EGL_Lib}
    ${PNG_LIBRARY}
    ${FLAC_LIBRARY}
    ${OGG_LIBRARY}
    ${THEORA_LIBRARY}
    ${OPENSSL_CRYPTO_LIBRARY}
    ${OPENSSL_SSL_LIBRARY}
    ${OPENSSL_LIBRARIES}
    ${Boost_LIBRARIES}
    ${Boost_SYSTEM_LIBRARY}
    ${Boost_FILESYSTEM_LIBRARY}
target_link_libraries(${TEST_PROJECT}
    PRIVATE Qt${QT_VERSION_MAJOR}::Core
    PRIVATE Qt${QT_VERSION_MAJOR}::Widgets
    PRIVATE Qt${QT_VERSION_MAJOR}::Gui
    PRIVATE Qt${QT_VERSION_MAJOR}::OpenGL
    ${OPENGL_gl_LIBRARY}
    ${FREETYPE_LIBRARIES}
    ${LUA_LIBRARY}
    ${GLES2_Lib}
    ${D3D_Lib}
    #${EGL_Lib}
    ${PNG_LIBRARY}
    ${FLAC_LIBRARY}
    ${OGG_LIBRARY}
    ${THEORA_LIBRARY}
    ${OPENSSL_CRYPTO_LIBRARY}
    ${OPENSSL_SSL_LIBRARY}
    ${OPENSSL_LIBRARIES}
    ${Boost_LIBRARIES}
    ${Boost_SYSTEM_LIBRARY}
    ${Boost_FILESYSTEM_LIBRARY}
    ${GTEST_BOTH_LIBRARIES}
#=============================================================== Add GoogleTests
message("")
message("Adding Tests to ${TEST_PROJECT}")
message("")
gtest_add_tests(TARGET ${TEST_PROJECT} TEST_LIST outVar)
message("Tests were added to ${TEST_PROJECT}. The List:")
foreach(X ${outVar})
    message("-  ${X}")
endforeach()
message("")

The Cmake config output is:

GTest Libs are: GTest::gtest;GTest::gtest_main
-- GoogleTest:   YES - under:
                 at Root:
-- freetype2:   YES - under: C:/msys64/mingw64/include/freetype2
-- LuaJit:   YES - under: C:/msys64/mingw64/include/luajit-2.1
-- libPNG:   YES - under: C:/msys64/mingw64/include;C:/msys64/mingw64/include
-- FLAC:   YES - under: C:/msys64/mingw64/include
-- OGG:   YES - under: C:/msys64/mingw64/include
-- THEORA:   YES - under: C:/msys64/mingw64/include
-- OpenSSL:   YES - under: C:/Program Files/OpenSSL-Win64/include
Adding Tests to Tester
Tests were added to Tester. The List:
-  TestWindowMain.TestingTest
-- Configuring done
-- Generating done

The Tester main.cpp is simply:

#include <gtest/gtest.h>
int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();

And the only Tester source file beside the main is window_main.cpp:

#include <gtest/gtest.h>
#include <window_main.h>
TEST(TestWindowMain, TestingTest){
    EXPECT_EQ(0,0) << "Test succeeded!";
    EXPECT_EQ(0,1) << "Test failed!";

It includes the Project1s window_main.h and should run these two dummy tests..
But all i come up with, is this:

Both Tests end up fata with all the Paths of my Sytem Environment..

I´m not really advanced in doing tests.. this is my first time trying that stuff out.. so.. it would be nice if you cen help me here :D

Ps.: The Test (TestWindowMain.TestingTest) was added without a Problem.

@SGaist via installer (not repo).
The project is created via QT Creator as CMake Project.

These are the Initial Cmake Parameters:

-GNinja
-DCMAKE_BUILD_TYPE:String=Debug
-DQT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable}
-DCMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}
-DCMAKE_C_COMPILER:STRING=%{Compiler:Executable:C}
-DCMAKE_CXX_COMPILER:STRING=%{Compiler:Executable:Cxx}

It looks like you might be using two different compilers so I wonder if there's some bad interactions there

I think the conjecture by @SGaist likely has a lot of merit.

Here is some "general information" about Googletest. (I'm just the messenger! As the saying goes, don't shoot the messenger!)

Google developers have stated that they do not recommend using precompiled/prepackaged googletest binaries.

Instead, building googletest from source in a project you configure yourself is actually recommended by Google. And for certain kinds of more elaborate tests, I have been bitten by not complying. (strange linker errors was usually the result, if I recall)

Moreover, it is important that you compile Googletest with the same compiler and compiler flags as the test you are writing.

https://github.com/google/googletest/issues/2184

Building it yourself is fairly straightforward. Just list all the googletest *cc files in a makefile of whatever style you prefer (qmake, CMake, gnu make, vcxproj) and build as usual.

Here is a (working) example of this in qmake fashion:

https://github.com/219-design/qt-qml-project-template-with-ci/blob/22788b057/third_party/googletest-release-1.8.0/googletest/googletest.pro

It looks like you might be using two different compilers so I wonder if there's some bad interactions there

I think the conjecture by @SGaist likely has a lot of merit.

Here is some "general information" about Googletest. (I'm just the messenger! As the saying goes, don't shoot the messenger!)

Google developers have stated that they do not recommend using precompiled/prepackaged googletest binaries.

Instead, building googletest from source in a project you configure yourself is actually recommended by Google. And for certain kinds of more elaborate tests, I have been bitten by not complying. (strange linker errors was usually the result, if I recall)

Moreover, it is important that you compile Googletest with the same compiler and compiler flags as the test you are writing.

https://github.com/google/googletest/issues/2184

Building it yourself is fairly straightforward. Just list all the googletest *cc files in a makefile of whatever style you prefer (qmake, CMake, gnu make, vcxproj) and build as usual.

Here is a (working) example of this in qmake fashion:

https://github.com/219-design/qt-qml-project-template-with-ci/blob/22788b057/third_party/googletest-release-1.8.0/googletest/googletest.pro

It looks like you might be using two different compilers so I wonder if there's some bad interactions there

I think the conjecture by @SGaist likely has a lot of merit.

Here is some "general information" about Googletest. (I'm just the messenger! As the saying goes, don't shoot the messenger!)

Google developers have stated that they do not recommend using precompiled/prepackaged googletest binaries.

Instead, building googletest from source in a project you configure yourself is actually recommended by Google. And for certain kinds of more elaborate tests, I have been bitten by not complying. (strange linker errors was usually the result, if I recall)

Moreover, it is important that you compile Googletest with the same compiler and compiler flags as the test you are writing.

https://github.com/google/googletest/issues/2184

Building it yourself is fairly straightforward. Just list all the googletest *cc files in a makefile of whatever style you prefer (qmake, CMake, gnu make, vcxproj) and build as usual.

Here is a (working) example of this in qmake fashion:

https://github.com/219-design/qt-qml-project-template-with-ci/blob/22788b057/third_party/googletest-release-1.8.0/googletest/googletest.pro

@KH-219Design ok.. i´ve no clue how to build an integrate gtest into my project.. all that cmd stuff and that google manual is more cryptic to me than helpful...

I really need help here..

I cloned the Repo,
in powershell:

$ cd googletest
$ mkdir gtest_build
$ cd gtest_build
$ cmake ..

And now?

@BDC_Patrick

So... I cannot possibly blame you for the approach that you took (which is essentially to follow the Googletest manual for how to do their equivalent of "make; make install"). That was a sane assumption on your part.

However, that was not what I actually intended to recommend. (Nor what I have done to make googletest work for me in my C++ projects).

What I was saying is to get all the source code for googletest, then create your own Makefile from scratch (be it a qmake pro file, or a CMakeLists.txt, or a vcxproj). Just take the C++ source files from googletest and write a library buildfile in your own preferred Makefile system to build those C++ source files.

In other words, you imagine that you have just personally created all these sources (gtest-all.cc, gtest-death-test.cc, gtest-filepath.cc, etc), and you do "whatever you normally do after writing a C++ class" in terms of adding that C++ class/file to your build.

Do you know how: (a) to use CMake to compile a shared library (either a DLL or an *.so)?

And then after that, do you know how to (b) use CMake to compile an exe that links against the shared library from step (a)?

Knowing about (a) and (b) and prerequisites to the path forward that I am suggesting. So perhaps I got ahead of myself by not making sure you are confident in how to do (a) and (b).

@BDC_Patrick

So... I cannot possibly blame you for the approach that you took (which is essentially to follow the Googletest manual for how to do their equivalent of "make; make install"). That was a sane assumption on your part.

However, that was not what I actually intended to recommend. (Nor what I have done to make googletest work for me in my C++ projects).

What I was saying is to get all the source code for googletest, then create your own Makefile from scratch (be it a qmake pro file, or a CMakeLists.txt, or a vcxproj). Just take the C++ source files from googletest and write a library buildfile in your own preferred Makefile system to build those C++ source files.

In other words, you imagine that you have just personally created all these sources (gtest-all.cc, gtest-death-test.cc, gtest-filepath.cc, etc), and you do "whatever you normally do after writing a C++ class" in terms of adding that C++ class/file to your build.

Do you know how: (a) to use CMake to compile a shared library (either a DLL or an *.so)?

And then after that, do you know how to (b) use CMake to compile an exe that links against the shared library from step (a)?

Knowing about (a) and (b) and prerequisites to the path forward that I am suggesting. So perhaps I got ahead of myself by not making sure you are confident in how to do (a) and (b).

(a) no
(b) not yet

But i think i found a way, to fetch the latest repo of googletest and include it. That worked well..

Still got a FATAL Problem at the Test Result Window, which seems to be a QT Creator Problem, since it isn´t able to run Tests.exe, but gives out (correct) Test Results. More, the Tests.exe can be started (via a batch that adds the QT dll location to PATH) and gives a perfect Powershell output of all Test.

@BDC_Patrick said in QT + Cmake + GTest = Test end up to be fatal:

it isn´t able to run Tests.exe, but gives out (correct) Test Results

what the h...??

how could anything output correct test results without running Tests.exe??

It seems like you are making progress little by little, so I am happy for you! I'll take slow and tiny progress over no progress any day :)

@SGaist The Error says:

But as i said, The Test run though completely and there is no such FATAL Error in Powershell, cmd or the Console-View of QT Creator Test Result.

No FATAL Error here...
Test runn Through

Btw.. The Batch i run the Tests.exe from:

set PATH=C:\Qt\5.15.2\mingw81_64\bin;%PATH% start PowerShell -NoExit -Command "[Path\To\Build\Folder]\Tests.exe"

I think you have an interpretation error here. Your tests executable ran successfully so to say. The fatal here means that one or more of your tests failed.

If it had crashed you would not see it in the report panel but you would have a dialog stating that there was a crash.

I think you have an interpretation error here. Your tests executable ran successfully so to say. The fatal here means that one or more of your tests failed.

If it had crashed you would not see it in the report panel but you would have a dialog stating that there was a crash.