How can I use runtime library without hardcoding it

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
devc1
Member
Member
Posts: 439
Joined: Fri Feb 11, 2022 4:55 am
Location: behind the keyboard

How can I use runtime library without hardcoding it

Post by devc1 »

I want to use the C Runtime Library functions in my kernel for some purposes like converting integers to text and vice versa. I'm using MSVC CL, I run vcvars64.bat and when I call for example _itoa() it gives unresolved external error.
Even compiling with /MT does not work.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How can I use runtime library without hardcoding it

Post by iansjack »

Are you including the appropriate .lib file in the list of files to link?
devc1
Member
Member
Posts: 439
Joined: Fri Feb 11, 2022 4:55 am
Location: behind the keyboard

Re: How can I use runtime library without hardcoding it

Post by devc1 »

I'm not including anything, and talking about .lib files is there any static cruntime lib which I can include ?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How can I use runtime library without hardcoding it

Post by iansjack »

atoi is implemented in libucrt.lib for static linking.
devc1
Member
Member
Posts: 439
Joined: Fri Feb 11, 2022 4:55 am
Location: behind the keyboard

Re: How can I use runtime library without hardcoding it

Post by devc1 »

Thank you, now itoa works but I'm a little bit worried about this :

Code: Select all

libucrt.lib
kernel32.lib(KERNEL32.dll) : warning LNK4237: /SUBSYSTEM:NATIVE specified when importing from 'KERNEL32.DLL';  Use /SUBSYSTEM:CONSOLE or /SUBSYSTEM:WINDOWS.
libucrt includes kernel32.dll, It's fine because I'm not using any windows related function but at the same time I'll be linking my kernel to some dlls in the future (for e.g. the driver development kit or Graphics API), so the time I'll add DLL importing to my bootloader the bootloader will likely fail because it cannot find kernel32.dll.

This is the related document : https://learn.microsoft.com/en-us/cpp/e ... w=msvc-170

On top of that I may be naming some functions a similar name to windows one's
here are the exported functions :

Code: Select all

Section contains the following imports:

    KERNEL32.dll
      FFFF80000000B000 Import Address Table
      FFFF8000000138C0 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                         4F5 RtlCaptureContext
                         4FD RtlLookupFunctionEntry
                         504 RtlVirtualUnwind
                         3A0 IsDebuggerPresent
                         5E6 UnhandledExceptionFilter
                         5A4 SetUnhandledExceptionFilter
                         27D GetLastError
                         564 SetLastError
                         232 GetCurrentProcess
                         5C4 TerminateProcess
                         3A8 IsProcessorFeaturePresent
                         178 ExitProcess
                         1C5 FreeLibrary
                         295 GetModuleHandleW
                         294 GetModuleHandleExW
                         2CD GetProcAddress
                         36C HeapAlloc
                         370 HeapFree
                         149 EnterCriticalSection
                         3E0 LeaveCriticalSection
                         123 DeleteCriticalSection
                         1B6 FlsGetValue
                         1B7 FlsSetValue
                         386 InitializeCriticalSectionAndSpinCount
                         3E6 LoadLibraryExW
                         3D4 LCMapStringW
                         3AE IsValidCodePage
                         1CC GetACP
                         2B6 GetOEMCP
                         1DB GetCPInfo
                         2F8 GetStringTypeW
                         412 MultiByteToWideChar
                         637 WideCharToMultiByte
                          DA CreateFileW
                         503 RtlUnwindEx
                         145 EncodePointer
                         487 RaiseException
                         5D8 TlsGetValue
                         5D9 TlsSetValue
                         4FF RtlPcToFileHeader
                         555 SetFilePointerEx
                         57F SetStdHandle
                         1B9 FlushFileBuffers
                         64B WriteFile
                         21A GetConsoleOutputCP
                         216 GetConsoleMode
                          94 CloseHandle
                         64A WriteConsoleW
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: How can I use runtime library without hardcoding it

Post by Octocontrabass »

C runtime libraries are designed to be used in hosted environments. In a freestanding environment like an OS kernel, you will need to either find a library designed to be used in a freestanding environment or write the necessary functions yourself.
devc1
Member
Member
Posts: 439
Joined: Fri Feb 11, 2022 4:55 am
Location: behind the keyboard

Re: How can I use runtime library without hardcoding it

Post by devc1 »

Ok So I think that I'm going to write the library by myself and I'll be copying some functions from Linux to speed up the coding process.
I will make a separate c runtime library (cruntime.dll) and a whole nos runtime library (nosrt.dll) for user mode programs.
If you have better name suggestion let me know and thanks!
Last edited by devc1 on Fri Mar 24, 2023 2:29 pm, edited 1 time in total.
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: How can I use runtime library without hardcoding it

Post by Octocontrabass »

devc1 wrote:I'll be copying some functions from Linux
Pay attention to the license. If you copy GPL code, your kernel must be released under the GPL.
devc1
Member
Member
Posts: 439
Joined: Fri Feb 11, 2022 4:55 am
Location: behind the keyboard

Re: How can I use runtime library without hardcoding it

Post by devc1 »

Then I will just proceed and use existing functions from my previous OS, It has most of the functions that I need.
Post Reply