C ++:平台相关类型 - 最佳模式 [英] C++: Platform dependent types - best pattern

查看:112
本文介绍了C ++:平台相关类型 - 最佳模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种模式来组织C ++中多个平台的头文件。

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

我有一个包装器.h文件,应该在Linux和Win32 。

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

这是我最好的吗?

// 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...



<我真的不喜欢C ++中的预处理器东西。

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

推荐答案

您应该使用一个能够执行平台检查的配置脚本并生成相应的编译器标志和/或配置头文件。

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

有几个工具可以执行此任务,例如autotools Scons Cmake

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

在你的情况下,我建议使用CMake,因为它很好地与Windows集成,能够生成Visual Studio项目文件以及Mingw makefile。

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.

这是一个注释的CMake示例(CMakeFiles。 txt):

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)

这样,你必须提供一个config-cmake.h.in模板,将由cmake处理以生成一个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@

我邀请您前往cmake网站以了解有关此工具的更多信息。

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

我个人是cmake的粉丝,我用于我的个人项目。

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

这篇关于C ++:平台相关类型 - 最佳模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