tags:

views:

327

answers:

8

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.

A: 

On what platform? On Unix, you use gcc:

#include <iostream>
int main(int argc,char **argv)
{
  std::cout << "Hello world!\n";
}

Then:

$ g++ -o x.cpp
$ ./x
Hello world!
$
vy32
I'm not asking how to create a C++ program. I'm asking how to write a freestanding C++ program (doesn't run in an OS).
Bob Dylan
On unix you use gcc??? Aren't you forgetting about the native compilers? How about aCC on HPUX, sun studio (CC) on Solaris, intel's icc is pretty good on Linux. There's a lot more than gcc out there and they're frequently better (produce faster code) than GCC
Glen
Doesn't deserve to be hammered with down votes as it was written before the OP clarified the question. *Should* be deleted though as it is not responsive.
dmckee
+1  A: 

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.

Jerry Coffin
+2  A: 

google 'embedded c++' for a start

Another idea is to start with the embedded systems emulators, for example the atmel AVR site has a nice IDE the emulates atmel AVR systems, and allows you to build raw code in C and load it into an emulated CPU, they use gcc as toolchain (I think)

pm100
+2  A: 

C++ is used in embedded systems programming, even to write OS kernels.

Usually you have at least a few assembler instructions early in the boot sequence. A few things are just easier to express that way, or there may be reference code from the CPU vendor you need to use.

For the initial boot process, you won't be able to use the standard library. No exceptions, RTII, new/delete. It's back to "C with classes". Most people just use C here.

Once you have enough supporting infrastructure loaded though, you can use whatever parts of the standard library you can port.

Marsh Ray
+2  A: 

You will need an environment that provides:

  • A working C library, or enough of it to do what you want
  • The parts of the C++ runtime that you intend to use. This is compiler-specific

In addition to any other libraries. If you have no dynamic linker on your platform (if you have no OS, you probably have no linker) then you will have to static-link it all.

In practice this means linking some small C++ runtime, and a C library appropriate for your platform. Then you can simply write a standalone C++ program.

MarkR
A: 

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.

gary
+2  A: 

See this page: http://wiki.osdev.org/C%2B%2B

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%2B%2B_bare_bones. It pretty much walks you through getting to the c++ entry point of an OS.

Evan Teran
+5  A: 

Have a look at this article:

http://www.codeproject.com/KB/tips/boot-loader.aspx

You would need a little assembly start-up code to get you as far as main() but then you could write the rest in C++. You'd have to write your own heap manager (new/delete) if you wanted to create objects at runtime and your own scheduler if you wanted more than one thread.

PhilMY