简体   繁体   English

Cygwin:Windows上的Linux? 如何确保Windows创建的程序适用于Linux

[英]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.

相关问题 Windows下用cygwin编译的C程序可以正常工作,Linux下分割错误。 Cygwin GCC是“坏”的吗? - C program compiled with cygwin in Windows works, segmentation fault under Linux. Is cygwin GCC 'bad'? 为什么此C代码在Linux上有效,但在没有cygwin的Windows上无效 - Why does this C code works on linux but not on Windows without cygwin 这个C程序在Windows中可以正常工作,但在Linux中不能正常工作 - This c program works fine in windows but not working in Linux “ Web浏览器”程序可在Windows中运行,但不适用于Linux - 'web browser' program works in Windows but not Linux 网络程序在Linux下工作,在Cygwin下不行 - Network program works in Linux, not under Cygwin 使用cygwin在Windows中链接Linux静态库 - linkning linux static libraries in windows with cygwin 如何在Linux中使用Windows套接字编译程序? - How to compile program with windows sockets in linux? 如何在windows上运行的linux上编译c程序 - how to compile c program on linux that runs on windows 我的程序在Windows机器上崩溃,但在Linux上运行正常 - My program crashes on the Windows machine yet it works fine on the Linux 为什么这个C程序在Windows上崩溃并且在linux上工作正常? - Why is this C program crashes on windows and works fine on linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM