Manage targets with CMake and CLion - build

Hi I'm new to CMake and all that stuff.
I try to set up an Project with imgui and OpenGL.
My Project builds and works fine, but CLion shows all the GLFW examples and GoogleTests as Target.
I clone and build GLFW with these CMakeList:
CMakeLists.txt
find_package(OpenGL REQUIRED)
#download and build glew
configure_file(CMakeLists.txt.in glfw-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/glfw-download )
if(result)
message(FATAL_ERROR "CMake step for glfw failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/glfw-download )
if(result)
message(FATAL_ERROR "Build step for glfw failed: ${result}")
endif()
#add dir
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/glfw-src
${CMAKE_CURRENT_BINARY_DIR}/glfw-build
EXCLUDE_FROM_ALL)
add_executable(gbadbgCLI main.cpp)
#imgui
target_sources(gbadbgCLI PRIVATE imgui/imgui.cpp imgui/imgui_draw.cpp imgui/imgui_impl_opengl2.cpp imgui/imgui_impl_glfw.cpp imgui/imgui_widgets.cpp imgui/imgui_demo.cpp)
# link packages
target_link_libraries(gbadbgCLI gbadbgLIB)
target_link_libraries(gbadbgCLI OpenGL::GL)
target_link_libraries(gbadbgCLI glfw)
CMakeLists.txt.in
cmake_minimum_required(VERSION 3.10)
project(glfw-download NONE)
include(ExternalProject)
ExternalProject_Add(glfw PREFIX glfw
GIT_REPOSITORY https://github.com/glfw/glfw.git
GIT_TAG 3.3.2
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/glfw-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/glfw-build"
UPDATE_COMMAND ""
CMAKE_ARGS
"-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>"
"-DCMAKE_BUILD_TYPE=Release"
"-DGLFW_BUILD_EXAMPLES=OFF"
"-DGLFW_BUILD_TESTS=OFF"
"-DGLFW_BUILD_DOCS=OFF"
CMAKE_CACHE_ARGS
"-DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER}"
"-DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER}"
LOG_DOWNLOAD 1 LOG_UPDATE 1 LOG_CONFIGURE 1 LOG_BUILD 1 LOG_INSTALL 1
)
Is there an easy way I can remove this targets?

Sorry, it is known problem
CPP-14899 CLion creates "Application" run configurations for gtest targets when gtest 1.8.1 is used
But it has work around:
Please,
set the registry (Help | Find Action... | Actions | Registry...) value cidr.test.framework.targetTypeFromHeaderDetectionEnable to true ([v])
close project
remove .idea folder
open project

Related

CMake error "Automatic include detection via cmake could not be executed. See log for details."

