V
V
Vladislav Tikhonov2017-09-15 23:16:29
C++ / C#
Vladislav Tikhonov, 2017-09-15 23:16:29

How to embed C++ compiler in OS?

Hello. We write our OS with the team. The architecture chosen was Microkernel. C++ language. The core itself on the assembler. You need to somehow build a C ++ compiler into the OS. Or not embed, but make C++ code readable by the OS so that the OS can run C++ programs. It seems like it’s possible to implement this, it’s not possible to write everything in Assembler)) The basis is ready for us. Drivers are written, services are ready. If you know how to "embed", tell me. I will be very grateful.
PS: I am not a C++ programmer. I am an Assembler programmer. I don't know how C++ works. I ask you not to throw tomatoes and funny things, but to explain normally

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2017-09-15
@wolterx

You there: wiki.osdev.org

S
safinaskar, 2017-09-27
@safinaskar

Your question is strange. If you are seriously writing OS and have already advanced so much, then you should not have such abstractly formulated questions. Such an abstract question simply makes you doubt your competence to write an OS.
I'll start with the basics (in the presentation I will simplify some points a little). There is machine code. Zeros and ones. Each architecture (x86, arm, and so on) has its own. There is a family of programming languages ​​that are as close as possible to machine code: assembly languages ​​(often instead of "assembler language" they just say "assembler"). One assembly language for each architecture. They are so close to machine code that the concepts of "assembly language" and "machine code" are often confused, and this is normal. In particular, this is what Top_Pudge did in the next answer, this is normal, this is ordinary IT slang.
Programming languages ​​(C++, python, etc.) are divided into compiled and interpreted. C++ is a compiled language. This means that in order to execute a C++ program, it must first be compiled, that is, converted into machine code by a special program called a compiler. The resulting machine code is then simply executed on the target processor. (Actually, nothing prevents you from interpreting a compiled language, but as I promised, I'll keep it simple.)
Porting means making a program that runs on one OS work on another OS.
Now about how to "embed" C ++ in the OS. The above will apply both to the kernel of your OS (if, say, you decide to rewrite it in C ++), and to the rest of the programs in your OS. That is, the word "program" hereinafter can be understood as including the core. So, there is the concept of cross-compilation and native compilation. Cross compilation is when a compiler running on one architecture/OS creates a program designed for another architecture/OS. Native compilation - when for the same. A compiler that performs cross compilation is called a cross compiler. Native - native compiler.
In your case, cross compilation is when you take some compiler (preferably an existing one, i.e. gcc, clang, MSVC++, etc.), run it on a regular OS, the one you use for development (Windows, Linux, etc.). etc.) and it creates a binary suitable to run on your OS.
Well, native compilation in your case is when you already have a compiler (your own or an existing one ported to your OS, such as gcc, clang, etc.) working in your OS, and with its help you are inside your OS create binaries for your OS.
So, I give 99% of what you will need to start with cross-compilation anyway, I will explain why a little later.
So, cross compilation. Where to begin? Let's start with the fact that I categorically do not advise you to create your own C ++ compiler. C++ is a very complex language, creating a compiler for it is an unrealistically difficult task. You need to take an existing compiler and have it generate code for your OS. So that the headers of the binaries match those accepted in your OS and that the system calls compile correctly. Write - I can try to tell you more.
When the cross compiler is ready, you can use it to build C++ programs for your OS. Everything is ready.
What's next? Then you can do native compilation. Whether to do it depends on what your OS is designed for. If this is an OS for soda machines, etc., of course, you don’t need to do it. Why would a soda machine need to be able to compile C++ on its own? Not necessary. Just compile all the necessary programs for your machine with a cross-compiler and upload it to the firmware. Everything. But if you are writing a general-purpose OS, then a native compiler must appear there sooner or later. Okay, how to make it? Well, let's say that your cross compiler is based on gcc. Ok, then take gcc and compile it with your gcc cross compiler. And you will get gcc native for your OS. Ready. In practice, of course, things can get a little more complicated.
I promised to tell you why you need to start with cross-compilation anyway. Because, in order to immediately start with native, you will have to write your own native C ++ compiler in assembler. What is unreal. If you start with cross-compiling, you can then cross-compile, say, gcc and thus get a native compiler for your OS.
Now a question. Do you work for some customer/owner for money or do you just write your project with some team (whether commercial or not)?
If the first, and if at the same time this question of yours on toster.ru expresses the opinion of your entire team (that is, no one in your team really knows how to even start supporting C ++ in the OS), then tell the customer / owner that fired the whole team and looked for another. Really. This question on toster.ru shows your complete incompetence. People who write an OS should at least know that there is C ++, that it is compiled, that there is machine code, there are assembly languages, there is cross compilation, in general, everything that I wrote above. Even I can handle OS writing better. And this despite the fact that, in general, I don’t know how to do this, and I never got to writing my own OS with support for my custom binaries. The maximum I got to: a couple of attempts to write an OS, see my github github.com/safinaskar , duo and gavin projects. If you have absolutely no one to take on the project, take me better. I at least roughly know what needs to be done, and in the end I will write you an OS, even if it takes an unrealistically long time. I'm serious, I'm not working anywhere right now, my resume is here: safinaskar.com/resume.pdf.
If the second is true, i.e. you just write a project (I say again, it doesn’t matter if it’s commercial or not) with your team, don’t rush anywhere, you want to learn how to do it, well, go ahead, writing your own OS is very interesting and instructive case, wiki.osdev.org to help you.
Top_Pudge in the next answer suggests a completely unrealistic thing: to write your own C ++ compiler (which is difficult in itself), and even in assembler (nightmare! apparently no one has ever done this, even Stroustrup). Rave.
In any case, I advise you to definitely read this wonderful article: www.interstron.ru/upload/images/pubs/Redkaya_profe.... An interesting text about writing your own C++ compiler. About how difficult it is. So that you do not have any thoughts to write your own C ++ compiler. Although, on the other hand (unless, of course, you work for a customer who waits for a result in a reasonable time), if you want, you can try to write your own C ++ compiler. This is a great self learning project. But very long.
If you have any questions, write the answer here, either in a personal message on Habré ( habrahabr.ru/users/safinaskar ) or by mail ( [email protected] ). I will gladly answer. I have written here not everything that I think on the issue, I can write more. In general, in any case, write what you read, I tried, it is important for me to know that they read me.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question