Last active
October 2, 2023 15:06
-
-
Save jpcofr/f4b6fb4264262564d0c956bb20f833ae to your computer and use it in GitHub Desktop.
Minimal C++/gtest cmake project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
build | |
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.25) | |
set(CMAKE_CXX_STANDARD 11) | |
project(c_cpp_snippet) | |
set(CMAKE_BUILD_TYPE Debug) | |
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g") | |
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g") | |
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) | |
# Find ccache | |
find_program(CCACHE_PROGRAM ccache) | |
if(CCACHE_PROGRAM) | |
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}") | |
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "${CCACHE_PROGRAM}") | |
endif() | |
## Configure testing | |
include(FetchContent) | |
set(FETCHCONTENT_QUIET TRUE) | |
# Download and configure googletest | |
FetchContent_Declare( | |
gtest | |
GIT_REPOSITORY https://github.com/google/googletest.git | |
GIT_TAG v1.13.0 | |
GIT_SHALLOW ON | |
) | |
FetchContent_GetProperties(gtest) | |
if(NOT gtest_POPULATED) | |
FetchContent_Populate(gtest) | |
add_subdirectory(${gtest_SOURCE_DIR} ${CMAKE_BINARY_DIR}/_deps/gtest-build) | |
endif() | |
include_directories(${gtest_SOURCE_DIR}/googletest/include) | |
## Configure executables | |
add_executable(test main.cpp) | |
target_link_libraries(test PRIVATE gtest_main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### This CMakeLists.txt fixes Google Test builds for some Linux installations. | |
### First issue: The build finishes but the binary does not execute correctly: | |
# | |
# /lib/lib64/libstdc++.so.6: version | |
# `GLIBCXX_3.4.21'/`CXXABI_1.3.9'/`GLIBCXX_3.4.29'/`GLIBCXX_3.4.26'/`GLIBCXX_3.4.21' | |
# not found (required by ./test) | |
# | |
# The fix is to set the CMAKE_INSTALL_RPATH and CMAKE_BUILD_WITH_INSTALL_RPATH variables as shown below. | |
# Also, add stdc++ to the target_link_libraries command. | |
# | |
# The main cause is that cmake cannot find the correct libstdc++ library. | |
# | |
### Second issue: The build fails with: | |
# | |
# undefined reference to symbol nextafter@@GLIBC_2.2.5 | |
# error adding symbols: DSO missing from command line | |
# | |
# The fix is to add m to the target_link_libraries command. | |
# The main cause is that m should be configured explicitly. | |
cmake_minimum_required(VERSION 3.25) | |
set(CMAKE_CXX_COMPILER "/path/to/gcc/version/bin/gcc") | |
set(CMAKE_INSTALL_RPATH "/path/to/gcc/version/lib64") | |
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) | |
set(CMAKE_CXX_STANDARD 11) | |
project(c_cpp_snippet) | |
set(CMAKE_BUILD_TYPE Debug) | |
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g") | |
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g") | |
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) | |
# Find ccache | |
find_program(CCACHE_PROGRAM ccache) | |
if(CCACHE_PROGRAM) | |
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}") | |
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "${CCACHE_PROGRAM}") | |
endif() | |
## Configure testing | |
include(FetchContent) | |
set(FETCHCONTENT_QUIET TRUE) | |
# Download and configure googletest | |
FetchContent_Declare( | |
gtest | |
GIT_REPOSITORY https://github.com/google/googletest.git | |
GIT_TAG v1.13.0 | |
GIT_SHALLOW ON | |
) | |
FetchContent_GetProperties(gtest) | |
if(NOT gtest_POPULATED) | |
FetchContent_Populate(gtest) | |
add_subdirectory(${gtest_SOURCE_DIR} ${CMAKE_BINARY_DIR}/_deps/gtest-build) | |
endif() | |
include_directories(${gtest_SOURCE_DIR}/googletest/include) | |
## Configure executables | |
add_executable(test main.cpp) | |
target_link_libraries(test PRIVATE stdc++ gtest gmock m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <gtest/gtest.h> | |
int sum (int a, int b) | |
{ | |
return a + b; | |
} | |
TEST(SumTest, PositiveNumbers) | |
{ | |
EXPECT_EQ(2, sum(1, 1)); | |
EXPECT_EQ(5, sum(2, 3)); | |
EXPECT_EQ(10, sum(5, 5)); | |
} | |
int main(int argc, char** argv) | |
{ | |
::testing::InitGoogleTest(&argc, argv); | |
return RUN_ALL_TESTS(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment