如何判断函数是否已声明? [英] How to tell if function has been declared?

查看:73
本文介绍了如何判断函数是否已声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以在编译时确定某个函数是否已经声明了?b / b
我的具体原因是因为memicmp()

由某些编译器在命名空间std中声明,而不是由其他编译器声明的stricmp()。我试过这个但很明显没办法了:


#ifndef memicmp

int memicmp(const void * s1, const void * s2,size_t n);

#endif


-

Simon Elliott http://www.ctsn.co.uk

解决方案

Simon Elliott写道:

编译时是否有办法确定某个函数是否已被声明?


没办法。

我的具体原因是因为memicmp()和stricmp()在命名空间std中声明了一些编译器而不是其他编译器。我试过这个,但显然没有工作:

#ifndef memicmp
int memicmp(const void * s1,const void * s2,size_t n);
#endif




当然没有。


我只能推荐这个宏(假设1和2)把它放在标准和3和

4不要):


#if定义(COMPILER_ONE)||定义(COMPILER_TWO)

#define MYmemicmp std :: memicmp

#define MYstricmp std :: stricmp

#elif defined(COMPILER_THREE)||定义(COMPIER_FOUR)

#define MYmemicmp memicmp

#define MYstricmp stricmp

#else

#error"编译器未知

#endif


(或类似的东西),然后在代码中使用''MYmemicmp''。

COMPILER_ONE和其他宏是为那些编译器定义的那些

具体来说,查看编译器文档。


BTW,那里没有标准功能''memicmp''和''stricmp'',你知道

那个,不是吗?


V


Victor Bazarov< v。******** @ comAcast.net>写道:

Simon Elliott写道:

在编译时是否有办法确定某个函数是否已被声明?


我只推荐这个宏(假设1和2把它放在std和3中,
4不要):

#if defined(COMPILER_ONE)||定义(COMPILER_TWO)


Yuck - 这很脆弱,并且一旦编译器2停止在标准中放置

非标准函数,它就会中断。 br />
#else
#error"编译器未知"


更糟糕!因为什么时候一个未知的编译器出错了?


一个更好的方法 - 与没有办法相反。上面的断言,是在数千个应用程序中常用的 -
是使用autoconf。


简而言之,你编写了一个试图编译的shell脚本并运行一个小的

测试应用程序,它使用您正在测试的功能。如果测试应用程序构建

确定并产生预期输出,则脚本返回零,否则

非零。


Autoconf产生配置脚本 - 你可能已经运行了很多

而不用担心它们是如何制作的。除此之外,这个

脚本将config.h.in文件作为输入,运行一系列测试脚本,如上所述

,并生成一个config.h头文件包含或评论各种宏

,以反映测试结果。


例如,您的config.h.in可能包含:


#define HAVE_MEMICMP


这可以按原样复制到config.h,也可以注释掉,具体取决于

检查memicmp()的结果。


autoconf方法比检查特定操作系统和/ b
或编译器更脆弱版本,并且在未知的b $ b操作系统和/或编译器上有更好的成功机会。


在这里查看更多:

< http://sources.redhat.com/autobook/>

BTW,没有标准功能''memicmp''和''stricmp''




更有理由在公司检查它mpile时间。而不是制作依赖于非标准功能的代码

,如果它们可用,或者使用更便携的标准,它可以利用它们 - 基本的替代方案,如果没有。


sherm--


-

Perl中的Cocoa编程: http://camelbones.sourceforge.net

雇用我!我的简历: http://www.dot-app.org


Sherm Pendley写道:

Victor Bazarov< v。******** @ comAcast.net>写道:

Simon Elliott写道:

编译时是否有办法确定函数是否已经
声明?

我只能推荐这个宏(假设1和2把它放在std和3中,
4不要):

#if defined(COMPILER_ONE)||定义(COMPILER_TWO)

Yuck - 这很脆弱,并且一旦编译器2停止将st />非标准函数放入std中就会中断。




是的。非标准行为(将非标准函数放入''std'')

无法用任何标准来对抗。只要任何编译器 -

特定解决方案停止工作,它就必须被注意并重新工作。

这就是为什么它是_compiler-specific_。

#else
#error" Compiler unknown"



更糟糕!因为什么时候是一个未知的编译器错误?




这个特定的代码是一个错误 - 一个未知的指标

必须将编译器考虑到此条件块中。一旦你添加它,就会没有更多的错误。

一个更好的方法 - 与没有办法相反。以上断言,在数千个应用程序中常用 - 就是使用autoconf。


