编写跨平台C ++代码(Windows,Linux和Mac OSX) [英] Writing cross-platform C++ Code (Windows, Linux and Mac OSX)

查看:2014
本文介绍了编写跨平台C ++代码(Windows,Linux和Mac OSX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试编写任何在C ++中稍微复杂的东西,我试图构建一个可以与Objective-C和.NET应用程序接口的共享库(确定,稍后部分。 。)

This is my first-attempt at writing anything even slightly complicated in C++, I'm attempting to build a shared library that I can interface with from Objective-C, and .NET apps (ok, that part comes later...)

我的代码是 -

#ifdef TARGET_OS_MAC
  // Mac Includes Here
#endif

#ifdef __linux__
  // Linux Includes Here
  #error Can't be compiled on Linux yet
#endif

#ifdef _WIN32 || _WIN64
  // Windows Includes Here
  #error Can't be compiled on Windows yet
#endif

#include <iostream>

using namespace std;

bool probe(){
  #ifdef TARGET_OS_MAC
    return probe_macosx();
  #endif
  #ifdef __linux__
    return probe_linux();
  #endif
  #ifdef _WIN32 || _WIN64
    return probe_win();
  #endif
}

bool probe_win(){
  // Windows Probe Code Here
  return true;
}

int main(){

  return 1;
}



我有一个编译器警告,只需 untitled:In函数'bool probe()':untitled:29:warning:控制到达非void函数的结束 - 但我也很感激任何人可能建议如何写这种类型的信息或资源的代码更好....

I have a compiler warning, simply untitled: In function ‘bool probe()’:untitled:29: warning: control reaches end of non-void function - but I'd also really appreciate any information or resources people could suggest for how to write this kind of code better....

推荐答案

我将谈到这个特定的功能:

I'll address this specific function:

bool probe() {
#ifdef TARGET_OS_MAC
  return probe_macosx();
#elif defined __linux__
  return probe_linux();
#elif defined _WIN32 || defined _WIN64
  return probe_win();
#else
#error "unknown platform"
#endif
}


b $ b

这样编写if-elif-else链,消除了错误,因为它不可能没有一个有效的return语句或者敲打#error来编译。

Writing it this way, as a chain of if-elif-else, eliminates the error because it's impossible to compile without either a valid return statement or hitting the #error.

(我相信WIN32是为32位和64位Windows定义的,但是我不能确定地告诉你,没有查找它。这将简化代码。)

(I believe WIN32 is defined for both 32- and 64-bit Windows, but I couldn't tell you definitively without looking it up. That would simplify the code.)

不幸的是,你不能使用#ifdef _WIN32 || _WIN64:请参阅 http://codepad.org/3PArXCxo 以获取示例错误消息。

Unfortunately, you can't use #ifdef _WIN32 || _WIN64: see http://codepad.org/3PArXCxo for a sample error message. You can use the special preprocessing-only defined operator, as I did above.

有关如何使用 根据功能或整个文件拆分平台(如建议),您可能会也可能不想这样做。它将取决于您的代码的详细信息,例如平台之间共享的内容以及您(或您的团队)最适合保持功能同步等问题。

Regarding splitting up platforms according to functions or entire files (as suggested), you may or may not want to do that. It's going to depend on details of your code, such as how much is shared between platforms and what you (or your team) find best to keep functionality in sync, among other issues.

此外,您应该在构建系统中处理平台选择,但这并不意味着您不能使用预处理器:为每个平台有条件地定义(由makefile或构建系统)宏。事实上,这是模板和内联函数通常最实用的解决方案,这使得它比尝试消除预处理器更灵活。

Furthermore, you should handle platform selection in your build system, but this doesn't mean you can't use the preprocessor: use macros conditionally defined (by the makefile or build system) for each platform. In fact, this is the often the most practical solution with templates and inline functions, which makes it more flexible than trying to eliminate the preprocessor. It combines well with the whole-file approach, so you still use that where appropriate.

你可能需要一个配置头文件来翻译所有的编译器和平台特定宏到您控制的知名和理解的宏。或者你可以在你的编译器命令行中添加-DBEAKS_PLAT_LINUX来定义该宏(记住使用宏名称的前缀)。

You might want to have a single config header which translates all the various compiler- and platform-specific macros into well-known and understood macros that you control. Or you could add -DBEAKS_PLAT_LINUX to your compiler command line—through your build system—to define that macro (remember to use a prefix for macro names).

这篇关于编写跨平台C ++代码(Windows,Linux和Mac OSX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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