[英]Cygwin: Linux on Windows? How to ensure that program created in Windows works for Linux
we have a school project and it requires our C programs to work on Linux and is c99 compatible. 我们有一个学校项目,它需要我们的C程序在Linux上运行并且兼容c99。 Since I am working on Windows 10, I installed Cygwin and assumed that if it will compile in cygwin, it will most certainly work in Linux.
由于我在Windows 10上工作,因此我安装了Cygwin,并假定如果它将在cygwin中编译,则肯定可以在Linux中运行。 It works fine with windows and I tried to compile it in cygwin and it works as well.
它可以在Windows上正常工作,我尝试在cygwin中编译它,并且也可以正常工作。 So can I assure that it will work in Linux and is C99 Compatible?
因此,我可以确保它可以在Linux中运行并且与C99兼容吗? If no, why not?
如果没有,为什么不呢? I am only using stdio and stdlib
我只使用stdio和stdlib
You can never be sure. 您永远无法确定。 Even though your program seems to work it may still contain bugs that invoke undefined behaviour.
即使您的程序似乎可以运行,它可能仍然包含调用未定义行为的错误。
However, you can reduce the risk of malfunctions by using best practice, for example: 但是,您可以通过使用最佳实践来降低发生故障的风险,例如:
Heed compiler warnings 注意编译器警告
Turn up compiler warning. 出现编译器警告。 For example:
例如:
gcc -Wall -Wextra -pedantic -std=c99
This turns up the warning level to a fairly high level. 这会将警告级别提高到相当高的级别。 .
。 Compiler warnings are often indications of bugs, so fix them all.
编译器警告通常是错误的征兆,因此请修复所有错误。 To ensure that you fix them, add
-Werror
so you can't run your code until all warnings have been fixed. 为了确保您已修复它们,请添加
-Werror
以便在所有警告均已修复之前无法运行代码。
-std=c99
tells the compiler to use the rules for the C99 version of the C standard. -std=c99
告诉编译器使用C标准的C99版本的规则。 The current version is C11 ( -stdc11
). 当前版本为C11(
-stdc11
)。 This is an important flag, and if omitted gcc
defaults to the older C89 standard. 这是一个重要的标志,如果省略,则
gcc
默认为旧的C89标准。
Use a tool to detect invalid use of memory 使用工具检测内存的无效使用
Tools like Valgrind (linux) will report common memory bugs like buffer overruns and memory leaks. 诸如Valgrind(linux)之类的工具将报告常见的内存错误,例如缓冲区溢出和内存泄漏。 Not sure which tool is most often used in Windows, but there is a wide selection listed here .
不确定Windows中最常使用哪种工具,但此处列出了多种选择。
If it is vanilla C99, at 99.9% if works on Cygwin it will work on any Linux. 如果是香草C99,则在Cygwin上可以达到99.9%,它将在任何Linux上运行。 I never saw a case that did not work.
我从未见过无效的案件。
Reverse is not always true, I would estimate ~ 98%, as linking could give some headache on some corner case. 反向并不总是正确的,我估计约为98%,因为在某些极端情况下链接可能会让人头疼。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.