什么'''autoconf''?我从来没有听说过它,而且我用
C ++编程超过10年。

简而言之,你写了一个shell脚本


在什么操作系统上?我没有贝壳。或脚本。并且

如果是这样的话,我旁边的系统上的操作系统要么没有,要么有不同的。

[...]

BTW,没有标准函数''memicmp''和''stricmp''



所有的更多的理由在编译时检查它。而不是使代码依赖于非标准功能,如果它们可用,它可以利用它们,或者如果没有,则可以使用更便携的基于标准的替代方案。




哪种更便携的基于标准的替代品是那些? ''autoconf''?


我建议的是处理_code_中的情况。你建议使用的是外部工具,而不一定是系统上的b / b
。它是如何更好的?


V


Is there a way at compile time to determine if a function has been
declared?

My specific reason for this is because of memicmp() and stricmp() which
are declared in namespace std by some compilers but not by others. I
tried this but evidently didn''t work:

#ifndef memicmp
int memicmp(const void *s1, const void *s2, size_t n);
#endif

--
Simon Elliott http://www.ctsn.co.uk

解决方案

Simon Elliott wrote:

Is there a way at compile time to determine if a function has been
declared?
There is no way.
My specific reason for this is because of memicmp() and stricmp() which
are declared in namespace std by some compilers but not by others. I
tried this but evidently didn''t work:

#ifndef memicmp
int memicmp(const void *s1, const void *s2, size_t n);
#endif



Of course it didn''t.

I can only recommend this macro (assuming 1 and 2 put it in std and 3 and
4 don''t):

#if defined(COMPILER_ONE) || defined(COMPILER_TWO)
#define MYmemicmp std::memicmp
#define MYstricmp std::stricmp
#elif defined(COMPILER_THREE) || defined(COMPIER_FOUR)
#define MYmemicmp memicmp
#define MYstricmp stricmp
#else
#error "Compiler unknown"
#endif

(or something like that), and then use ''MYmemicmp'' in your code. The
COMPILER_ONE and other macros are the ones defined for those compilers
specifically, check the compiler documentation.

BTW, there are no standard functions ''memicmp'' and ''stricmp'', you do know
that, don''t you?

V


Victor Bazarov <v.********@comAcast.net> writes:

Simon Elliott wrote:

Is there a way at compile time to determine if a function has been
declared?

I can only recommend this macro (assuming 1 and 2 put it in std and 3 and
4 don''t):

#if defined(COMPILER_ONE) || defined(COMPILER_TWO)
Yuck - That''s fragile, and will break as soon as Compiler Two stops putting
non-standard functions in std.
#else
#error "Compiler unknown"
Even worse! Since when is an unknown compiler an error?

A better approach - which, contrary to the "no way" assertion above, is in
common use in thousands of applications - is to use autoconf.

In a nutshell, you write a shell script that tries to compile and run a tiny
test app that uses the function you''re testing for. If the test app builds
OK and produces the expected output, the script returns a zero, otherwise a
non-zero.

Autoconf produces a "configure" script - you''ve probably run many of them
without ever worrying about how they were produced. Among other things, this
script takes a config.h.in file as input, runs a series of test scripts as
described above, and produces a config.h header with various macros either
included or commented, to reflect the results of the tests.

For instance, your config.h.in might contain:

#define HAVE_MEMICMP

This would either be copied as-is to config.h, or commented out, depending on
the result of the check for memicmp().

The autoconf approach is *far* less fragile than checking for specific OS and/
or compiler versions, and stands a much better chance of success on an unknown
OS and/or compiler.

Have a look here for more:
<http://sources.redhat.com/autobook/>
BTW, there are no standard functions ''memicmp'' and ''stricmp''



All the more reason to check for it at compile-time. Instead of making code
that''s dependent on non-standard functions, it can take advantage of them if
they''re available, or use more portable standards-based alternatives if not.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


Sherm Pendley wrote:

Victor Bazarov <v.********@comAcast.net> writes:

Simon Elliott wrote:

Is there a way at compile time to determine if a function has been
declared?

I can only recommend this macro (assuming 1 and 2 put it in std and 3 and
4 don''t):

#if defined(COMPILER_ONE) || defined(COMPILER_TWO)

Yuck - That''s fragile, and will break as soon as Compiler Two stops putting
non-standard functions in std.



Yes. Non-standard behaviour (putting non-standard functions in ''std'')
cannot be countered with anything standard. As soon as any compiler-
specific solution stops working, it has to be attended and re-worked.
That''s why it''s _compiler-specific_.
#else
#error "Compiler unknown"


Even worse! Since when is an unknown compiler an error?



It''s an error for this particular code -- an indicator that the unknown
compiler has to be factored into this conditional block. As soon as you
add it, there will be no more error.
A better approach - which, contrary to the "no way" assertion above, is in
common use in thousands of applications - is to use autoconf.
What''s ''autoconf''? I''ve never heard of it and I''ve been programming in
C++ for more than 10 years.
In a nutshell, you write a shell script
On what operating system? Mine doesn''t have a "shell" or a "script". And
if it does, the operating system on a system right next to mine either
doesn''t, or has a different one.
[...]

BTW, there are no standard functions ''memicmp'' and ''stricmp''


All the more reason to check for it at compile-time. Instead of making code
that''s dependent on non-standard functions, it can take advantage of them if
they''re available, or use more portable standards-based alternatives if not.



Which "more portable standard-based alternatives" are those? ''autoconf''?

What I suggested is taking care of the situation in the _code_. What you
are suggesting is the use of an external tool not necessarily available on
the system. How is it "better"?

V


这篇关于如何判断函数是否已声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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