C语言是开源的吗?
C(或任何其他低级语言,就此而言)是否有源代码,或者编译器是“完成所有工作”(包括解析)的部分?如果是这样,不同的编译器难道不能有不同的 C 方言吗? stdlib 在哪里影响到这个?我真的很想知道这是如何运作的。
Does C (or any other low-level language, for that matter) even have source, or is the compiler the part that "does all the work", including parsing? If so, couldn't different compilers have different C dialects? Where does the stdlib factor into this? I would really like to know how this works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
C 语言不是一个软件,而是一个定义的标准,所以不会说它是开源的,而是说它是一个开放标准。
然而,C 语言有无数种不同的编译器,其中许多确实是开源的。最值得注意的例子是GCC的C编译器,它都在GNU 通用公共许可证 (GPL),一种开源许可证。
还有更多选择。例如,Watcom 是开源的。开源 C 编译器并不缺乏,但毫无疑问,最广泛使用的编译器(至少在非 Windows 世界中)是 GCC。
对于 Windows,最好的选择可能是 Watcom 或 GCC,使用 Cygwin 或 MinGW。
The C language is not a piece of software but a defined standard, so one wouldn't say that it's open-source, but rather that it's an open standard.
There are a gazillion different compilers for C however, and many of those are indeed open-source. The most notable example is GCC's C compiler, which is all under the GNU General Public License (GPL), an open-source license.
There are more options. Watcom is open-source, for instance. There is no shortage of open-source C compilers, but without a doubt the most widespread one, at least in the non-Windows world, is GCC.
For Windows, your best bet is probably Watcom or GCC by using Cygwin or MinGW.
C 是一个标准,指定 C 编译器应如何生成程序。
C本身没有任何源代码,就像音符没有任何塑料一样。
一些 C 编译器(例如 GCC)是开源的。
C is a standard which specifies how C compilers should generate programs.
C itself doesn't have any source code, just like a musical note doesn't have any plastic.
Some C compilers, such as GCC, are open source.
C 只是一种语言,而且还是一种标准化语言。它几乎是编译器“完成了所有工作”。不同的编译器确实有不同的方言;在 C99 ANSI 标准出现之前,有像 Borland C 和其他竞争编译器这样的东西,它们以自己奇妙的方式实现了 C 语言。
stdlib
只是一个商定的标准库集合,任何 ANSI C 实现中都需要这些库。C is just a language, and a standardised one at that, too. It pretty much is the compiler that "does all the work". Different compilers did have different dialects; before the the C99 ANSI standard, you had things like Borland C and other competing compilers, that implemented the C language in their own fantastic ways.
stdlib
is just an agreed-upon collection of standard libraries that are required to be present in any ANSI C implementation.要补充其他很好的答案:
关于不同的方言——C 中添加了一些特定于编译器的附加功能。您可以向 gcc 提供命令行标志
-std=...
来指定您要使用的 C 标准,每个标准在语法上都有细微的变化/添加,最常见的可能是c99
。每个编译器都倾向于实现一些不同的附加功能,例如,
typeof()
不在 C 标准中,因此编译器不必实现它,但它仍然很有用,并且大多数编译器都提供它。这是 gcc C 扩展列表stdlib 是一组函数C 标准中规定。与编译器非常相似,stdlib 可以有不同的实现。 GNU 实现是开源的,就像 gcc 一样,但还有其他编译器,并且可能是闭源的 stdlib 的其他实现。
To add on to the other great answers:
Regarding different dialects -- there are some additional features added to C that are compiler specific. You can provide the command line flag
-std=...
to gcc to specify the C standard that you want to use, each has slight variations/additions to syntax, the most common is probablyc99
.Each compiler tends to implement a few different extras, for example,
typeof()
is not in the C standard and so compilers do not have to implement this but nevertheless it is useful and most compilers provide it. Here is a list of gcc C extensionsThe stdlib is a set of functions specified in the C standard. Much like compilers, stdlib can have different implementations. The GNU implementation is open source, as is gcc, but there are other compilers and could be other implementations of stdlib that are closed source.
编译器将确定从 C 到汇编等的所有映射...但就拥有它的人而言...没有人真正拥有 C,但是 ANSI/ISO 确定标准
The Compiler would determine all the mappings from C to Assembly etc... but as far as someone owning it.....noone really owns C however the ANSI/ISO determines the standards
GCC 的 C 编译器是用 C 编写的。所以我们知道至少有一个 C 编译器是用 C 编写的。GNU
的
stdlib
(glibc) 也是用 C 编写的(stdio.h) ,stdlib.h)。但它也有一些部分是用汇编语言编写的。GCC's C compiler is written in C. So we know there are at least one C compiler written in C.
GNU's
stdlib
(glibc) is also written in C (stdio.h, stdlib.h). But it also has some parts written in assembly language.一个非常好的问题。 有一种方法可以以“的形式定义语言标准(而不是实现!)”源代码”,使用严格且明确的语言。不幸的是,所有旧语言,包括 C,都定义得很差。但仍然可以将这些定义翻译成源代码形式。
另一种方法是通过其操作语义来定义语言,通常以简单(和低效)参考实现。
A really good question. There is a way to define a language standard (not the implementation!) in a form of a "source code", in a strict and unambigous language. Unfortunately, all of the old languages, including C, are poorly defined. But it is still possible to translate that definitions into a source code form.
Another approach is to define a language via its operational semantics, often in a form of a simple (and unefficient) reference implementation.
我想说 C 作为一种语言不是开源的。
正如许多人指出的那样,您可以免费下载 GNU 许可的编译器和库,但如果您想编写自己的 C 编译器,则需要遵循 ISO C 标准,并且 ISO 对 C 语言规范收取硬金,在发布时为 178 美元。
因此,答案实际上取决于您对免费和开源的哪些元素感兴趣。
I would say that C as a language is not open source.
As pointed out by many, you can download GNU licensed compilers and libraries for free, but if you wanted to write your own C compiler, you would need to follow the ISO C standards, and ISO charge hard cash for the specification of the C language, which at the time of posting this is $178.
So really the answer depends on what elements you are interested in being free and open source.
Helgi Hrafn Gunnarsson 写了主要答案,但我认为值得注意的是,你也可以有效地得到方言。
编译器应该对它们支持的任何标准做同样的事情(现在应该几乎都是相同的版本),但是存在灰色区域。例如,编译器处理“未定义”功能的方式。如果 C 规范规定特定情况下的行为是未定义的,那么编译器几乎可以做它想做的事情。
还有编译器制造商添加到库(以及添加的新库)中的函数示例,以支持特定的平台特征、创造竞争优势或只是为了让生活更轻松。愤世嫉俗的人可能会建议添加其中一些内容来帮助将人们锁定在特定的编译器中。
Helgi Hrafn Gunnarsson has written the main answer but I thought it would be worth noting that you can effectively end up with dialects too.
The compilers should do the same thing with regards to whichever standard they support (which these days should be pretty much all the same version) but there are grey areas. The way in which the compilers work for 'undefined' functionality for example. If the C specification says that the behaviour is undefined for a specific case then the compiler can do pretty much what it wants.
There are also examples of functions added to the libraries (and new libraries added) by the compiler makers to support specific platform traits, create a competitive advantage or simply to make life easier. The cynical might suggest that some of these are added to help lock people into a specific compiler too.
我不确定你对“开源”的定义是什么。
对于标准化过程,任何人都可以参与,但如果您想能够投票,那么您需要付费加入您的国家机构(例如,美国的 ANSI、英国的 BSI、法国的 AFNOR) ETC。)。一般来说,大多数标准机构的会员费都是由公司支付的。也就是说,这个过程是相当开放的。您可以在标准网站上访问讨论文件。
标准本身也不是免费的。 ISO pdf 商店目前以 198 瑞士法郎的价格出售 C 标准。该标准的草稿副本可以轻松免费找到。
编译器和库都有很多开源实现。
I'm not sure what your definitions of "open source" are.
For the standardization process, it is possible for anyone to participate, but if you want to be able to vote then you will need to pay to join your national body (for instance, ANSI for the USA, BSI for the UK, AFNOR for France etc.). As a rule most standards body memberships are paid by corporations. That said, the process is fairly open. You can access discussion papers on the standards web site.
The standards themselves are not free either. The ISO pdf store currently sells the C standard for 198 swiss francs. Draft copies of the standard can be found easily for free.
There are plenty of open source implementations of both compilers and libraries.