开发者

How do C++ programs work? [closed]

开发者 https://www.devze.com 2023-03-12 23:30 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, in开发者_如何学Gocomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form.
It's difficult to tell what is being asked here. This question is ambiguous, vague, in开发者_如何学Gocomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

To make my question more clear, I'd like to mention the reason for the question:-

  1. I want to know if my program will be OS-dependent. For example, I thought C++ was OS-independent; Now I know if I program in Visual C++, my programs will be Windows dependent. So, I would like to know how to make sure my program isn't OS-dependent.

  2. I want to know what how the thing works in general to be comfortable when programming. (It helps to know what happens when you include a header file.)

  3. Maybe I can optimize my programs with just knowing how the thing works, without having to read a book on optimization and consume time on optimization rather than development.

I know there are books on how compilers work. But I'm not interested in this, I'm only interested in knowing the phases programs go through to get a fully working program for the reasons I've previously mentioned. Maybe there are core phases and optional/IDE dependent phases; if this is true, I would like to know both.

EDIT: Thank you very much for the answers. I got what I wanted to know, but I will open 2 other questions for more specific info.


C++, including its standard library, is platform independent. If you write code in pure C++, then the code will be portable.

However, the C++ standard library is not as large as say the Java or Python standard libraries. Therefore, to write real applications, you usually need various 3rd party libraries. These may or may not be platform independent. Good platform independent 3rd party libraries that will meet a lot of your needs are Boost and Qt.


C++, the language is OS and architecture independent. The compiled executable file, however, is OS dependent. If you don't use Visual C++ specific features or any "syntax bugs" associated with it, I am pretty sure your program can run on other platforms once you compile it there.

As to your question how C++ works, I wonder if you know assembly language of any architecture. Translation from C++ to assembly is very straightforward for human (unoptimized of course), and that's how C++ programs work: they become instructions that the machine can directly run. But in reality, the compiler often get your code optimized for register usage, cache usage, branch prediction etc.

You don't get your code much faster if you depend on this level of optimization. Design better algorithms is what you should really work on.


  1. C++ is a standard, therefore it’s technology independent. Things get complex once you start using libraries, as not all libraries are available everywhere. Same goes for relying on language extensions and non-standard behaviour.

  2. Too complex to be answered in current form. What exactly do you want to know? Wouldn’t it be better to create a separate question?

  3. Optimization is harder than you think. It can be said that algorithmic optimization yields best results – if you choose a dumb algorithm, your language skills won’t help you however you try.


C++ is OS independent, but you have to compile it for each system you plan to use it on. There is however libraries and stuff that are OS dependent, for instance Winapi is Windows dependent.

Check out the standard libraries, for instance at cplusplus or look at Boost for that matter. There you have OS independent code.

0

精彩评论

暂无评论...
验证码 换一张
取 消