Modify CMakeLists.txt for googletests in order to use googlemock - cmake

I was checking the quickstarter to use googletest with cmake. I was wondering how I should modify the CMakeLists.txt they provide (given below) in order to use googlemock.
cmake_minimum_required(VERSION 3.14)
project(my_project)
# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_executable(
hello_test
hello_test.cc
)
target_link_libraries(
hello_test
GTest::gtest_main
)
include(GoogleTest)
gtest_discover_tests(hello_test)
for the moment, #include <gtest/gtest.h> works but #include <gmock/gmock.h> doesn't

GMock should be available via GTest::gmock cmake target. At least this is the target name used in the cmake configuration files of a GTest build installed via cmake.
target_link_libraries(
hello_test
GTest::gmock
)

Related

cmake : Boost, plog and other packages not found. Packages installed via vcpkg

I am building a cross-platform project using cmake in Visual Studios 2022 Community Edition.
I am unable to include library headers files and linker files.
Error
1> [CMake] Could NOT find Boost (missing: Boost_INCLUDE_DIR)
Top-level CMake project file
# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)
# Set vcpkg library path
set(CMAKE_TOOLCHAIN_FILE C:/vcpkg/scripts/buildsystems/vcpkg.cmake)
project ("Test")
# Include sub-projects.
add_subdirectory ("Test")
CMake project for Test
# CMakeList.txt : CMake project for Test, include source and define
# project specific logic here.
#
# Set to C++20 standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#set(CMAKE_BUILD_TYPE debug)
# Set to C17 standard
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
#add library packages
set(Boost_USE_STATIC_LIBS ON) # only find static libs
set(Boost_USE_DEBUG_LIBS OFF) # ignore debug libs and
set(Boost_USE_RELEASE_LIBS ON) # only find release libs
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(BOOST_INCLUDEDIR "C:\\vcpkg\\installed\\x64-windows-static\\include\\boost")
find_package(Boost REQUIRED)
if(Boost_FOUND)
message(${Boost_VERSION_STRING})
include_directories(${Boost_INCLUDE_DIR})
target_link_libraries(Test ${Boost_LIBRARIES})
endif()
find_package(pystring REQUIRED)
find_package(plog REQUIRED)
# Add source to this project's executable.
add_executable (Test main.cpp common.cpp MqttClient.cpp Protocol.cpp SerialPort.cpp)
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET Test PROPERTY CXX_STANDARD 20)
endif()
# TODO: Add s and install targets if needed.

Using cmake.js with google test for testing a node.js addon

