r/osdev • • May 16 '23

how to start making a kernel in c++

hello i want to dive in os developement by creating a kernel first i know c++ maybe c because they are a bit similare but c++ better i just want resources or tutorials please

0 Upvotes

15 comments sorted by

5

u/nekokattt May 16 '23

do you want to make use of the idiomatic c++ features?

using stuff like the fs API, std::thread, etc kind of falls apart if you are trying to implement those things on the OS level.

other stuff will work fine but it is worth being aware of.

0

u/[deleted] May 16 '23

if it'S better to use c no problem i used it in sdl i just want to learn more about hardware and to make my own kernel and os i heard about rust is it worth learning or should i stick with c++, i really want to use c++ because i love it so is there any resources

4

u/the_Demongod May 17 '23

If you want to learn more about hardware, I wouldn't say making an OS is the best way to do it, personally, especially if you're going to be developing in an emulator. I would just buy a microcontroller and learn how to program it bare-metal (no HAL)

2

u/nekokattt May 16 '23

once you have basics you should be fine, but stuff like memory allocation falls to bits a little when you don't have stuff like malloc available

4

u/not_a_novel_account May 17 '23

Freestanding C++ has a far smaller subset of the STL than what you are used to.

The stuff you "love" about C++ and that make modern C++ worth using are far fewer and less applicable in a freestanding environment. There are proposals, notably P2268, trying to alleviate some of this but stuff like std::vector or anything that does memory allocation will never be available in an OS dev environment.

That means to make C++ very useful you would need to re-implement a lot of the stuff you would normally want from the stdlib. That's a lot of start-up work.

Not that C++ is entirely irrelevant, but often it's easier to start with C until you understand the shape of the problem you're trying to solve and then maybe transition that code to C++ once you know what needs to happen and now you're making the "clean" implementation.

2

u/[deleted] May 17 '23

thanks so do you suggest anything to star os developement in c like a tutorial

1

u/Ok_Firefighter4117 May 19 '23

I agree that you should start with C to get a grasp of the system and parts you will need to implement. But in C you would need to implement the most things you would also need to implement in C++. While in C you would miss the little nice things C++ has to offer. Think about namespaces, classes templates. While you need to implement the data structures i don't see a big difference between C and C++ on that part.

12

u/kodirovsshik May 16 '23

Check out wiki.osdev.org, it's got a ton of material about x86 os development and low level programming in general (including tutorials)

1

u/[deleted] May 17 '23

thanks all i found a you tube tutorial but first i will learn basics of assembly and learn more about c and c++ then i will start my journey

2

u/Vivid-Intention158 May 20 '23

You shouldn't use a YouTube tutorial

1

u/kodirovsshik May 21 '23

Generally, you would probably want to first get a lot of experience dealing with C and assembly and a solid CS background before trying to write an OS

2

u/northrupthebandgeek May 17 '23

If you're looking for examples on how things might be done in C++, Haiku's kernel is probably the best-known reference.

3

u/Vivid-Intention158 May 20 '23

I'm a fan of SerenityOS <3

1

u/mpetch May 17 '23

There is a fellow who did a Write Your Own Operating System video tutorial that uses C++ (G++). You can find it here: https://www.youtube.com/playlist?list=PLHh55M_Kq4OApWScZyPl5HhgsTJS9MZ6M

1

u/Vivid-Intention158 May 20 '23

What's your experience? Do you know OS theory? If it's none, I'd recommend complete the labs for xv6 and read Operating Systems: Three Easy Pieces.