Undefined reference to memset [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

Undefined reference to memset [SOLVED]

Post by aether123 »

I'm trying to use the memset function from utils.h, but it seems like gcc might not be detecting it?

Error:

Code: Select all

i386-elf-gcc -ffreestanding -m32 -g -nostdlib -nostartfiles -Ttext 0x1000 -o build/gdt.o build/gdts.o build/gdtc.o
/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: build/gdtc.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:15: includes] Error 1
Project files: https://github.com/i-love-winter/SolsticeOS/tree/main
Last edited by aether123 on Sat Jun 21, 2025 10:47 pm, edited 1 time in total.
Capybara Gymastics is one of the most prestigious Olympic sports
Octocontrabass
Member
Member
Posts: 5873
Joined: Mon Mar 25, 2013 7:01 pm

Re: Undefined reference to memset

Post by Octocontrabass »

You can't combine two object files that way. If you need a single file that contains both object files, create a static library with i386-elf-ar. Otherwise, delete that line in your makefile and link both object files separately into your final kernel binary.
nullplan
Member
Member
Posts: 1908
Joined: Wed Aug 30, 2017 8:24 am

Re: Undefined reference to memset

Post by nullplan »

Your Makefile is very broken. You should really model the actual dependencies, not write pseudo batch files. There is also this linking step in the middle that I don't understand: Why are you linking gdtc.o and gdts.o into a single executable file? It is possible that you actually wanted a relocatable link (-r) here, but again, why? Why not just put gdtc.o and gdts.o on the final linker command line?

It is this middle linker step that is failing.
Carpe diem!
aether123
Posts: 18
Joined: Fri Jun 20, 2025 5:23 pm
Libera.chat IRC: aether123

Re: Undefined reference to memset

Post by aether123 »

I used to have it like this: i386-elf-gcc -ffreestanding -m32 -g -nostdlib -nostartfiles -Ttext 0x1000 -o build/gdt.o src/kernel/gdt/gdt.c src/kernel/gdt/gdt.s

But then I started getting errors about it not being compiled correctly:

Code: Select all

src/kernel/gdt/gdt.s: Assembler messages:
src/kernel/gdt/gdt.s:1: Error: no such instruction: `global gdt_flush'
src/kernel/gdt/gdt.s:4: Error: operand size mismatch for `mov'
src/kernel/gdt/gdt.s:7: Error: operand size mismatch for `mov'
src/kernel/gdt/gdt.s:8: Error: operand size mismatch for `mov'
src/kernel/gdt/gdt.s:9: Error: operand size mismatch for `mov'
src/kernel/gdt/gdt.s:10: Error: operand size mismatch for `mov'
src/kernel/gdt/gdt.s:11: Error: operand size mismatch for `mov'
src/kernel/gdt/gdt.s:12: Error: operand size mismatch for `mov'
src/kernel/gdt/gdt.s:14: Error: junk `:.flush' after expression
src/kernel/gdt/gdt.s:19: Error: no such instruction: `global tss_flush'
src/kernel/gdt/gdt.s:22: Error: operand size mismatch for `mov'
make: *** [Makefile:13: includes] Error 1
So I'm not sure what to do here, I'm very puzzled
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: Undefined reference to memset

Post by aether123 »

Oh, thankyou nullplan, I think I get it now! I've been so confused, and somehow never considered linking the files separately, sorry for wasting your time
Capybara Gymastics is one of the most prestigious Olympic sports
nullplan
Member
Member
Posts: 1908
Joined: Wed Aug 30, 2017 8:24 am

Re: Undefined reference to memset

Post by nullplan »

That command line still tries to perform an executable link. And it still does not make sense to me. It is actually very simple:
  • If the command line contains a -c switch, it compiles to an object file.
  • If it contains a -r switch, it will perform a relocatable link (i.e. combine object files to another object file).
  • If it contains a -shared, it will perform a shared lib link (i.e. combine object files to a shared library).
  • And if it contains none of these, it will perform an executable link.
The reason you are getting those messages is because gdt.s is written for nasm, and you are passing it to GCC, which attempts to compile it with gas, which has a different syntax. But again, you can just add gdts.o and gdtc.o to the linker command line for the main kernel, and get rid of building gdt.o altogether.
Carpe diem!
aether123
Posts: 18
Joined: Fri Jun 20, 2025 5:23 pm
Libera.chat IRC: aether123

Re: Undefined reference to memset [SOLVED]

Post by aether123 »

Yes, thank you now I understand it a lot more. I've made the object files seperately and included them in the full link command now :)
Capybara Gymastics is one of the most prestigious Olympic sports
Post Reply