Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a freestanding C++ program?

Tags:

c++

I'm just wondering how you create a freestanding program in C++?

Edit: By freestanding I mean a program that doesn't run in a hosted envrioment (eg. OS). I want my program to be the first program the computer loads, instead of the OS.

like image 238
Bob Dylan Avatar asked Jan 18 '10 22:01

Bob Dylan


People also ask

How do I write a simple C program?

The simplest way to get started is to install an IDE (a program for writing code) and a C compiler, and then experiment with some sample code. This wikiHow article will teach you how to write a basic C program that displays a string of text on the screen. Install an Integrated Development Environment (IDE).

How do I run a C++ program in Visual Studio?

Run Dev/C++ Go to Start Menu > All Programs > Bloodshed Dev-C++ > Dev-C++. This should start the program. 2. Create a New Source File Once the program opens, you need to create a new source file so you can start writing your first program. To do this select File > New > Source File.

How to start C++ program in AutoCAD?

1. Run Dev/C++ Go to Start Menu > All Programs > Bloodshed Dev-C++ > Dev-C++. This should start the program. 2. Create a New Source File Once the program opens, you need to create a new source file so you can start writing your first program.

How to create a program in C sharp?

How to Create a Program in C Sharp. 1. Open Visual C#. Visual Studio has an icon that resembles a purple ribbon. Click the icon in Windows Start menu to open Visual Studio. 2. Click Create a new project. It's the last option on the title page. 3. Select Console App (.NET Core) for C# and click Next. ...


3 Answers

If you were using BSD Unix, you would link with the standalone library. That included a basic IO system for disk and tty. Your source code looked the same as if it were to be run under Unix, but the binary could be loaded into a naked machine.

like image 148
gary Avatar answered Oct 28 '22 06:10

gary


Legacy Systems

Even with your clarification, the answer is that it depends -- the exact boot sequence depends on the hardware -- though there's quite a bit of commonality. The boot loader is typically loaded at an absolute address, and the file it's contained in is frequently read into memory exactly as-is. This means instead of a normal linker, you typically use a "linking locator". Where a typical linker produces an executable file ready for relocation and loading, a locator produces an executable that's already set up to run at one exact address, with all relocations already applied. For those old enough to remember them, it's typically pretty much like an MS-DOS .COM file.

Along with that, it has to (of course) statically link the whole run-time that the program depends upon -- it can't depend on something like a DLL or shared object library, because the code to load either of those hasn't itself been loaded yet.

EFI/UEFI

Current PCs (and Macs) use EFI/UEFI. I'm going to just refer to UEFI throughout the remainder of this article, but most of it applies about equally to EFI as well (but UEFI is much more common).

These provide quite a bit more support for boot code. This includes drivers for most devices (it supports installing device drivers), so your boot code can use networking and such, which is much more difficult to support in legacy mode.

Bootable code under EFI uses the same PE format as Windows executables. Libraries are also available so quite a bit of boot code can be written much more like normal code that runs inside an OS. I won't try to get into a lot of detail, but here are links to some information.

https://www.intel.com/content/www/us/en/developer/articles/tool/unified-extensible-firmware-interface.html

https://www.intel.com/content/dam/doc/guide/uefi-driver-network-boot-devices-guide.pdf

https://www.intel.com/content/dam/www/public/us/en/documents/guides/bldk-v2-uefi-standard-based-guide.pdf

And perhaps the most important one--the development kit:

https://github.com/tianocore/edk2

like image 24
Jerry Coffin Avatar answered Oct 28 '22 06:10

Jerry Coffin


See this page: http://wiki.osdev.org/C++

It has everything necessary to start writing an OS using c++ as the core language using the more popular toolchains.

In addition this page should prove to be very helpful: http://wiki.osdev.org/C++_Bare_Bones. It pretty much walks you through getting to the c++ entry point of an OS.

like image 24
Evan Teran Avatar answered Oct 28 '22 06:10

Evan Teran