Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake configuration for multiple test files

Tags:

c

cmake

I'm running into following error. Basically I've two test .c files, how can I've them run both independently? Each one having it's own main function?

duplicate symbol _main in:

CMakeFiles/Tests.dir/test.c.o

CMakeFiles/Tests.dir/test2.c.o

Root CMakeLists.txt

cmake_minimum_required(VERSION 3.9)
project(Polymorphism)

set(CMAKE_C_STANDARD 99)

add_subdirectory(src)
add_subdirectory(test)

src/CMakeLists.txt

add_library(Polymorphism person.c employee.c)
target_include_directories(Polymorphism PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

test/CMakeLists.txt

add_executable(Tests test.c test2.c)
target_link_libraries(Tests Polymorphism)

test/test.c

void main() {
    // some tests
}

test/test2.c

void main() {
   // some tests
}
like image 482
App2015 Avatar asked Oct 16 '25 17:10

App2015


1 Answers

This is actually quite simple to solve. CMake allows you to create multiple executables (These can have their own main function) and also contains a function add_test used to bind these to tests.

So, given that you have files test.c and test2.c, each with their own main function:

add_executable(test1 test.c)
add_executable(test2 test2.c)

add_test(NAME t1 COMMAND test1)
add_test(NAME t2 COMMAND test2)

Now, you can run the commands make test or ctest, which will register two separate tests and run them as such.

like image 120
Arnav Borborah Avatar answered Oct 19 '25 05:10

Arnav Borborah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!