GTest build first at CMake [duplicate] - c++

This question already has answers here:
How to start working with GTest and CMake
(11 answers)
Closed 6 years ago.
I'm building dynamic library and want to use gtest for testing.
SET(GTEST_LIBRARY libs/googletest-master)
set(GTEST_INCLUDE_DIR libs/googletest-master/googletest/include)
#set(GTEST_MAIN_LIBRARY libs/googletest-master/googletest/include/gtest)
#find_package(PostgreSQL REQUIRED)
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIR})
But, berofe setting GTEST_MAIN_LIBRARY I have to build it first.
How I can configure CMake to achieve this
Build gtest with Cmake && make (on unix)
Get appropriate path to GTEST_MAIN_LIBRARY
continue build

I copied gtests srcs into project
make a build with Make
and add this to CMake
add_subdirectory(libs/googletest-master)
SET(GTEST_LIBRARY libs/googletest-master)
set(GTEST_INCLUDE_DIR libs/googletest-master/googletest/include)
set(GTEST_MAIN_LIBRARY libs/googletest-master/googlemock/gtest)
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIR})
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

If your put the source for googletest in a subdirectory parented where your CMakeLists.txt file is, the following should work:
add_subdirectory(./googletest)
add_executable(your_program ${MY_SRC})
add_dependencies(your_program gmock)
add_dependencies(your_program gtest)

Related

Link restbed with Cmake [duplicate]

This question already has answers here:
How to add "-l" (ell) compiler flag in CMake
(2 answers)
Closed 2 years ago.
In trying to link restbed with CMake I get the usual undefined function error. However, trying the exact same code linked with g++ test.cpp -o test -lrestbed works fine.
Furthermore, when I first implemented the CMakeLists.txt it also worked fine and as I added to the project it started facing issues. Now even a single restbed function is not defined.
My restbed includes are located at /usr/local/include and the shared objects to link at /usr/local/lib. Pretty standard locations.
CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(vcar-server)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -lrestbed")
file(GLOB CXX_EXEC "src/*.cpp")
add_subdirectory(vcar-embedded)
add_executable(vcar-server ${CXX_EXEC})
target_link_libraries(vcar-server vcar)
Do not use CMAKE_CXX_FLAGS, or rather use it as a "last resort" when configuring the build system as a user. Prefer to use object model in place of global variables. The modern cmake way would most probably be something along:
cmake_minimum_required(VERSION 3.11)
project(vcar-server)
add_subdirectory(vcar-embedded)
find_package(Threads REQUIRED)
add_executable(vcar-server ${cxx_exec})
target_link_libraries(vcar-server PUBLIC vcar restbed Threads::Threads)
set_target_properties(vcar-server PUBLIC CXX_STANDARD 20)

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

Cmake error: Cannot specify link libraries for target [duplicate]

This question already has an answer here:
Build project with "experimental/filesystem" using cmake
(1 answer)
Closed 3 years ago.
I am trying to build a simple CMake project but I'm having problems understanding CMake or at least the error I get.
The project i made is divided in a couple directories.
The main one has this cmake:
cmake_minimum_required(VERSION 3.7.2)
project(Raytracer)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(src)
set(COMMON_INCLUDES ${PROJECT_SOURCE_DIR}/inc)
From there my project splits in 2 folders src and inc.
and in src i have the following cmake with the idea to global all subfolders:
FILE(GLOB sub-dirs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *)
FOREACH(dir ${sub-dirs})
IF(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${dir})
ADD_SUBDIRECTORY(${dir})
ENDIF()
ENDFOREACH()
add_executable(raytracer main.cpp)
I also add the executable there which is in this main src folder.
From there on i want to be able to make sub folders adding with their own Cmake files and linking those files to my executable.
I have the following cmake:
set(OBJECTS
asdf.cpp
)
add_library(obj_files ${OBJECTS} ${COMMON_FILES})
target_link_libraries(raytracer obj_files)
but when I try to build I get the following error:
Cannot specify link libraries for target "raytracer" which is not built by this project.
Basically in Cmake file or toolchain.cmake the order of commands are important!
target_link_libraries() or target_include_directories() have to be always after add_executable()

