"ifstream" not working in kernel, even when I use g++

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
amandude
Posts: 10
Joined: Wed Jul 07, 2021 7:18 pm

"ifstream" not working in kernel, even when I use g++

Post by amandude »

I am trying to read/write to a .txt file using "fstream" to store permanent data, even if the system reboots.
The problem is that when I go to compile, it says:

Code: Select all

kernel.cpp:(.text+0x52): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream()'
ld: kernel.cpp:(.text+0x6d): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode)'
make: *** [Makefile:47: link] Error 1
I tried using g++ instead of gcc, but it still did not work.

Does anyone know how to fix this?

PS: I am following off of this tutorial: https://github.com/Absurdponcho/PonchoO ... ead/kernel

Here is the code:


Makefile:

Code: Select all

OSNAME = CustomOS

GNUEFI = ../gnu-efi
OVMFDIR = ../OVMFbin
LDS = kernel.ld
CC = g++
ASMC = nasm
LD = ld

CFLAGS = -ffreestanding -fshort-wchar -mno-red-zone -fno-exceptions
ASMFLAGS = 
LDFLAGS = -T $(LDS) -static -Bsymbolic -nostdlib

SRCDIR := src
OBJDIR := lib
BUILDDIR = bin
BOOTEFI := $(GNUEFI)/x86_64/bootloader/main.efi

rwildcard=$(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d))

SRC = $(call rwildcard,$(SRCDIR),*.cpp)  
ASMSRC = $(call rwildcard,$(SRCDIR),*.asm)  
OBJS = $(patsubst $(SRCDIR)/%.cpp, $(OBJDIR)/%.o, $(SRC))
OBJS += $(patsubst $(SRCDIR)/%.asm, $(OBJDIR)/%_asm.o, $(ASMSRC))
DIRS = $(wildcard $(SRCDIR)/*)

kernel: $(OBJS) link

$(OBJDIR)/interrupts/interrupts.o: $(SRCDIR)/interrupts/interrupts.cpp
	@ echo !==== COMPILING $^
	@ mkdir -p $(@D)
	$(CC) -mno-red-zone -mgeneral-regs-only -ffreestanding -c $^ -o $@

$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
	@ echo !==== COMPILING $^
	@ mkdir -p $(@D)
	$(CC) $(CFLAGS) -c $^ -o $@
	
$(OBJDIR)/%_asm.o: $(SRCDIR)/%.asm
	@ echo !==== COMPILING $^
	@ mkdir -p $(@D)
	$(ASMC) $(ASMFLAGS) $^ -f elf64 -o $@
	 
link:
	@ echo !==== LINKING
	$(LD) $(LDFLAGS) -o $(BUILDDIR)/kernel.elf $(OBJS)

setup:
	@mkdir $(BUILDDIR)
	@mkdir $(SRCDIR)
	@mkdir $(OBJDIR)

buildimg:
	dd if=/dev/zero of=$(BUILDDIR)/$(OSNAME).img bs=512 count=93750
	mformat -i $(BUILDDIR)/$(OSNAME).img -f 1440 ::
	mmd -i $(BUILDDIR)/$(OSNAME).img ::/EFI
	mmd -i $(BUILDDIR)/$(OSNAME).img ::/EFI/BOOT
	mcopy -i $(BUILDDIR)/$(OSNAME).img $(BOOTEFI) ::/EFI/BOOT
	mcopy -i $(BUILDDIR)/$(OSNAME).img startup.nsh ::
	mcopy -i $(BUILDDIR)/$(OSNAME).img $(BUILDDIR)/kernel.elf ::
	mcopy -i $(BUILDDIR)/$(OSNAME).img $(BUILDDIR)/zap-light16.psf ::

run:
	qemu-system-x86_64 -drive file=$(BUILDDIR)/$(OSNAME).img -m 256M -cpu qemu64 -drive if=pflash,format=raw,unit=0,file="$(OVMFDIR)/OVMF_CODE-pure-efi.fd",readonly=on -drive if=pflash,format=raw,unit=1,file="$(OVMFDIR)/OVMF_VARS-pure-efi.fd" -net none


kernel.cpp

Code: Select all

#include <fstream>
#include <stdint.h>
using namespace std;

#include "kernelUtil.h"
#include "eif.h"
#include "userinput/keyboard.h"
#include "prepare_os/prepare-os.h"
#include "prepare_os/frame_handler.h"
#include "ahci/ahci.h"



extern "C" void _start(BootInfo* bootInfo){

    KernelInfo kernelInfo = InitializeKernel(bootInfo);

    GlobalRenderer->Clear();

    IntInfo defaultInfo = GetDefaultIntfSettings();

    // this is just to make the interface (basically just plotting pixels)

    InitializeInterface(defaultInfo);

    // Here is where I do the file opening

    ifstream myFile;

    // I am just testing this out to see if it compiles,  
    
    myFile.open("info/username.txt");


    while(true){
        asm ("hlt");
    }
}
Thanks for your help! :D
Octocontrabass
Member
Member
Posts: 5443
Joined: Mon Mar 25, 2013 7:01 pm

Re: "ifstream" not working in kernel, even when I use g++

Post by Octocontrabass »

"ifstream" is part of the C++ standard library. You need to port the C++ standard library to your OS before you can use anything in it.

It's also a good idea to use a cross-compiler. (I personally would also get rid of GNU-EFI and use plain UEFI headers with a cross-compiler, but that's just me.)

Don't trust OS development tutorials. They're full of bugs.
Post Reply