-1

we have a school project and it requires our C programs to work on Linux and is c99 compatible. 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. It works fine with windows and I tried to compile it in cygwin and it works as well. So can I assure that it will work in Linux and is C99 Compatible? If no, why not? I am only using stdio and stdlib

user6595053
  • 39
  • 1
  • 5
  • 2
    Lol instead of voting, please enlighten me. You can help other people that way – user6595053 Sep 12 '16 at 12:08
  • it's difficult to help you with so little information, specially without code. But if you just use standard library, without writing filename inside your code, it will probably compile and run on linux as on you computer. Be sure to use "-Wall" flag when you compile and have no warning – Garf365 Sep 12 '16 at 12:14
  • 1
    If the code is plain C, and not using anything platform specific, then yes it should work on any compliant compiler. Note that the *code* should work, but not the executable file. C is only source-code platform independent. – Some programmer dude Sep 12 '16 at 12:15
  • also, to check on other platforms, you can use online compilers like ideone or coliru – Garf365 Sep 12 '16 at 12:16
  • Sorry I didn't include the code because my classmates might find this question and see my code and use it in their projects. My teacher will kill me lol. Alright thanks, btw I forgot to mention that I also used string.h, will that be okay? – user6595053 Sep 12 '16 at 12:20
  • string.h is included inside standard lib, so it's portable – Garf365 Sep 12 '16 at 12:21
  • Oh okay. Its nice to know that there are online compilers, I am new to C so idk much about it – user6595053 Sep 12 '16 at 12:21
  • But yeah, so what does Cygwin do exactly? – user6595053 Sep 12 '16 at 12:23
  • Cygwin is, basically, a single library that emulates POSIX system calls. There's also an environment built around the library to emulate the POSIX shell environment. So actually you *can* use some POSIX platform specific calls in your code, and it should compile fine on any other POSIX system (like Linux). – Some programmer dude Sep 12 '16 at 12:27
  • @user6595053: Basically, it provides (large parts of) the Linux API (programming interface) via a Windows DLL. The original intention was to enable compiling *Linux* programs for a *Windows* target. Some functions *cannot* be mimicked on Windows and are thus unavailable, and some things need to be taken into consideration to get code that is fully compatible both ways, but that's the short of it. Some kind of a counterpart on Linux is Wine, mimicking Windows functions on a Linux host so you can run Windows executables there. (Cygwin requires recompilation, though, while Wine doesn't.) – DevSolar Sep 12 '16 at 12:43
  • @DevSolar, There is the [Winelib](https://wiki.winehq.org/Winelib) project for this, AFAIK. Cygwin and this tech are equivalent, the runtime wine is closer to this """Linux on Windows""" thing. – MayeulC Sep 12 '16 at 12:52
  • Thanks for all the answers guys! It sure helped :) – user6595053 Sep 12 '16 at 13:34
  • Install VirtualBox on your Windows box, and install Linux in the VM. – jxh Sep 12 '16 at 16:30

2 Answers2

2

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.

-std=c99 tells the compiler to use the rules for the C99 version of the C standard. The current version is C11 (-stdc11). This is an important flag, and if omitted gcc defaults to the older C89 standard.

Use a tool to detect invalid use of memory

Tools like Valgrind (linux) will report common memory bugs like buffer overruns and memory leaks. Not sure which tool is most often used in Windows, but there is a wide selection listed here.

Community
  • 1
  • 1
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • `-Wextra` perhaps? `-Wall` is just the baseline, really (and a really annoying misnomer for that). And that should probably be `-std=c11` by now. – DevSolar Sep 12 '16 at 13:41
  • @DevSolar -Wextra is good. Adding! OP explicitly mentioned that the code should be C99. I've added some extra information on `-std` for clarification. – Klas Lindbäck Sep 12 '16 at 14:45
  • `-Werror` perhaps? Also, `valgrind` would be a Linux tool. – jxh Sep 12 '16 at 16:20
  • @jxh Thanks. I added a link to a list of Windows substitutes and added a mention of `-Werror`. – Klas Lindbäck Sep 13 '16 at 07:17
  • Also, most uninitialized variable warnings won't show up unless optimization is also enabled. – jxh Sep 13 '16 at 15:34
0

If it is vanilla C99, at 99.9% if works on Cygwin it will work on any 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.

matzeri
  • 8,062
  • 2
  • 15
  • 16
  • If it's vanilla C99, it will run on *any* platform with a C99 compiler. Not 99.9% of the time, *always*. That's the whole point of the language standard. Cygwin is for something else entirely -- those parts of the Unix / Linux API that are *not* covered by C99. – DevSolar Sep 12 '16 at 12:44
  • 1
    Always is the theory. Also C99 compliance of a compiler can not be really proved with a lot of efforts that is NEVER there. – matzeri Sep 12 '16 at 12:47
  • If it doesn't work it's because either the program, the compiler, or the library is non-compliant. And I have implemented large parts of a C99 library, *including* tests, for cross-platform use. But that is beside the point. Your answer is not answering the question IMHO. – DevSolar Sep 12 '16 at 12:50
  • Can you state for every compiler variant used on every Linux distribution that they are C99 100% compliant ? I doubt. For the rest feel free to disagree with me. – matzeri Sep 12 '16 at 13:03
  • This question is not about compiler bugs or limits of any given implementation. It is about Cygwin, and how much a user could rely on "works on Cygwin" to mean "works on Linux". To which your answer is OT. And since you seem to be reluctant to improve it, downvoted. – DevSolar Sep 12 '16 at 13:06
  • My answer is YES, but it seems you missed to understand it. – matzeri Sep 12 '16 at 20:59