3

Hello I'm looking into C Programming.

I'm wondering if there are differences between Linux and OSX in C? I know there are some between Windows and Linux/Unix (like getting a system timestamp). Are there any specific commands or techniques which won't work one of the two? Should "basic" programs run on both?

I'm aware that C isn't a cross compiling language but OSX and Linux are both Unix - aren't they?

arnoapp
  • 2,416
  • 4
  • 38
  • 70
  • 2
    o.O ... O_O ... :o -- "I'm aware that C isn't a cross platform language" - then you are aware of something completely wrong. C is a cross-platform language. –  Oct 16 '13 at 20:24
  • possible duplicate of [Is a Linux executable "compatible" with OS X?](http://stackoverflow.com/questions/9439436/is-a-linux-executable-compatible-with-os-x) – zubergu Oct 16 '13 at 20:25
  • Maybe I should added isn't a cross-plattform language like Java is. Is cross-compiling language the better expression – arnoapp Oct 16 '13 at 20:26

2 Answers2

8

What changes is not the language itself, but the libraries (and related API calls). There is no difference between Mac OSX and Linux under this aspect, as long as you stick with standard POSIX calls. Both Linux and Mac OSX are POSIX-compliant systems.

Of course, when talking about proprietary Apple libraries, you can't expect to find them under Linux. But this is another problem. Same for Linux internals.

Note that we are talking about source compatibility, not binary compatibility. You won't have to modify your source code at all, but you will have to compile it for each platform separately.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
  • 1
    Yay, exactly, and one can't use the Linux API on OS X either. But standard C is "standard" for a reason. –  Oct 16 '13 at 20:27
3

Linux includes quite a few extensions over the basic POSIX standard that both Linux and Darwin follow (Linux is "standard" in that it is exactly like Linux). As Stefano notes, in many cases this is fine, but if you have a program that was written for Linux without concern for portability ("runs on both Ubuntu and SuSE" is not "portability"), you should expect to see some different behaviors and missing extensions. For instance, mremap() and pipe2() are Linux-specific functions. SOCK_NONBLOCK is a Linux-specific flag to socket(), etc. The man pages will typically indicate when something is Linux-specific in the "Conforming To" section.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610