Build/linker error for a C project using GoogleTest

  build, c++, cmake, linker

So I have a simple template for C projects that I have created, and have used it to implement the bowling game kata to shake out the bugs. The template is for a C project using C11, and it uses GTest as the testing framework.

I have got stuck with a linker issue when CMake is trying to build the test binary.

The CMakeLists.txt in the test directory is as follows

include(AddGoogleTest)

## Added these #########
include_directories("${PROJECT_SOURCE_DIR})/include")
set(FILES ../src/library.c ../include/template_demo/library.h)
##################

set(TEST_FILES test_template_demo.cpp)
add_executable(template-demo-test ${TEST_FILES} ${FILES})
add_gtest(template-demo-test)

Without the lines in the ‘added these’ block (as I originally had the file), I get an undefined reference to all of the functions I’m trying to call from my source code (located at src/library.c and include/template_demo/library.h).

So I added the lines in the ‘added these’ block, as I thought that the issue was the test binary couldn’t see the source code. With these lines in, the error changes to a no such file issue.

/projects/template_bowling/src/library.c:2:10: fatal error: template_demo/library.h: No such file or directory
    2 | #include "template_demo/library.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [tests/CMakeFiles/template-demo-test.dir/build.make:90: tests/CMakeFiles/template-demo-test.dir/__/src/library.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:1023: tests/CMakeFiles/template-demo-test.dir/all] Error 2
make: *** [Makefile:146: all] Error 2

The production binary is still built and runs OK as far as I can tell (it’s a minimal stub).

The test_template_demo.cpp has extern C around the include for the library.h.

Any help gratefully received!

Edit: The undefined reference error (the first one at least) is

/usr/bin/ld: CMakeFiles/template-demo-test.dir/test_template_demo.cpp.o: in function `BowlingTest_test_bowling_gutter_game_Test::TestBody()':
test_template_demo.cpp:(.text+0x42): undefined reference to `score_game'

The other errors are essentially the same, but when calling the other functions in the file.

The score_game function is defined in src/library.c, and has the following structure (implementations omitted for brevity)

#include <stdlib.h>
#include "template_demo/library.h"

#define MAX_ROLLS 21

struct bowling
{...};

struct bowling *bowling_init(void)
{...}

void bowling_free(struct bowling *b)
{
    free(b);
}

void roll_ball(struct bowling *b, int n)
{...}

int score_game(struct bowling *b)
{...}

The header that goes with the file is located at include/template_demo/library.h

#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
struct bowling;
struct bowling *bowling_init(void);
void bowling_free(struct bowling *b);
void roll_ball(struct bowling *b, int n);
int score_game(struct bowling *b);
#ifdef __cplusplus
}
#endif

Source: Windows Questions C++

LEAVE A COMMENT