Reputation: 538
I have a C project that I am trying to compile using CMake. This is my project structure:
root
├─ CMakeLists.txt
├─ docs
├─ build
└─ src
├─ main.c
├─ library1.h
├─ library2.h
├─ ...
The project needs unit tests for which I'm using CMocka, which is referenced in main.c
:
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
void test_example()
{
assert_true(1);
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_example),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
I've written a CMake file for compiling all the source files from into the build
directory, but it's not working properly:
make[2]: Entering directory '/home/radu/workspace/c/tls-server/build'
[ 50%] Building C object CMakeFiles/craking.dir/src/main.c.o
/usr/bin/cc -std=c99 -lcmocka -o CMakeFiles/craking.dir/src/main.c.o -c /home/radu/workspace/c/tls-server/src/main.c
[100%] Linking C executable craking
/usr/bin/cmake -E cmake_link_script CMakeFiles/craking.dir/link.txt --verbose=1
/usr/bin/cc -std=c99 -lcmocka -rdynamic CMakeFiles/craking.dir/src/main.c.o -o craking
/usr/bin/ld: CMakeFiles/craking.dir/src/main.c.o: in function `test_example':
main.c:(.text+0x1d): undefined reference to `_assert_true'
/usr/bin/ld: CMakeFiles/craking.dir/src/main.c.o: in function `main':
main.c:(.text+0x88): undefined reference to `_cmocka_run_group_tests'
collect2: error: ld returned 1 exit status
In the above, I've noticed that the parameter order is wrong:
/usr/bin/cc -std=c99 -lcmocka -rdynamic CMakeFiles/craking.dir/src/main.c.o -o craking
Whereas I think it should be:
/usr/bin/cc CMakeFiles/craking.dir/src/main.c.o -std=c99 -lcmocka -rdynamic -o craking
CMake seems to inverse the paramter order which causes problems with the linker, but I have no idea how to fix this. Here is my CMake file:
cmake_minimum_required(VERSION 3.0)
project(craking)
file(GLOB_RECURSE src
"src/*.h"
"src/*.c"
)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}-std=c99 -lcmocka")
add_executable(${PROJECT_NAME} src/main.c)
Any ideas?
Upvotes: 1
Views: 65