• <small id='7pANa'></small><noframes id='7pANa'>

      <bdo id='7pANa'></bdo><ul id='7pANa'></ul>

    <i id='7pANa'><tr id='7pANa'><dt id='7pANa'><q id='7pANa'><span id='7pANa'><b id='7pANa'><form id='7pANa'><ins id='7pANa'></ins><ul id='7pANa'></ul><sub id='7pANa'></sub></form><legend id='7pANa'></legend><bdo id='7pANa'><pre id='7pANa'><center id='7pANa'></center></pre></bdo></b><th id='7pANa'></th></span></q></dt></tr></i><div id='7pANa'><tfoot id='7pANa'></tfoot><dl id='7pANa'><fieldset id='7pANa'></fieldset></dl></div>

    1. <tfoot id='7pANa'></tfoot>

      <legend id='7pANa'><style id='7pANa'><dir id='7pANa'><q id='7pANa'></q></dir></style></legend>
      1. 为什么需要为每个操作系统重新编译 C/C++?

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

            <legend id='HSoi0'><style id='HSoi0'><dir id='HSoi0'><q id='HSoi0'></q></dir></style></legend>
            <i id='HSoi0'><tr id='HSoi0'><dt id='HSoi0'><q id='HSoi0'><span id='HSoi0'><b id='HSoi0'><form id='HSoi0'><ins id='HSoi0'></ins><ul id='HSoi0'></ul><sub id='HSoi0'></sub></form><legend id='HSoi0'></legend><bdo id='HSoi0'><pre id='HSoi0'><center id='HSoi0'></center></pre></bdo></b><th id='HSoi0'></th></span></q></dt></tr></i><div id='HSoi0'><tfoot id='HSoi0'></tfoot><dl id='HSoi0'><fieldset id='HSoi0'></fieldset></dl></div>
            • <bdo id='HSoi0'></bdo><ul id='HSoi0'></ul>

                  <tbody id='HSoi0'></tbody>
              1. <tfoot id='HSoi0'></tfoot>

                  <small id='HSoi0'></small><noframes id='HSoi0'>

                1. 本文介绍了为什么需要为每个操作系统重新编译 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++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                      • <bdo id='OVd4U'></bdo><ul id='OVd4U'></ul>

                            <i id='OVd4U'><tr id='OVd4U'><dt id='OVd4U'><q id='OVd4U'><span id='OVd4U'><b id='OVd4U'><form id='OVd4U'><ins id='OVd4U'></ins><ul id='OVd4U'></ul><sub id='OVd4U'></sub></form><legend id='OVd4U'></legend><bdo id='OVd4U'><pre id='OVd4U'><center id='OVd4U'></center></pre></bdo></b><th id='OVd4U'></th></span></q></dt></tr></i><div id='OVd4U'><tfoot id='OVd4U'></tfoot><dl id='OVd4U'><fieldset id='OVd4U'></fieldset></dl></div>
                              <tbody id='OVd4U'></tbody>

                            <small id='OVd4U'></small><noframes id='OVd4U'>

                          • <tfoot id='OVd4U'></tfoot><legend id='OVd4U'><style id='OVd4U'><dir id='OVd4U'><q id='OVd4U'></q></dir></style></legend>