Home > Enterprise >  CSTDDEF file not found in GTest macOS
CSTDDEF file not found in GTest macOS

Time:03-26

I'm trying to create a project in C using Gtest for unit tests. For this, I have installed Xcode developer tools (because I'm in macOS big sur environment). and after this, I am cloning gtest and adding to submodules.

But, when I am trying to run test, the error appear :

googletest/googletest/include/gtest/gtest.h:52:10: fatal error: 'cstddef' file not found

After trying to reinstall Xcode tools, download the header to set it in my env, I have no more idea.

Do you have any idea to fix this or anybody had the same ?

Thank

PS : this is my CMakeLists

cmake_minimum_required(VERSION 3.0)
project(MasterDSA)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -o3")
add_library(Addition src/addition.c include/addition.h)
add_executable(Main src/main.c)
target_link_libraries(Main Addition)

set_target_properties(Main
    PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin")


set (gtest_force_shared_crt ON CACHE BOOL "MSVC defaults to shared CRT" FORCE)
add_subdirectory(googletest/googletest)
target_compile_definitions(gtest
  PUBLIC
    GTEST_LANG_CXX20
    GTEST_HAS_TR1_TUPLE=0
)
add_executable(tests)
target_sources(tests
  PRIVATE
    test/addition_test.c
)
set_target_properties(tests PROPERTIES COMPILE_FLAGS "${cxx_strict}")
target_link_libraries(tests gtest gtest_main Core)

CodePudding user response:

You are attempting to compile C code (in gtest.h) with a C compiler. (CMake chooses a C compiler for source files whose names end with .c.) C and C are different languages, not, generally speaking, source compatible in either direction. You must compile C with a C compiler, and C with a C compiler.

If considerable care is exercised then it is possible to write code in the shared subset of the two languages or that use the preprocessor to adapt to the chosen language, but this is uncommon, except sometimes for headers that declare functions and external objects having (from the C perspective) C linkage. That evidently is not how your gtest.h is written.

Your main options are:

  • convert your project to a C project. This has significant implications, so do not do it lightly.
  • write C tests for your C code. This may or may not be viable.
  • choose a C test framework instead of C -specific GTest.
  • Related