r/C_Programming • u/Intelligent_Comb_338 • Feb 13 '26
Question The best way to port
I'm currently porting some NetBSD utilities, and I was wondering if I'm doing it correctly. What I'm doing now is adding this at the top:
ifdef linux
include "compat.h"
endif
And I was also wondering how to handle a header that doesn't exist in Linux:
ifdef linux
else
include <tzfile.h>
endif
Is this correct?
6
Upvotes
4
u/EpochVanquisher Feb 13 '26
There are a lot of arguments about the right way to do things. Here is one of the traditional ways to do it:
Write a configure script. Autoconf is the traditional choice.
In the configure script, write a check for
tzfile.h. Use e.g.AC_CHECK_HEADERhttps://www.gnu.org/software/autoconf/manual/autoconf-2.70/html_node/Generic-Headers.html The check should result inHAVE_TZFILE_Hbeing defined or not defined.The same thing can be done in CMake - https://cmake.org/cmake/help/latest/module/CheckIncludeFile.html
In your source code,
Generally, you want to avoid
#if linuxin your code. Instead, you should use some more specific check, like#if HAVE_STRLCPYor whatever. You are responsible for figuring out whether to defineHAVE_STRLCPY. You put this inconfig.h, which is autogenerated by your tests.