I am new at using CUnit and writing a C program. I have this error printing when I run make
and then make test
:
/usr/bin/ld: test_errors.o: in function 'main':
test_errors.c:(.text+0x35): multiple definition of `main'; motcache.o:motcache.c:(.text+0x0): first defined here
/usr/bin/ld: test_file_handling.o: in function `main':
test_file_handling.c:(.text+0x96): multiple definition of `main'; motcache.o:motcache.c:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
make: *** [Makefile:21: link_test] Error 1
I have this in my Makefile :
CC = gcc
OPTIONS = -Wall -Wextra
EXE = motcache
SRC_DIR = src
TESTS_DIR = test
all : start
compile :
$(CC) $(OPTIONS) -c $(SRC_DIR)/*.c
test: run_test
link: compile
$(CC) *.o -o $(EXE)
compile_test :
$(CC) $(OPTIONS) -c $(TESTS_DIR)/*.c
link_test : compile_test
$(CC) $(OPTIONS) *.o -o cunit_tests -lcunit
run_test: link_test
./cunit_tests
clean:
rm -f $(EXE) *.o cunit_tests
start: link
./$(EXE)
This is my test_file_handling.c
that test a function that simply open a file :
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include "CUnit/CUnit.h"
#include "CUnit/Basic.h"
#include "../src/file_handling.h"
void test_open_file_success(void){
char *argv[] = {"./motcache", "mandoline.txt"};
FILE *fptr = open_file(argv);
CU_ASSERT_PTR_NOT_NULL(fptr);
fclose(fptr);
}
int main(){
CU_initialize_registry();
CU_pSuite suite = CU_add_suite("Ouverture de fichiers", NULL, NULL);
CU_add_test(suite, "Test d'ouverture de fichier avec succès", test_open_file_success);
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return 0;
}
And this is my main file motcache.c
:
#include <stdio.h>
#include "motcache.h"
#include "errors.h"
#include "file_handling.h"
int main(){
return 0;
}
I am struggling to solve this issue since I am still struggling to understand. Is it not allowed to have a main
method in each of my functions? My code works and the test work fine if the methods don't have the same name. I would be glad to take any advice or help in this issue. Thank you