Page 1 of 1

Function defined twice? [SOLVED]

Posted: Sat Jun 21, 2025 5:05 pm
by aether123
I am getting this error when running make, and am not sure where the other function definition is, as the only function definition for memset and vga_print I can see are in utils.c. Sorry if this is a really dumb question, but this is really confusing me

The error:

Code: Select all

i386-elf-gcc -ffreestanding -m32 -g -c src/kernel/kernel.c -o build/kernel.o
In file included from src/kernel/gdt/gdt.c:3,
                 from src/kernel/kernel.c:4:
src/kernel/gdt/../utils/utils.c:4:6: error: redefinition of 'memset'
    4 | void memset(void *dest, char val, uint32_t count) {
      |      ^~~~~~
In file included from src/kernel/kernel.c:3:
src/kernel/utils/utils.c:4:6: note: previous definition of 'memset' with type 'void(void *, char,  uint32_t)' {aka 'void(void *, char,  long unsigned int)'}
    4 | void memset(void *dest, char val, uint32_t count) {
      |      ^~~~~~
src/kernel/gdt/../utils/utils.c:11:6: error: redefinition of 'vga_print'
   11 | void vga_print(const char *str) {
      |      ^~~~~~~~~
src/kernel/utils/utils.c:11:6: note: previous definition of 'vga_print' with type 'void(const char *)'
   11 | void vga_print(const char *str) {
      |      ^~~~~~~~~
make: *** [Makefile:21: kernel] Error 1 
The project files: https://github.com/i-love-winter/SolsticeOS

Re: Function defined twice?

Posted: Sat Jun 21, 2025 5:09 pm
by clementttttttttt
You need to add header guards in utils.h, just add #pragma once in the beginning

Re: Function defined twice?

Posted: Sat Jun 21, 2025 5:11 pm
by aether123
Sorry, I don't fully understand what you mean. Do you mean literally just add the line #pragma to the start of utils.h? Because doing that doesn't seem to change anything

Re: Function defined twice?

Posted: Sat Jun 21, 2025 5:18 pm
by aether123
Or do you mean like this?

Code: Select all

#ifndef memset
#define memset
#ifndef vga_print
#define vga_print

#include <stdint.h>

void memset(void *dest, char val, uint32_t count);

void vga_print(const char *str);

#endif /* memset */
#endif /* vga_print */

Re: Function defined twice?

Posted: Sat Jun 21, 2025 5:20 pm
by clementttttttttt
Okay, upon further inspection it seems like you're directly including source files(.c) in kernel.c instead of including the headers(.h). You have to include the headers, instead of the source files. And yes, put #pragma once at the start of your header files, forget about the ifndefs

Re: Function defined twice?

Posted: Sat Jun 21, 2025 5:25 pm
by aether123
Thankyou so much for your help! Except now I'm getting this error instead :(

Code: Select all

i386-elf-gcc -ffreestanding -m32 -g -nostdlib -nostartfiles -Ttext 0x1000 -o build/gdt.o build/gdts.o src/kernel/gdt/gdt.c
/usr/lib/gcc/i386-elf/15.1.0/../../../../i386-elf/bin/ld: warning: cannot find entry symbol _start; defaulting to 00001000
/usr/lib/gcc/i386-elf/15.1.0/../../../../i386-elf/bin/ld: /tmp/cc4KL3Hl.o: in function `writeTSS':
/home/arch/SolsticeOS/src/kernel/gdt/gdt.c:41:(.text+0x109): undefined reference to `memset'
collect2: error: ld returned 1 exit status
make: *** [Makefile:14: includes] Error 1

Re: Function defined twice?

Posted: Sat Jun 21, 2025 5:31 pm
by aether123
Do you know how I could fix this?

Re: Function defined twice?

Posted: Sat Jun 21, 2025 5:46 pm
by aether123
And when I'm compiling with my makefile, should I use the header files or the .c files?

Re: Function defined twice?

Posted: Sat Jun 21, 2025 5:50 pm
by aether123
It looks like that was my problem, I was using the c files instead of the header files in my makefile and includes, sorry for bothering you.