Function defined twice? [SOLVED]

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
aether123
Posts: 18
Joined: Fri Jun 20, 2025 5:23 pm
Libera.chat IRC: aether123

Function defined twice? [SOLVED]

Post 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
Last edited by aether123 on Sat Jun 21, 2025 6:40 pm, edited 1 time in total.
Capybara Gymastics is one of the most prestigious Olympic sports
clementttttttttt
Member
Member
Posts: 72
Joined: Tue Jul 14, 2020 4:01 am
Libera.chat IRC: clementttttttttt

Re: Function defined twice?

Post by clementttttttttt »

You need to add header guards in utils.h, just add #pragma once in the beginning
aether123
Posts: 18
Joined: Fri Jun 20, 2025 5:23 pm
Libera.chat IRC: aether123

Re: Function defined twice?

Post 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
Capybara Gymastics is one of the most prestigious Olympic sports
aether123
Posts: 18
Joined: Fri Jun 20, 2025 5:23 pm
Libera.chat IRC: aether123

Re: Function defined twice?

Post 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 */
Capybara Gymastics is one of the most prestigious Olympic sports
clementttttttttt
Member
Member
Posts: 72
Joined: Tue Jul 14, 2020 4:01 am
Libera.chat IRC: clementttttttttt

Re: Function defined twice?

Post 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
aether123
Posts: 18
Joined: Fri Jun 20, 2025 5:23 pm
Libera.chat IRC: aether123

Re: Function defined twice?

Post 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
Capybara Gymastics is one of the most prestigious Olympic sports
aether123
Posts: 18
Joined: Fri Jun 20, 2025 5:23 pm
Libera.chat IRC: aether123

Re: Function defined twice?

Post by aether123 »

Do you know how I could fix this?
Capybara Gymastics is one of the most prestigious Olympic sports
aether123
Posts: 18
Joined: Fri Jun 20, 2025 5:23 pm
Libera.chat IRC: aether123

Re: Function defined twice?

Post by aether123 »

And when I'm compiling with my makefile, should I use the header files or the .c files?
Capybara Gymastics is one of the most prestigious Olympic sports
aether123
Posts: 18
Joined: Fri Jun 20, 2025 5:23 pm
Libera.chat IRC: aether123

Re: Function defined twice?

Post 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.
Capybara Gymastics is one of the most prestigious Olympic sports
Post Reply