检查一个系统是否实现了一个功能 [英] Check if a system implements a function
问题描述
我正在创建一个跨系统应用程序。它使用例如函数 itoa
,它在一些系统上实现,但不是全部实现。如果我只是提供自己的 itoa
实现:
header.h: 115:13:错误:'itoa'的冲突类型
extern void itoa(int,char []);
在header.h中包含的文件中:2:0,file.c中的
:2:0,
c:\path\to\mingw\include \stdlib.h:631:40:note:之前的'itoa'声明在这里
_CRTIMP __cdecl __MINGW_NOTHROW char * itoa(int,char *,int);
我知道我可以检查宏是否已预定义,如果不是,则定义它们:
#ifndef _SOME_MACRO
#define _SOME_MACRO 45
#endif
有没有办法检查C函数是否被预先实现,如果没有,实现它?或者简单地取消实现一个函数?
我假设您使用GCC,因为我可以在您的路径中看到MinGW。 ..有一种方式GNU链接器可以为你照顾这个。所以你不知道是否有 itoa
的实现。试试这个:
创建一个名为 my_itoa.c
的新文件(不带任何标题):
char * itoa(int,char *,int);
$ b $ char * my_itoa(int a,char * b,int c)
{
return itoa(a,b,c);
$ / code>
现在创建另一个文件 impl_itoa.c
。在这里,写下
itoa
的实现,但添加一个弱别名:
char * __attribute__((weak))itoa(int a,char * b,int c)
{
//在这里执行
}
编译所有文件,最后使用 impl_itoa.c
。
这样,如果 itoa
在标准库中不可用,这个链接就会被链接。你可以确信它是否可用。
I'm creating a cross-system application. It uses, for example, the function itoa
, which is implemented on some systems but not all. If I simply provide my own itoa
implementation:
header.h:115:13: error: conflicting types for 'itoa'
extern void itoa(int, char[]);
In file included from header.h:2:0,
from file.c:2:0,
c:\path\to\mingw\include\stdlib.h:631:40: note: previous declaration of 'itoa' was here
_CRTIMP __cdecl __MINGW_NOTHROW char* itoa (int, char*, int);
I know I can check if macros are predefined and define them if not:
#ifndef _SOME_MACRO
#define _SOME_MACRO 45
#endif
Is there a way to check if a C function is pre-implemented, and if not, implement it? Or to simply un-implement a function?
I assume you are using GCC, as I can see MinGW in your path... there's one way the GNU linker can take care of this for you. So you don't know whether there is an itoa
implementation or not. Try this:
Create a new file (without any headers) called my_itoa.c
:
char *itoa (int, char *, int);
char *my_itoa (int a, char *b, int c)
{
return itoa(a, b, c);
}
Now create another file, impl_itoa.c
. Here, write the implementation of itoa
but add a weak alias:
char* __attribute__ ((weak)) itoa(int a, char *b, int c)
{
// implementation here
}
Compile all of the files, with impl_itoa.c
at the end.
This way, if itoa
is not available in the standard library, this one will be linked. You can be confident about it compiling whether or not it's available.
这篇关于检查一个系统是否实现了一个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!