什么时候必须编译C/C ++? [英] When does C/C++ have to be compiled?

查看:51
本文介绍了什么时候必须编译C/C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题假定所编写的C/C ++代码是可移植的,并且不使用平台特定的代码.

This question assumes that the C/C++ code that is written is portable and does not use platform specific code.

我开始学习C ++以及如何编译代码.我知道创建C是为了避免使用汇编语言,因为必须针对不同的硬件配置来重写程序.我也知道必须针对不同的计算机配置重新编译C/C ++.

I am beginning to learn C++ and how to compile code. I know that C was created to avoid assembly languages because programs would have to be rewritten for different hardware configurations. I also know that C/C++ has to be recompiled for different computer configurations.

这让我想知道什么是新配置.我可以从Internet下载可以正常运行的可执行文件,而Internet显然不知道我的硬件配置.是否必须为每个不同的CPU编译代码?为一个操作系统版本编译的代码是否对另一版本有效?(Windows 7代码无需重新编译即可用于Windows 8吗?)

That made me wonder what constitutes a new configuration. I can download an executable file from the internet that works just fine, and the internet obviously does not know my hardware configuration. Does code have to be compiled for each different CPU? Does code compiled for one OS version work for another one? (Does Windows 7 code work for Windows 8 without a recompilation?)

简而言之,什么条件定义了必须针对其进行编译的新配置?

In a nutshell, what conditions define a new configuration that has to be compiled for?

推荐答案

C和C ++是已编译的编程语言.编译语言是一种编程语言,其实现通常是编译器-从源代码生成机器代码的翻译器-而不是解释器..机器代码是由计算机处理器直接执行的一组指令.有两件事使程序与硬件/软件组合不兼容.您要在其上运行二进制文件的计算机(硬件)中最重要的一个.例如,您需要知道在64位CPU上编译的程序不能在32位或ARM处理器上运行.

C and C++ are compiled programming languages. A compiled language is a programming language whose implementations are typically compilers - translators which generate machine code from source code -, and not interpreters. Machine code is a set of instructions executed directly by a computer's processor. There is two things make programs incompatible with hardware/software combinations. Most important one in The Machine (Hardware) you are trying to run your binary on. For example you need to know that a program compiled on a 64-bit CPU, can not be run on a 32-bit or ARM processor.

数十种,其中计算机体系结构和子体系结构.主要体系结构是x86,x86_64(amd64),可能还有ARM.如果您使用的语言会将其代码编译为机器代码,则只能在具有与您的CPU兼容的CPU的计算机上分发二进制工件(除非您进行交叉编译).

There are dozens of computer architectures and sub-architectures. Major architectures are x86, x86_64 (amd64) and probably ARM. If you are using a language that it's code is going to be compiled to machine code, you will be able to distribute binary artifacts only on computers that have a CPU compatible with yours (Unless you are cross-compiling).

硬件并不是平台之间的唯一区别.通常,您的代码是在与CPU见面之前由操作系统处理的.操作系统中执行此工作的部分称为应用程序二进制接口.即使在同一台计算机上,也无法在带有Windows的Linux上运行编译的ELF二进制文件,因为Win32可执行文件是不同的.

Hardware is not the only difference between platforms. Usually your code is meant to be handled by Operating System before meeting CPU. The part of operating system that do this job is called application Binary Interface. You can't run an ELF binary compiled on Linux with Windows even if on same machine, because Win32 executables are different.

这篇关于什么时候必须编译C/C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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