I am trying to use the google test framework with my current eclipse project, which builds using CDT builder with cmake4eclipse 3.0.3. For including the googletest package, I used the following to download it as an external project within my project. The following is my CMakeLists.txt.in file:
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG main
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
And this is the CMakeLists.txt in which I actually build the google test and include it for my tests:
project(Myproject)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
##################### build google test #####################
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build)
#EXCLUDE_FROM_ALL)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
endif()
#######################################################################################
# Some irrelevant code here ...
#######################################################################################
############## Build my test ###################
add_executable(unit_test src/SomeUnitTest.cpp)
target_link_libraries(unit_test gtest gtest_main)
add_test(NAME big_test COMMAND unit_test)
My output is:
[cmake]: -- Configuring done
[cmake]: -- Generating done
[cmake]: -- Build files have been written to: C:/Users/Me/eclipse-workspace/Myproject/intermediate/cmake/AXCF2152,22.3.0.20/Release/googletest-download
[cmake]: [ 11%] [34m[1mPerforming update step for 'googletest'[0m
[cmake]: Current branch main is up to date.
[cmake]: [ 22%] [34m[1mNo configure step for 'googletest'[0m
[cmake]: [ 33%] [34m[1mNo build step for 'googletest'[0m
[cmake]: [ 44%] [34m[1mNo install step for 'googletest'[0m
Automatic include detection via cmake could not be executed. See log for details.
Successfully generated all files with the 'code' generator for C:\Users\Me\eclipse-workspace\Myproject.
I am using a Windows machine and as I mentioned above my eclipse is using the CDT builder with cmake4eclipse 3.0.3. When I query the cmake version it tells me it is 3.13.1. I do have another cmake version 3.23.2 installed on my machine as well.
I could not find any info on what the cmake error "Automatic include detection via cmake could not be executed. See log for details." means, I could also not find any relevant info about it in my CMakeOutput.log. What is going on?

How to resolve the dependency of glog on gtest during cmake configuration time?

So I am trying to use CMake to download and configure both gtest and then glog, for my project. When downloading them separately, compiling and linking, that works fine.
However, I would like to be able to configure the project accurately during the configuration time.
The problem is that glog during configuration time wants to find gtest, which is not build yet.
-- Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)
CMake Warning at CMakeLists.txt:78 (find_package): By not providing "Findgflags.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "gflags", but CMake did not find one.
The way I am currently doing is using the method mentioned here for both gtest 1st and glog 2nd, (https://crascit.com/2015/07/25/cmake-gtest/), in that order.
When I build glog when gtest is not found I get a bunch of errors like "error MSB3073 : The command setlocal", "error C2065: 'snprintf': undeclared identifier" and "error C2375: 'snprintf': redefinition; different linkage".
Please let me know if you have any advice, thank you.
Edit 1 :
gtest is version 1.11.0
glog is version 0.5.0
Edit 2 :
Adding a small code sample, to show to replicate the issue.
Below is the project structure.
Project
├───CMakeLists.txt (A)
└───external
├───CMakeLists.txt (B)
├───tester
| ├───CMakeLists_Google_Tester.in
| └───CMakeLists.txt (C)
└───logger
├───CMakeLists_Google_Logger.in
└───CMakeLists.txt (D)
Below is CMakeLists.txt (A).
cmake_minimum_required(VERSION 3.20.2)
project(SampleProject)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
include(CTest)
# External projects.
add_subdirectory(external)
Below is CMakeLists.txt (B).
cmake_minimum_required(VERSION 3.20.2)
add_subdirectory(tester)
add_subdirectory(logger)
Below is CMakeLists.txt (C).
cmake_minimum_required(VERSION 3.20.2)
project(ExternalTesterProject NONE)
configure_file(CMakeLists_Google_Tester.in external_tester_download/CMakeLists.txt)
execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/external_tester_download")
execute_process(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/external_tester_download")
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
add_subdirectory("${CMAKE_CURRENT_BINARY_DIR}/external_tester_src"
"${CMAKE_CURRENT_BINARY_DIR}/external_tester_build")
Below is CMakeLists.txt (D).
cmake_minimum_required(VERSION 3.20.2)
project(ExternalLoggerProject NONE)
configure_file(CMakeLists_Google_Logger.in external_logger_download/CMakeLists.txt)
execute_process(
COMMAND "${CMAKE_COMMAND}" . WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/external_logger_download")
execute_process(
COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/external_logger_download")
add_subdirectory("${CMAKE_CURRENT_BINARY_DIR}/external_logger_src"
"${CMAKE_CURRENT_BINARY_DIR}/external_logger_build")
Below is CMakeLists_Google_Logger.in .
cmake_minimum_required(VERSION 3.20.2)
project(ExternalLoggerProject NONE)
include(ExternalProject)
ExternalProject_Add(external_logger
GIT_REPOSITORY "https://github.com/google/glog.git"
GIT_TAG "v0.5.0"
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/external_logger_src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/external_logger_build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND "")
Below is CMakeLists_Google_Tester.in .
cmake_minimum_required(VERSION 3.20.2)
project(ExternalTesterProject NONE)
include(ExternalProject)
ExternalProject_Add(external_tester
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.11.0
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/external_tester_src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/external_tester_build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND "")

CMake Error: Unable to find package GTest [duplicate]

This question already has answers here:
How to start working with GTest and CMake
(11 answers)
Closed 2 years ago.
I am developing a C/C++ app using CMake. I want to use GTest in my project for unit testing. For this I have decided to use GTest as a git submodule in my repository.
My directory hierarchy is as follows:
/
--include
--lib
--GoogleTest
--src
--Tests
The GoogleTest subdirectory in lib contains the source code of GTest from their official repository.
But I am unable to use it to test my source. The CMakeLists.txt file at the root of my repository is as follows:
OPTION (BUILD_UNIT_TESTS "Build unit tests" ON)
if (BUILD_UNIT_TESTS)
enable_testing ()
find_package (GTest REQUIRED)
add_subdirectory (Tests)
endif ()
But I receive the error:
Error:Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR
GTEST_MAIN_LIBRARY)
When I searched for this, there were many questions similar to mine, but none of them addressed my problem. And their manual is very limited and does not tell much about its usage properly.
CMake is successfully able to build the target GTest but fails to recognise it when I try to use it as an external package.
You don't have to manually download the Gtest git. Just add the following lines to your CMakeLists.txt
# -------- GOOGLE TEST ----------
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
# -------------------------------------------------------------------------
enable_testing()
include_directories("${gtest_SOURCE_DIR}/include")
And add the following lines to another file, named CMakeLists.txt.in, in the same directory your CMakeLists.txt file is
cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.10.0
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
What does it do ?
The things you added to your main CMakeLists will download GTest and GMock git projects at the version specified in the CMakeLists.txt.in file. Then it will build these, and include the build path in your main project.
Source : GoogleTest Git

GoogleTest fails to run with 0xc0000135 when building with CMake under "WindowsStore" (UWP)

I'm trying to add gtest to an existing UWP project that is being built by CMake.
The build process seems fine and I'm able to create and build the project using:
cmake ../ -G "Visual Studio 15 2017 Win64" -T v141 -DCMAKE_SYSTEM_NAME=WindowsStore -DCMAKE_SYSTEM_VERSION=10.0.14393.0 -Dgtest_disable_pthreads=ON -DCMAKE_USE_WIN32_THREADS_INIT=ON
However when I run the test I get:
E:\Test-Gtest\build>ctest -C Debug
Test project E:/Test-Gtest/build
Start 1: example_test
1/1 Test #1: example_test .....................Exit code 0xc0000135
***Exception: 0.03 sec
0% tests passed, 1 tests failed out of 1
Total Test time (real) = 0.05 sec
The following tests FAILED:
1 - example_test (Exit code 0xc0000135
)
Errors while running CTest
Trying to run the executable example.exe results in:
VCRUNTIME140D_APP.dll was not found
followed by
MSVCP140D_APP.dll was not found
This is a link to a MCVE on github for convenience which has the following structure (mostly based on the guidelines in the docs):
│ CMakeLists.txt
│
└───src-test
│ CMakeLists.txt
│ CMakeLists.txt.in
│
└───src-integration
basic_logon.cpp
main CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(TestGtest CXX C)
set (CMAKE_CXX_STANDARD 14)
enable_testing()
add_subdirectory(src-test)
src-test CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# Now simply link against gtest or gtest_main as needed. Eg
message(STATUS "CMAKE_CURRENT_LIST_DIR: ${CMAKE_CURRENT_LIST_DIR}")
file(GLOB SRC_TEST_INTEGRATION_FILES ${CMAKE_CURRENT_LIST_DIR}/src-integration/*.cpp)
add_executable(example ${SRC_TEST_INTEGRATION_FILES})
target_link_libraries(example gtest_main)
add_test(NAME example_test COMMAND example)
CMakeLists.txt.in
cmake_minimum_required(VERSION 3.12)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.8.1
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
basic_logon.cpp
#include "gtest/gtest.h"
#include <cmath>
TEST(Pow, Two)
{
EXPECT_EQ(4, std::pow(2,2));
}

GTEST_LIB not found

I'm trying to use gtest into an existing cmake project. I'm following this steps:
CMakeLists.txt
project(myproject C CXX)
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
${CMAKE_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
find_library(GTEST_LIB gtest)
message(STATUS ${GTEST_LIB})
CMakeLists.txt.in
cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
And, I'm getting this message "GTEST_LIB-NOT FOUND".
Is there a way to fix this?
I've already tried the solutions mentioned here: How to start working with GTest and CMake. But, am facing the same issue.