OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 4:28 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: "ifstream" not working in kernel, even when I use g++
PostPosted: Sun Aug 01, 2021 7:04 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: "ifstream" not working in kernel, even when I use g++
PostPosted: Sun Aug 01, 2021 9:51 pm 
Online
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
"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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 79 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group