Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Platform dependent types - best pattern [closed]

Tags:

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?

like image 282
Joe Schneider Avatar asked Jul 16 '10 15:07

Joe Schneider


People also ask

Is C language platform dependent or independent?

C is a portable programming language If you write a C code in your machine, it will run on any machine which supports C, without modifying a single line of code. Because it is not tied to any hardware or system. We can say, it is a hardware independent language or platform independent language.

Is .exe platform dependent?

So a *.exe file is a platform-dependent thing, because it says exactly how the computer will do what you told it to do.

What is mean by platform dependent and architecture dependent in C?

This is known as Platform Dependency. That means if we develop one application using Windows Operating System if that application can only be executed on Windows Operating System and can be run on other Operating Systems like Mac, Linux, this is called platform dependency.

Which programming language is platform dependent?

C Compiler is platform dependent since it is closely linked to the OS kernel which is different for different OS.


2 Answers

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.

like image 120
SirDarius Avatar answered Sep 24 '22 02:09

SirDarius


One way to do this is put the OS specific headers into different directories and control which directory is found by passing that directory to the compiler via the -I flag.

In you case you just would have

#include "defs.h" 
like image 29
mmmmmm Avatar answered Sep 25 '22 02:09

mmmmmm