为什么需要为每个操作系统重新编译C / C ++? [英] Why do you need to recompile C/C++ for each OS?

查看:232
本文介绍了为什么需要为每个操作系统重新编译C / C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更多的是理论问题,而不是任何问题。我是计算机科学专业,对底层编程非常感兴趣。我喜欢找出事物的幕后运作方式。我的专长是编译器设计。



无论如何,当我在开发第一个编译器时,发生的事情让我有些困惑。



当您使用C / C ++编写程序时,人们所知道的传统知识是,编译器神奇地将您的C / C ++代码转换为该机器的本机代码。

但是这里没有加起来。如果我针对x86架构编译C / C ++程序,则似乎同一程序应在具有相同架构的任何计算机上运行。但这不会发生。您需要针对OS X,Linux或Windows重新编译代码。(然后再次针对32位和64位)



我只是想知道为什么案子?编译C / C ++程序时,我们不是针对CPU体系结构/指令集吗? Mac OS和Windows Os可以在完全相同的体系结构上运行。



(我知道Java和类似的目标是VM或CLR,因此它们不会计数)



如果我对此做出了最好的回答,我会说C / C ++必须然后编译为特定于OS的指令。但是我阅读的所有资料都说编译器针对的是机器。所以我很困惑。

解决方案


我们不是以CPU体系结构/指令集为目标


不,您不会。



我的意思是,您正在编译CPU指令集。但这不是 all 编译的全部。



考虑最简单的世界,你好!程序。它所做的就是调用 printf ,对吗?但是没有 printf指令集操作码。那么...到底发生了什么?



嗯,那是C标准库的一部分。它的 printf 函数对字符串和参数进行一些处理,然后显示它。怎么发生的?好吧,它将字符串发送到标准输出。好吧...是谁控制的?



操作系统。而且也不存在标准输出操作码,因此向标准输出发送字符串涉及某种形式的OS调用。



而且OS调用未在整个操作系统之间进行标准化。几乎每个标准库函数都无法完成您在C或C ++中无法自行构建的功能,因此将与操作系统进行讨论以至少完成其某些工作。



malloc ?记忆不属于你;它属于操作系统,因此您也许可以拥有一些。 scanf ?标准输入不属于您;它属于操作系统,您也许可以从中读取内容。等等。



您的标准库是通过对OS例程的调用而构建的。而且这些OS例程是不可移植的,因此您的标准库实现是不可移植的。因此,您的可执行文件中包含了这些不可移植的调用。



最重要的是,不同的操作系统对可执行文件甚至都有不同的看法。看起来像。毕竟,可执行文件不仅仅是一堆操作码。您认为所有这些常量和预初始化的 static 变量存储在哪里?不同的操作系统具有不同的启动可执行文件的方式,而可执行文件的结构就是其中的一部分。


This is more of a theoretical question than anything. I'm a Comp sci major with a huge interest in low level programming. I love finding out how things work under the hood. My specialization is compiler design.

Anyway, as I'm working on my first compiler, things are occurring to me that are kind of confusing.

When you write a program in C/C++, the traditional thing people know is, a compiler magically turns your C/C++ code into native code for that machine.

But something doesn't add up here. If I compile my C/C++ program targeting the x86 architecture, it would seem that the same program should run on any computer with the same architecture. But that doesn't happen. You need to recompile your code for OS X or Linux or Windows.(And yet again for 32-bit vs 64-bit)

I'm just wondering why this is the case? Don't we target the CPU architecture/instruction set when compiling a C/C++ program? And a Mac OS and a Windows Os can very much be running on the same exact architecture.

(I know Java and similar target a VM or CLR so those don't count)

If I took a best-shot answer at this, I'd say C/C++ must then compile to OS-specific instructions. But every source I read says the compiler targets the machine. So I'm very confused.

解决方案

Don't we target the CPU architecture/instruction set when compiling a C/C++ program?

No, you don't.

I mean yes, you are compiling for a CPU instruction set. But that's not all compilation is.

Consider the simplest "Hello, world!" program. All it does is call printf, right? But there's no "printf" instruction set opcode. So... what exactly happens?

Well, that's part of the C standard library. Its printf function does some processing on the string and parameters, then... displays it. How does that happen? Well, it sends the string to standard out. OK... who controls that?

The operating system. And there's no "standard out" opcode either, so sending a string to standard out involves some form of OS call.

And OS calls are not standardized across operating systems. Pretty much every standard library function that does something you couldn't build on your own in C or C++ is going to talk to the OS to do at least some of its work.

malloc? Memory doesn't belong to you; it belongs to the OS, and you maybe are allowed to have some. scanf? Standard input doesn't belong to you; it belongs to the OS, and you can maybe read from it. And so on.

Your standard library is built from calls to OS routines. And those OS routines are non-portable, so your standard library implementation is non-portable. So your executable has these non-portable calls in it.

And on top of all of that, different OSs have different ideas of what an "executable" even looks like. An executable isn't just a bunch of opcodes, after all; where do you think all of those constant and pre-initialized static variables get stored? Different OSs have different ways of starting up an executable, and the structure of the executable is a part of that.

这篇关于为什么需要为每个操作系统重新编译C / C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