Will C/C++ code written under Linux compile and link in FreeBSD ?

If you use Linux's system call API or other Linux API's Documentation to write a C/C++/Assembly source code ... will the code compile and link/run under FreeBSD ?

How about the executable files ? Both systems use elf right ?
 
If you use Linux's system call API or other Linux API's Documentation to write a C/C++/Assembly source code ... will the code compile and link/run under FreeBSD ?
It depends on your definition of a Linux API call. If you mean kernel API call, then most likely no. The kernels are quite different. If you're talking about C library API calls, then yes, it will likely compile. Although the C libraries aren't 100% source compatible, Linux may have some functions that FreeBSD doesn't and visa versa. Some calls can also respond a little different or require different arguments.

That said, it's usually do-able. But may need some patches or #ifdef constructs. Just look at the number of ports in our ports tree. Most applications are written and developed on Linux and compile just fine with FreeBSD.

How about the executable files?
FreeBSD can execute Linux binaries using the Linux ABI layer: Chapter 10. Linux® Binary Compatibility
It works surprisingly well, but it's not 100% compatible. So some Linux binaries aren't going to like it.

Both systems use elf right ?
Yes, but this has little to do with execution, it's just a way to store an executable. See elf(5).
 
Although the C libraries aren't 100% source compatible, Linux may have some functions that FreeBSD doesn't and visa versa.
In general, you'll be quite safe if you limit yourself to functions that are part of the standard C library or a POSIX specification. Compile with -std=c11 -pedantic and use feature defines where you need POSIX functionality (e.g. #define _POSIX_C_SOURCE 200112L before any #includes in a source file that needs a function from POSIX.1-2001 or earlier) to make the compiler reject anything else and possibly platform-specific.
 
Back
Top