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

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

问题描述

这更像是一个理论问题.我是 Comp sci 专业,对低级编程非常感兴趣.我喜欢了解引擎盖下的工作原理.我的专长是编译器设计.

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.

当您使用 C/C++ 编写程序时,人们通常知道的是,编译器会神奇地将您的 C/C++ 代码转换为该机器的本机代码.

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.

但有些东西在这里并没有增加.如果我针对 x86 架构编译我的 C/C++ 程序,那么相同的程序似乎应该在具有相同架构的任何计算机上运行.但这不会发生.您需要为 OS X 或 Linux 或 Windows 重新编译代码.(再次针对 32 位与 64 位)

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)

我只是想知道为什么会这样?我们在编译 C/C++ 程序时不是针对 CPU 架构/指令集吗?Mac 操作系统和 Windows 操作系统可以在完全相同的架构上运行.

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.

(我知道 Java 和类似的目标是 VM 或 CLR,所以这些不算数)

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

如果我对此给出了最佳答案,我会说 C/C++ 必须编译为特定于操作系统的指令.但是我读过的每个来源都说编译器针对机器.所以我很困惑.

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.

推荐答案

我们在编译 C/C++ 程序时不是针对 CPU 架构/指令集吗?

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

不,你没有.

我的意思是,是的,您正在为 CPU 指令集进行编译.但这不是全部编译.

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

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

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?

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

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.

并且操作系统调用在操作系统之间没有标准化.几乎每个标准库函数都可以完成一些您无法用 C 或 C++ 自行构建的功能,它们都将与操作系统对话以至少完成部分工作.

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?记忆不属于你;它属于操作系统,您也许可以拥有一些.scanf?标准输入不属于你;它属于操作系统,您可以从中读取.等等.

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.

您的标准库是通过调用 OS 例程构建的.并且这些操作系统例程是不可移植的,因此您的标准库实现是不可移植的.所以你的可执行文件中有这些不可移植的调用.

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.

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

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天全站免登陆