hippietrail
hippietrail

Reputation: 16994

Is there a good online tutorial for writing portable C?

I have some tools I'm working on in portable C that works in Windows Visual Studio 2008 and gcc in Ubuntu Linux based on #ifdef _WIN32 but adding support for Solaris seems to be trickier, especially if I want to support cc as well as gcc.

For one example I have some code which sprintfs into an allocated memory buffer which uses vasprintf on Linux/gcc and _vscprintf/vsprintf on Windows/MSVC. Neither are available on Solaris where I could use vsnprintf but I have no idea what to add to my #ifdefs or whether I should move to something else.

Hopefully I don't have to move to configure with cygwin, mingw.

Upvotes: 3

Views: 368

Answers (2)

Steve Emmerson
Steve Emmerson

Reputation: 7832

The autoconf(1) manual has a section on portable C programming.

Upvotes: 1

Ben Stott
Ben Stott

Reputation: 2218

The only real way to do a test like this is to use something like gnu's autoconf + configure (or just plain configure). You can then test to see if vsprintf exists, failing that test for vasprintf, failing that test for vsnprintf, etc.. You can then get configure to define HAS_VSPRINTF and the like to use in your code, and write a wrapper function around the correct function.

This would be the most portable way to test,and the most portable way to then code up a solution, though perhaps also the most cumbersome - it'd definitely be what I'd do for production code though.

Upvotes: 1

Related Questions