System programming in C

Hi guys, i'm programming in C for Linux but i preferred to program in FreeBSD and some FreeBSD system calls are not available in Linux and i want to make my code portable but i don't now really how, but i think if i used some C preprocessors i can make it portable, and the problems is that i don't know how. I'd appreciate if someone enlightened me. Thank you in advance.

What you want to do is get a copy of Stevens 'Advanced Programming in the UNIX Environment'

It shows all of the system calls for linux/solaris/FreeBSD and shows how to get portability.

...your question involves a lot more than you may think.

1 Like

What system calls have you found to be missing?

1 Like

unless you are coding based on POSIX standards it doesn't matter what platform your are using. all of them will do the trick.
"Advanced Programming in the UNIX Environment" is one of the best (no i think the best) books for this problem.

1 Like

If you want portability, you should not be using FreeBSD specific APIs. Stick to the set of APIs defined by the POSIX.1 standard.

1 Like

The original APUE from Steven's has been reworked by Rago, which gave birth the APUE 2nd edition, see APUE2e. This book is good, no point.

The most recent and comprehensive resource about UNIX system programming, is in my opinion Michael Kerrisk's book: The Linux Programming Interface . While this book deals with Linux specifics, it emphasizes on UNIX standards and writing portable programs.

HTH,
Lo�c

1 Like

I'm sorry about the delay on answering, now in "Advanced Programming" we are working with "Shared memory" to work with functions as shmat(),shmdt(), shmctl(),... and so on i need to include these libraries: sys/ipc.h, sys/ipc.h, sys/shm.h and machine/param.h, and the last one isn't a C library in Linux, i'm wondering how i can include then only if i am running FreeBSD.

AFAICS, the machine/param.h contains machine type dependent parameters. Try to compile your program on Linux without including this header file, then we'll see what is missing and where we can find it.

Lo�c

First of all, what you have mentioned here (i.e. all of *.h) files are the header files and not the libraries.

Secondly, whatever API you want to use, which are available in FreeBSD; just do the following command to search the appropriate header file in Linux (I'm talking general and not just for the header files you are mentioning here):

grep -nr <function_API_name> /usr/include/*

Where <function_API_name> is the actual function name you think available in a header file (.h file) under FreeBSD but not found by you under Linux.

This command will give you the header file containing the decleration of such function. You just need to include this header file now.