примечание (этот проект создается с помощью CMakeLists.txt и Microsoft Visual Studio (ОС Windows):
Код: Выделить всё
[main] Building folder: fake_gtest_folder
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Documents/Projects/fake_gtest_folder/build --config Debug --target test_dummy_meta -j 10 --
[build] MSBuild version 17.10.4+10fbfbf2e for .NET Framework
[build]
[build] gtest.vcxproj -> C:\Documents\Projects\fake_gtest_folder\build\lib\Debug\gtest.lib
[build] PaCoLib.vcxproj -> C:\Documents\Projects\fake_gtest_folder\build\Sources\Debug\PaCoLib.lib
[build] gtest_main.vcxproj -> C:\Documents\Projects\fake_gtest_folder\build\lib\Debug\gtest_main.lib
[build] test_dummy_meta.cc
[build] test_dummy_meta.obj : error LNK2019: unresolved external symbol "void __cdecl addi(int,int)" (?addi@@YAXHH@Z) referenced in function "private: virtual void __cdecl MyCodeTest_ADD_Test::TestBody(void)" (?TestBody@MyCodeTest_ADD_Test@@EEAAXXZ) [C:\Users\pparika\Documents\Projects\fake_gtest_folder\build\tests\test_dummy_meta.vcxproj]
[build] Hint on symbols that are defined and could potentially match:
[build] addi
[build] C:\Documents\Projects\fake_gtest_folder\build\tests\Debug\test_dummy_meta.exe : fatal error LNK1120: 1 unresolved externals [C:\Documents\Projects\fake_gtest_folder\build\tests\test_dummy_meta.vcxproj]
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Documents/Projects/fake_gtest_folder/build --config Debug --target test_dummy_meta -j 10 -- exited with code: 1
[driver] Build completed: 00:00:01.715
[build] Build finished with exit code 1
Код: Выделить всё
Sources
| |addition.c
| |addition.h
| |CMakeLists.txt
tests
| |tests_dummy.cc
| |fff.h
| |CMakeLists.txt
CMakeLists.txt
Код: Выделить всё
#ifndef ADDITION_H
#define ADDITION_H
void addi(int a, int b);
void do_something(const char* str);
#endif // ADDITION_H
Код: Выделить всё
#include "addition.h"
#include
void addi(int a, int b) {
int result= a+b;
if(result>10){
do_something("Result is greater than 10");
}
}
void do_something(const char* str){
printf("%s\n",str);
}
Код: Выделить всё
#include
#include "addition.h"
#include "fff.h"
extern "C" {
#include "addition.h"
}
DEFINE_FFF_GLOBALS;
FAKE_VOID_FUNC(do_something, const char*);
TEST(MyCodeTest, ADD){
addi(10,3);
ASSERT_EQ(do_something_fake.call_count, 2);
RESET_FAKE(do_something);
}
Я пытался изменить файл CMakeLists.txt из папки тестов, пытался получить полный путь к файлу add.c и связать его с тестовым двоичным файлом
/tests/CMakeLists.txt:
Код: Выделить всё
enable_testing()
include(GoogleTest)
# Specify the directory where fff.h is located (relative to
CMakeLists.txt in tests directory)
set(FFF_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
# Include directories for the test executables
include_directories(
${CMAKE_CURRENT_SOURCE_DIR} # Include current directory for local includes
${FFF_INCLUDE_DIR} # Include directory containing fff.h
)
# Get the full path to addition.c
get_filename_component(ADDITION_C_FULL "${CMAKE_CURRENT_SOURCE_DIR}/../Sources/addition.c" ABSOLUTE)
# List of source files to link to the test binary
set(SOURCE_FILES ${ADDITION_C_FULL})
# Discover and add all test files
file(GLOB TEST_FILES "test_*.c" "test_*.cc")
foreach(TEST_FILE ${TEST_FILES})
# Extract the test name from the file name
get_filename_component(TEST_NAME ${TEST_FILE} NAME_WE)
# Add the test executable
add_executable(${TEST_NAME} ${TEST_FILE} ${SOURCE_FILES})
# Link against GoogleTest
target_link_libraries(${TEST_NAME}
GTest::gtest_main
EaCoLib
)
# Optionally include directories if needed
target_include_directories(${TEST_NAME}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../Sources
PUBLIC ${FFF_INCLUDE_DIR} # Include fff.h directory
)
#Add the test to CTest
gtest_discover_tests(${TEST_NAME})
# Add the test to CTest
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
endforeach()
set(CMAKE_C_FLAGS"${CMAKE_C_FLAGS}-DFFF_ENABLE_MOCKING")
Код: Выделить всё
# Add all C source files in the current directory
file(GLOB SOURCES *.c)
set(INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}
)
# Add the Lib target if needed
add_executable(EaCoExe ${SOURCES})
target_include_directories(EaCoExe PUBLIC ${INCLUDE_DIRS})
# Add the Lib target
add_library(EaCoLib ${SOURCES})
# Specify the include directories for the library
target_include_directories(PaCoLib PUBLIC ${INCLUDE_DIRS})
Подробнее здесь: https://stackoverflow.com/questions/787 ... ich-is-inc