Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 921885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:56:21+00:00 2026-05-15T18:56:21+00:00

I’m looking for a pattern to organize header files for multiple platforms in C++.

  • 0

I’m looking for a pattern to organize header files for multiple platforms in C++.

I have a wrapper .h file that should compile under both Linux and Win32.

Is this the best I can do?

// defs.h

#if defined(WIN32)
#include <win32/defs.h>
#elif defined(LINUX)
#include <linux/defs.h>
#else
#error "Unable to determine OS or current OS is not supported!"
#endif

//  Common stuff below here...

I really don’t like preprocessor stuff in C++. Is there a clean (and sane) way to do this better?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:56:22+00:00Added an answer on May 15, 2026 at 6:56 pm

    You should use a configuration script able to perform platform checks and generate the appropriate compiler flags and/or configuration header files.

    There are several tools able to perform this task, like autotools, Scons, or Cmake.

    In your case, I would recommend using CMake, as it nicely integrates with Windows, being able to generate Visual Studio project files, as well as Mingw makefiles.

    The main philosophy behind these tools is that you do not test again the OS itself, but against features that might or might not be present, or for which values can vary, reducing the risk that your code fails to compile with a “platform non supported” error.

    Here is a commented CMake sample (CMakeFiles.txt):

    # platform tests
    include(CheckFunctionExists)
    include(CheckIncludeFile)
    include(CheckTypeSize)
    include(TestBigEndian)
    
    check_include_file(sys/types.h  HAVE_SYS_TYPES_H)
    check_include_file(stdint.h     HAVE_STDINT_H)
    check_include_file(stddef.h     HAVE_STDDEF_H)
    check_include_file(inttypes.h   HAVE_INTTYPES_H)
    
    check_type_size("double"      SIZEOF_DOUBLE)
    check_type_size("float"       SIZEOF_FLOAT)
    check_type_size("long double" SIZEOF_LONG_DOUBLE)
    
    test_big_endian(IS_BIGENDIAN)
    if(IS_BIGENDIAN)
      set(WORDS_BIGENDIAN 1)
    endif(IS_BIGENDIAN)
    
    # configuration file
    configure_file(config-cmake.h.in ${CMAKE_BINARY_DIR}/config.h)
    include_directories(${CMAKE_BINARY_DIR})
    add_definitions(-DHAVE_CONFIG_H)
    

    With that, you have to provide a config-cmake.h.in template that will be processed by cmake to generate a config.h file containing the definitions you need:

    /* Define to 1 if you have the <sys/types.h> header file. */
    #cmakedefine HAVE_SYS_TYPES_H
    /* Define to 1 if you have the <stdint.h> header file. */
    #cmakedefine HAVE_STDINT_H
    /* Define to 1 if your processor stores words with the most significant byte
       first (like Motorola and SPARC, unlike Intel and VAX). */
    #cmakedefine WORDS_BIGENDIAN
    /* The size of `double', as computed by sizeof. */
    #define SIZEOF_DOUBLE @SIZEOF_DOUBLE@
    /* The size of `float', as computed by sizeof. */
    #define SIZEOF_FLOAT @SIZEOF_FLOAT@
    /* The size of `long double', as computed by sizeof. */
    #define SIZEOF_LONG_DOUBLE @SIZEOF_LONG_DOUBLE@
    

    I invite you to go to the cmake website to learn more about this tool.

    I’m personally a fan of cmake, which I’m using for my personal projects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.