“C”中的跨平台库 [英] Cross platform lib in 'C'

查看:109
本文介绍了“C”中的跨平台库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提供一个通用的平台无关功能声明

引用不同硬件的不同实现

平台。


在C中执行此操作的最安全的方式是什么?

解决方案

3月23日下午4:30 ,大屠杀 < boyle.a ... @ gmail.comwrote:


我想提供一个通用平台无关的函数声明

引用不同不同硬件的实现

平台。


在C中执行此操作的最安全的方式是什么?



我不确定我们是否真的相信一个不可知的功能。


无论如何,如果你用ANSI / ISO C编写,那么你的代码将是

便携式。


关于数据的可移植性 - 它不是。

您必须使用类似netCDF的东西来帮助您:
http://www.unidata.ucar.edu/software/netcdf/


这只是部分解决方案(它处理基本数据类型<仅限



" Carnage" < bo ******** @ gmail.comwrites:


我想提供一个通用的平台无关功能声明

它引用了不同硬件的不同实现

平台。


在C中执行此操作的最安全的方式是什么?



void func(void)与平台无关。


我不认为你的问题可以在没有更多信息

关于你想要做什么。你试图用什么函数
声明,它们应该做什么?


-

Keith汤普森(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


3月24日,10日:早上50点,Keith Thompson< k ... @ mib.orgwrote:


" Carnage" < boyle.a ... @ gmail.comwrites:


我想提供一个通用平台无关的函数声明

引用不同不同硬件的实现

平台。


在C中执行此操作的最安全的方式是什么?



void func(void)与平台无关。


我不认为你的问题可以在没有更多信息

关于你想要做什么。你试图用什么函数
声明,它们应该做什么?


-

Keith Thompson(The_Other_Keith)k ... @ mib.org< http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://用户。 sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长



例如。我想为一些文件打开功能提供一个接口但是

我的lib支持的每个平台都有不同的实现。


一种方式是这样的:


//File.h


#if定义(WIN32)

#include" FileWin32.h" ;

#elif定义(OTHEROS)

#include" FileOtherOs.h

#endif


#define OpenFile _OpenFile


//FileWin32.h

typedef void * FileHandle;

FileHandle _OpenFile (const char * name,const char * access);


//FileWin32.c

FileHandle _OpenFile(const char * name,const char * access)

{

// win32特定fopen等..

}


...类似于OtherOS ...

===========================

我认为可能更好的另一种选择是:


//File.h

typedef void *(* FileOpenFunction)(con st char * name,const char *

access);

extern FileOpenFunction FileOpen;


//FileWin32.cpp

static void * FileOpenWin32(const char * name,const char * access);


FileOpenFunction FileOpen = FileOpenWin32;


static void * FileOpenWin32(const char * name,const char * access)

{

// win32特定fopen等..

}

第二个例子对我来说似乎更好,因为没有平台

#if所需的东西,没有使用宏定义而且没有标题
$ b每个平台实现都需要$ b - 例如FileWin32.h不再需要



还有其他想法吗?


I want to provide a common platform agnostic function declaration
which references different implementations for different hardware
platforms.

What is the most type safe way to do this in ''C''?

解决方案

On Mar 23, 4:30 pm, "Carnage" <boyle.a...@gmail.comwrote:

I want to provide a common platform agnostic function declaration
which references different implementations for different hardware
platforms.

What is the most type safe way to do this in ''C''?

I''m not sure if we can really believe in an agnostic function.

At any rate, if you write in ANSI/ISO C, then your code will be
portable.

As to data portability -- it''s not.
You will have to use something like netCDF to help you there:
http://www.unidata.ucar.edu/software/netcdf/

And that is only a partial solution (it handles fundamental data types
only).


"Carnage" <bo********@gmail.comwrites:

I want to provide a common platform agnostic function declaration
which references different implementations for different hardware
platforms.

What is the most type safe way to do this in ''C''?

void func(void) is platform-independent.

I don''t think your question can be answered without more information
about what you''re trying to do. What function(s) are you trying to
declare, and what are they supposed to do?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


On Mar 24, 10:50 am, Keith Thompson <k...@mib.orgwrote:

"Carnage" <boyle.a...@gmail.comwrites:

I want to provide a common platform agnostic function declaration
which references different implementations for different hardware
platforms.

What is the most type safe way to do this in ''C''?


void func(void) is platform-independent.

I don''t think your question can be answered without more information
about what you''re trying to do. What function(s) are you trying to
declare, and what are they supposed to do?

--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

eg. I want to supply an interface for some file open functionality but
there are different implementations for each platform my lib supports.

One way would be something like:

//File.h

#if defined(WIN32)
#include "FileWin32.h"
#elif defined(OTHEROS)
#include "FileOtherOs.h
#endif

#define OpenFile _OpenFile

//FileWin32.h

typedef void * FileHandle;
FileHandle _OpenFile(const char * name, const char * access);

//FileWin32.c

FileHandle _OpenFile(const char * name, const char * access)
{
//Do win32 specific fopen etc..
}

...Similar for OtherOS...
===========================
Another alternative which I think might be better is:

//File.h
typedef void * (*FileOpenFunction)(const char * name, const char *
access);
extern FileOpenFunction FileOpen;

//FileWin32.cpp
static void * FileOpenWin32(const char * name, const char * access);

FileOpenFunction FileOpen = FileOpenWin32;

static void * FileOpenWin32(const char * name, const char * access)
{
//Do win32 specific fopen etc..
}
In the second example seems better to me since there is no platform
#if stuff required and no use of macro defines and also no header
required for each platform implementation - eg FileWin32.h is not
required any more.

Any other ideas?


这篇关于“C”中的跨平台库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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