How do you create a freestanding C++ program?

后端 未结 7 906
予麋鹿
予麋鹿 2021-02-02 02:11

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.

相关标签:
7条回答
  • 2021-02-02 02:31

    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.

    0 讨论(0)
  • 2021-02-02 02:36

    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.

    0 讨论(0)
  • 2021-02-02 02:36

    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.

    0 讨论(0)
  • 2021-02-02 02:46

    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)

    0 讨论(0)
  • 2021-02-02 02:49

    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.

    0 讨论(0)
  • 2021-02-02 02:54

    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.

    0 讨论(0)
提交回复
热议问题