I am trying to test a node-js addon (built with cmake-js) with google test.
I am building on Mac OS.
The addon target builds and runs ok, but I have problems with the googletest target.
I am getting linking errors related to undefined V8 methods.
In CMakeLists.txt I printed (with message) the variables CMAKE_JS_SRC and CMAKE_JS_LIB and they are both empty. If CMAKE_JS_LIB is empty I don't see how target_link_libraries() should work to add the node/v8 library to my test executable...
If instead of using add_executable() I use add_library() the google_test target builds but of course I can not run it since it is not an executable anymore.
Can you help?
Below is my CMakeLists.txt:
make_minimum_required(VERSION 3.14)
include_directories(my_project)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_C_FLAGS "-Wno-pointer-sign -fno-signed-char -std=c99 -fpermissive")
set(CMAKE_CXX_FLAGS "-Wno-pointer-sign -fno-signed-char -std=c99 -fpermissive")
project (addon)
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1
)
FetchContent_MakeAvailable(googletest)
include_directories(${CMAKE_JS_INC} "my_project/include/pkcs11/v2.40/")
file(GLOB SOURCE_FILES "my_project/*.c" "my_project/*.h" "my_project/*.cpp")
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB} )
## google_test
enable_testing()
file(GLOB TEST_FILES "test/*.cc" "test/utils/*.c")
add_executable(
my_project_google_test
${TEST_FILES)
)
target_include_directories(my_project_google_test
PRIVATE
test/utils/*.h
)
target_link_libraries(
my_project_google_test
PRIVATE
GTest::gtest_main
${CMAKE_JS_LIB}
)
include(GoogleTest)
gtest_discover_tests(my_project_google_test)
CMAKE_JS_LIB is empty everywhere except on Windows.
Install v8 from Homebrew if you haven't done it yet: brew install v8.
Link the app to v8
target_link_libraries(
my_project_google_test
PRIVATE
v8
)
Other v8 libraries can be required, you have not post all errors: v8_libbase v8_libplatform.

CMake - How to get include directories of external project?

I tried to used https://github.com/julianxhokaxhiu/SteamworksSDKCI to use steam api on a simple SFML application (helloworld).
I wanted to use cmake to learn it, but I am struggling to understand how the provided CMakeLists and Find*.cmake file are expected to be used.
Currently, I have modified the CMakeLists to change the INSTALL_DIR
INSTALL_DIR "${CMAKE_BINARY_DIR}/../../vendor"
and my CMakeLists is :
cmake_minimum_required(VERSION 3.19)
project(SfmlWithCMake VERSION 1.0)
include(FetchContent)
set (CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake_steam")
# Configure external project
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/cmake_steam)
execute_process(
COMMAND ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR}/cmake_steam
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/cmake_steam
)
# Build external project
execute_process(
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/cmake_steam
)
set(BUILD_SHARED_LIBS OFF)
FetchContent_Declare(
SFML
GIT_REPOSITORY https://github.com/SFML/SFML.git
GIT_TAG 2.5.1
)
FetchContent_MakeAvailable(SFML)
find_package(STEAMWORKSSDK REQUIRED)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED true)
# Generate config.h
configure_file(config.h.in config.h)
add_executable(
SfmlWithCMake
main.cpp
)
get_target_property(STEAMSDK STEAMWORKSSDK::STEAMWORKSSDK INCLUDE_DIRECTORIES)
target_include_directories(
SfmlWithCMake
PRIVATE
"${PROJECT_BINARY_DIR}"
"${STEAMSDK}"
)
target_link_libraries(
SfmlWithCMake
sfml-graphics
STEAMWORKSSDK::STEAMWORKSSDK
-static gcc stdc++ winpthread -dynamic
)
install(TARGETS SfmlWithCMake DESTINATION bin)
How to get include directories?
I do not succeed to add the steam include to the target_include_directories.
Here the ${STEAMSDK} is my last attempt to get the directory.
If I replace this by ${PROJECT_BINARY_DIR}/vendor/include, everything works.
Also, why does the SFML include are automatically added to my target include directories and not the steam one?
Am I using the Find*.cmake file the right way?
I understood that ExternalProject_Add was performed at build time and thus, as the find_package is needed at configue time, I added the two "execute_process". But, the readme on github only says to do the find package and add the target to target_link_libraries...
Thanks.

how to split 'header', 'src', 'test' dir in cmake?(and donot add cmakelist.txt in each dir)

when building a project stucture like following:
LinearAlgebra
|---HEADER
|-----|---Linear.h
|---SRC
|-----|---Linear.cpp
|---TEST
|-----|---hello_test.cc
here is my CMakeList.txt:
cmake_minimum_required(VERSION 3.10)
project(LinearAlgebra )
set(CMAKE_CXX_STANDARD 11)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
add_library(LA_HEADER INTERFACE header/Linear.h)
add_library(LA_SRC INTERFACE src/Linear.cpp)
target_include_directories(LA_HEADER INTERFACE "${PROJECT_SOURCE_DIR}/header" "${PROJECT_SOURCE_DIR}/src")
enable_testing()
add_executable(
hello_test
test/hello_test.cc
)
target_link_libraries(
hello_test
gtest_main
LA_HEADER
LA_SRC
)
include(GoogleTest)
gtest_discover_tests(hello_test)
it works fine with cmake -S . -B build, but when it comes to cmake --build build following error occured:
Undefined symbols for architecture x86_64:
"Linear::Linear()", referenced from:
HelloTest_BasicAssertions_Test::TestBody() in hello_test.cc.o
it seems something wrong with my cmake file that didn't tell Linear.h where Linear.cpp located. So how to fix it ?? thanks for your help...
seems i misunderstand add_library, change three lines and it works out:
cmake_minimum_required(VERSION 3.10)
project(LinearAlgebra )
include_directories(header) # include_directories instead of add_library for .h files
add_library(LA_LIB src/Linear.cpp) # use add_library for .cpp files only!!!
set(CMAKE_CXX_STANDARD 11)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_executable(
hello_test
test/hello_test.cc
)
target_link_libraries(
hello_test
gtest_main
LA_LIB # link LA_LIB only..
)
include(GoogleTest)
gtest_discover_tests(hello_test)
leave it here for anyone have the same requirements.

Assimp with cmake

I want to include assimp to my project using CMake. I have Ubuntu 14.04 LTE and QTCreator.
Project contain of main.cpp and linked libraries stored in libs directory.
Main CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(test)
find_package(OpenGL REQUIRED)
# libs contain external libaries
add_subdirectory (libs)
include_directories(
libs/glfw-3.0.4/include
libs/assimp-3.1.1/include/
)
set(allLibs
${GLFW_LIBRARIES}
${OPENGL_LIBRARY}
GLEW_LIB
assimp
)
add_executable(test
main/main.cpp
)
target_link_libraries(Manipulator glfw assimp ${allLibs} )
CMakeList.txt inside libs directory
### GLFW ###
add_subdirectory (glfw-3.0.4)
include_directories(
glfw-3.0.4/include/GLFW/
glew-1.11.0/include/
assimp-3.1.1/include/
)
set(OPENGL_LIBRARY
-lGL -lGLU -lXrandr -lXext -lX11 -lrt
${CMAKE_DL_LIBS}
${GLFW_LIBRARIES}
)
### GLEW ###
set(GLEW_SOURCE
glew-1.11.0/src/glew.c
)
add_library( GLEW_LIB STATIC
${GLEW_SOURCE}
)
target_link_libraries(GLEW_LIB
${OPENGL_LIBRARY}
)
### ASSIMP ###
# Zlib
add_subdirectory( assimp-3.1.1/contrib/zlib )
# Boost workaround
include_directories( assimp-3.1.1/code/BoostWorkaround )
add_definitions( -DASSIMP_BUILD_BOOST_WORKAROUND )
# Compile AssImp
add_subdirectory( assimp-3.1.1/code )
And I receive following error.
CMake Error at libs/assimp-3.1.1/code/CMakeLists.txt:725 (INSTALL):
install TARGETS given no ARCHIVE DESTINATION for static library target
"assimp".
This point's me to this
INSTALL( TARGETS assimp # 725 line
LIBRARY DESTINATION ${ASSIMP_LIB_INSTALL_DIR}
ARCHIVE DESTINATION ${ASSIMP_LIB_INSTALL_DIR}
RUNTIME DESTINATION ${ASSIMP_BIN_INSTALL_DIR}
COMPONENT ${LIBASSIMP_COMPONENT})
How to correctly link this library?
This is a CMakeList.txt that I use to build my project.
All external libraries were stored in libs directory. Main file was stored in main directory.
cmake_minimum_required (VERSION 2.6)
project (Project name)
find_package(OpenGL REQUIRED)
#Adding external CMakeList.txt files
add_subdirectory(libs/glfw-3.1)
#add GLEW as STATIC library
add_library(GLEW STATIC libs/glew-1.11.0/src/glew.c libs/glew-1.11.0/include/GL/glew.h)
#add include directories
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
common/
libs/glfw-3.1/include/
libs/glew-1.11.0/include/
libs/glm/
libs/assimp-3.1.1/include/
libs/CImg_162/CImg.h
)
# set variables that are needed
set(ZLIB_LIBRARIES zlibstatic)
set(ENABLE_BOOST_WORKAROUND ON)
set(BUILD_STATIC_LIB ON)
set(BUILD_ASSIMP_TOOLS ON)
set(ASSIMP_BUILD_STATIC_LIB ON)
add_subdirectory(libs/assimp-3.1.1)
#Add my own liblary
add_subdirectory(common)
set(extLibs
${extLibs}
${GLFW_LIBRARIES}
)
add_executable (openGL main/main.cpp
Source/vertexShader.vs
Source/fragmentShader.fs
)
target_link_libraries(openGL common assimp GLEW glfw ${GLFW_LIBRARIES})
CMakelist.txt from Common looks like this:
add_library(common model.cpp)
This should work on Ubuntu 14.04. If some libraries are missing try to follow steps from this tutorial tutorial