将CppUTest连接到C接口

时间:2019-03-20 00:32:20

标签: c++ c cpputest

我正在使用CppUTest使用我的C库处理单元测试。但是,在编译测试文件时遇到问题。

这是我的MakefileCPPUTEST_HOME var是一个环境变量):

CPPFLAGS += -I$(CPPUTEST_HOME)/include
CXXFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorNewMacros.h
CFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorMallocMacros.h
LD_LIBRARIES = -L$(CPPUTEST_HOME)/lib -lCppUTest -lCppUTestExt

all:
        c++ -g -Wall $(CPPFLAGS) $(CXXFLAGS) $(CFLAGS) commonTest.cpp -o commonTest $(LD_LIBRARIES)

clean:
        rm -rf *.dSYM commonTest

当我运行make时,给出以下输出:

Undefined symbols for architecture x86_64:
  "_test_common_algos_commonCalloc_wrapper_c", referenced from:
      TEST_common_algos_commonCalloc_Test::testBody() in commonTest-992552.o
  "_test_common_algos_commonMalloc_wrapper_c", referenced from:
      TEST_common_algos_commonMalloc_Test::testBody() in commonTest-992552.o
  "_test_common_algos_commonRealloc_wrapper_c", referenced from:
      TEST_common_algos_commonRealloc_Test::testBody() in commonTest-992552.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1

在我看来,这似乎是一个链接问题。但是,根据the manual,我认为我没有做错任何事情:

commonTest.c

#include "../common.h"
#include "CppUTest/TestHarness_c.h"

void *ptr = NULL;
int list[10] = {1, 2, 32, 33, 2, 5, 7, 3, 9, 34};

TEST_GROUP_C(common_algos)
{

};

TEST_C(common_algos, commonMalloc)
{
  ptr = malloc_c(6);
  CHECK_EQUAL_C_INT(algos_error, ALGOS_OK);
  strncpy(ptr, "Hello", 5);
  CHECK_EQUAL_C_STRING("Hello", ptr);
  free_c(ptr);
}

TEST_C(common_algos, commonCalloc)
{
  ptr = calloc_c(sizeof(int), 10);
  CHECK_EQUAL_C_INT(algos_error, ALGOS_OK);
  memcpy(ptr, list, sizeof(int)*10);
  for (int i = 0; i < 10; i++)
      CHECK_EQUAL_C_INT(list[i], ptr[i]);
  free_c(ptr);
}

TEST_C(common_algos, commonRealloc)
{
  ptr = malloc_c(10);
  CHECK_EQUAL_C_INT(ALGOS_OK, algos_error);
  memcpy(ptr, list, sizeof(int)*10);
  ptr = realloc_c(ptr, 20);
  CHECK_EQUAL_C_INT(ALGOS_OK, algos_error);
  memcpy(ptr + 10, list, sizeof(int)*10);
  for (int i = 9; i < 19; i++)
      CHECK_EQUAL_C_INT(list[i-9], ptr[i]);
  free_c(ptr);
}

commonTest.cpp

#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTest/TestHarness_c.h"

TEST_GROUP_C_WRAPPER(common_algos)
{

};

TEST_C_WRAPPER(common_algos, commonMalloc);
TEST_C_WRAPPER(common_algos, commonCalloc);
TEST_C_WRAPPER(common_algos, commonRealloc);


int main(int ac, char **av)
{
 return RUN_ALL_TESTS(ac, av);
}

最后,我的目录结构:

common
  |______ common.c
  |
  |______ common.h
  |
  |______ test
           |
           |____commonTest.c
           |
           |____commonTest.cpp

在某些情况下,我不是C ++程序员,并且我没有使用CppUTest的经验,所以请多多包涵。

感谢您的帮助,随时可以提出问题。

0 个答案:

没有答案