How can I make CMake download an external package and use it? [duplicate]

This question already has answers here:
Unknown CMake command "ExternalProject_Add"
(2 answers)
Closed 3 years ago.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(CMakeTest)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package(event-cmake REQUIRED)
file(GLOB SOURCES "*.cpp" )
add_executable(test ${SOURCES})
cmake/Findevent-cmake.cmake:
ExternalProject_Add(event-cmake
GIT_REPOSITORY https://github.com/libevent/libevent.git
UPDATE_COMMAND ""
INSTALL_COMMAND ""
)
I know that the CMakeLists here has no chance to include the resolved package, but I cannot even get CMake to download the external repo. It errors out with:
CMake Error at cmake/Findevent-cmake.cmake:3 (ExternalProject_Add):
Unknown CMake command "ExternalProject_Add".
Call Stack (most recent call first):
CMakeLists.txt:4 (find_package)
Is there a way to make it so that cmake will download the project and link to it?
While it is not directly written in docs, CMake functions described under cmake-modules section requires including specific module.
As function ExternalProject_Add is described in the documentation page titled as "ExternalProject", you need to use
include(ExternalProject)
before using it.
Same strategy is works for any other modules except Find<name> ones. Those modules are used via
find_package(<name>)

How to clone and integrate external (from git) cmake project into local one

I faced with a problem when I was trying to use Google Test.
There are lot of manuals on how to use ExternalProject_Add for the adding gtest into the project, however most of these describe a method based on downloading zip archive with gtest and build it.
As we know gtest is github-hosted and cmake-based project. So I'd like to find native cmake way.
If this would be a header-only project, I'd write something like:
cmake_minimum_required(VERSION 2.8.8)
include(ExternalProject)
find_package(Git REQUIRED)
ExternalProject_Add(
gtest
PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/ext
GIT_REPOSITORY https://github.com/google/googletest.git
TIMEOUT 10
UPDATE_COMMAND ${GIT_EXECUTABLE} pull
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
LOG_DOWNLOAD ON
)
ExternalProject_Get_Property(gtest source_dir)
set(GTEST_INCLUDE_DIR ${source_dir}/googletest/include CACHE INTERNAL "Path to include folder for GTest")
set(GTEST_ROOT_DIR ${source_dir}/googletest CACHE INTERNAL "Path to source folder for GTest")
include_directories(${INCLUDE_DIRECTORIES} ${GTEST_INCLUDE_DIR} ${GTEST_ROOT_DIR})
message(${GTEST_INCLUDE_DIR})
and add this script from my cmake project like:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake.modules/")
include(AddGTest)
....
add_dependencies(${PROJECT_NAME} gtest)
However this requires a build step.
How should this be implemented?
By adding BUILD_COMMAND into ExternaProject_Add and linking with produced libs?
Or by including gtest's cmakelists into my project, something like this:
add_subdirectory (${CMAKE_SOURCE_DIR}\ext\src\gtest\googletest\CMakeLists.txt)
this is not correct way because on the moment of the project load the folder does not exist.
Any other ways?
What is a correct/prefer way?
I would go with the first approach. You don't need to specify a build command because cmake is used by default. This could look like:
cmake_minimum_required(VERSION 3.0)
project(GTestProject)
include(ExternalProject)
set(EXTERNAL_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/external)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
)
include_directories(${EXTERNAL_INSTALL_LOCATION}/include)
link_directories(${EXTERNAL_INSTALL_LOCATION}/lib)
add_executable(FirstTest main.cpp)
add_dependencies(FirstTest googletest)
target_link_libraries(FirstTest gtest gtest_main pthread)
I don't know if this is the correct/preferred way if there even is one. If you wanted to implement your second approach you would have to download the code with execute_process first.