Reliable portability of C code without using a preprocessor

Relying on the preprocessor and predefined compiler macros to achieve portability is difficult to handle. What is the best way to achieve portability for project C? I want the headers to indicate environment-specific code that behaves the same. Is there a way for the build environment to choose which headers to include?

I thought I put environment-specific headers in directories for specific environments. After that, the build environment will simply copy the headers from the platform directory to the root directory, build the project and delete the copies.

+3
source share
3 answers

C.

, - make :

INCDIRS=-I ./solaris
#INCDIRS=-I ./windows
#INCDIRS=-I ./linux
:
CC=gcc $(INCDIRS) ...

, . :

./solaris/io.h
./windows/io.h
./linux/io.h

, , , solaris.mk windows.mk, - .

, , , . , , . , #ifdef.

+6

, configure script - , make . GNU autoconf, , , , .

+3

pax , ,

  • ( , ) ().
  • , , . MyFileOpen(), fopen unix - . , , , MyFileOps.
+2
source

Source: https://habr.com/ru/post/1741701/


All Articles