I'm having a heck of a time understanding the CMake documentation. Most of my programming life has been in C#. So, this is new.
What I have is a directory structure like so:
- bin/
-- <empty>
- include/
-- cmocka.h
-- cmocka_pbc.h
-- cmocka_private.h
- lib/
-- libcmocka.dll.a
- src/
-- jargon/
| -- <*.c *.h>
- testing/
-- jargon_test/
| -- main.c (executable console)
| -- <*.c *.h>
I've been working on CMakeLists.txt as follows:
cmake_minimum_required(VERSION 3.20)
project(jargon_test C)
set(CMAKE_C_STANDARD 11)
add_executable(jargon_test main.c token_tests.h token_tests.c)
include_directories(
"../../src/jargon/"
"../../include"
)
add_library(cmocka SHARED IMPORTED)
set_target_properties(cmocka
PROPERTIES
IMPORTED_LOCATION "../../lib/libcmocka.dll.a"
INTERFACE_INCLUDE_DIRECTORIES "../../include/cmocka"
)
I'm using CLion 2021.2.3.
Trying to build, I get the following errors:
CMakeFiles\jargon_test.dir/objects.a(token_tests.c.obj): In function
test_hello': *directory*:29: undefined reference to
_assert_true`
CMakeFiles\jargon_test.dir/objects.a(token_tests.c.obj): In function
run_token_tests': *directory*:47: undefined reference to
_cmocka_run_group_tests`
The source itself has no issues. But, for completeness, here are the source files:
// token_tests.h
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>
#include "jargon.h"
#ifndef JARGON_TOKEN_TESTS_H
#define JARGON_TOKEN_TESTS_H
void test_hello(void ** state);
int setup(void ** state);
int teardown(void ** state);
int run_token_tests(void);
#endif //JARGON_TOKEN_TESTS_H
// token_tests.c
#include "token_tests.h"
void test_hello(void ** state) {
int ret = 0;
assert_true(ret == 0);
}
int setup (void ** state) {
printf("executing test\n");
return 0;
}
int teardown (void ** state) {
return 0;
}
int run_token_tests(void) {
const struct CMUnitTest tests [] = {
cmocka_unit_test(test_hello)
};
int count_fail_tests = cmocka_run_group_tests(tests, setup, teardown);
return count_fail_tests;
}