473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to tell if function has been declared?

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
Sep 7 '05 #1
10 2980
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
Sep 7 '05 #2
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
Sep 7 '05 #3
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
Sep 7 '05 #4
Victor Bazarov <v.********@comAcast.net> wrote in
news:c6******************@newsread1.mlpsca01.us.to .verio.net:
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.


See autoconf/automake, popularly used on many *NIX platforms (I've seen
Linux, Solaris, and HP/UX...) to determine which functions (and
libraries...) are available on the platform that you are currently
compiling on. Basically you run some sort of "configure" script before
compiling and it generates a config.h file with a whole bunch of #defines
representing what is and is not available. (But then we're going _way_
off-topic....)
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'?


To OP: If autoconf doesn't know about your platform, then you're still
out of luck. You'll still need to implement those functions on your own.
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"?


However, I'm with you. If you're doing platform (or compiler) -specific
#ifdefs, cause an error at the end if you don't recognize the compiler.
Better to blow up and cause one to examine the code to see if it's right
on a new compiler than to hope that it happens to work out... (which may
result in suboptimal implementations..... the code may have to assume a
C++-written function versus some carefully hand-crafted asm code which
may take full advantage of perhaps, say, a crypto co-processor....
(hypothetically speaking) )
Sep 7 '05 #5
On 07/09/2005, Andre Kostur wrote:

See autoconf/automake, popularly used on many *NIX platforms (I've
seen Linux, Solaris, and HP/UX...) to determine which functions (and
libraries...) are available on the platform that you are currently
compiling on. Basically you run some sort of "configure" script
before compiling and it generates a config.h file with a whole bunch
of #defines representing what is and is not available. (But then
we're going way off-topic....)


I need to support win32 compilers, so autoconf isn't really an option.

Thanks for all the suggestions and comments.

Boost comes with an interesting set of config files where various
vagaries and conformities of quite a few compilers are specified. But
boost have thousands of users who have tested against their compilers.

Instead of the suggested
#ifdef SOMETHING
#define MYmemicmp std::memicmp
#endif

would I be better doing something like this (I haven't tried to compile
this yet)?
#ifdef SOMETHING
namespace std
{
static inline int memicmp(const void *s1, const void *s2, size_t n)
{
return(MYmemicmp(s1, s2, n));
}
}
#endif

Or is it evil to add functions to namespace std? (In which case what
about those compilers, eg Borland C++ Builder, which do this?)

I realise that my version reverses the assignment of MYmemicmp and
std::memicmp, but I've got a ton of legacy code locked into other
developers' revision control, and I don't think I can go through it all
and get rid of all the std::memicmp references.

--
Simon Elliott http://www.ctsn.co.uk
Sep 7 '05 #6
Victor Bazarov <v.********@comAcast.net> writes:
What's 'autoconf'? I've never heard of it
Have you heard of Google?
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"?


No tool is universally "better" for everyone.

Autoconf is very useful if you need to write code that can configure and build
on a wide range of common OS/compiler combinations, including Unix variants,
Windows, Mac OS X, and others.

If that's not what you need to do, then autoconf is not so useful - for you.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
Sep 7 '05 #7
Simon Elliott wrote:
[..]
Or is it evil to add functions to namespace std? (In which case what
about those compilers, eg Borland C++ Builder, which do this?)
It's not evil. It's only illegal.

Implementors of the language (compiler creators) have much more
latitude when it comes to what they are allowed to do than mere
mortals that _use_ the compilers.
[..]


V
Sep 7 '05 #8
On Wed, 07 Sep 2005 12:14:08 -0400, Victor Bazarov <v.********@comAcast.net>
wrote:
Simon Elliott wrote:
[..]
Or is it evil to add functions to namespace std? (In which case what
about those compilers, eg Borland C++ Builder, which do this?)


It's not evil. It's only illegal.


Illegal? Nothing prevents it. It's merely unwise. ;-)

By the way, the discussion on autoconf, while interesting, has nothing to do
with the C++ language and should be taken to a different forum.
Sep 8 '05 #9
Dave Rahardja <as*@me.com> wrote in
news:h9********************************@4ax.com:
On Wed, 07 Sep 2005 12:14:08 -0400, Victor Bazarov
<v.********@comAcast.net> wrote:
Simon Elliott wrote:
[..]
Or is it evil to add functions to namespace std? (In which case what
about those compilers, eg Borland C++ Builder, which do this?)
It's not evil. It's only illegal.


Illegal? Nothing prevents it. It's merely unwise. ;-)


I thought there was a clause kicking around where mere mortals (as in not
compiler-writers) were not permitted to add additional identifiers to the
std:: namespace...
By the way, the discussion on autoconf, while interesting, has nothing
to do with the C++ language and should be taken to a different forum.


Only mentioned by way of explanation to Victor.... it's definitely in the
realm of platform-specific (even if it is many platforms.... :) )
Sep 8 '05 #10

Simon Elliott schreef:
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:


Yes, although it's a nasty one. IIRC, the trick is to provide a worse
alternative. E.g you provide an alternative memicmp with an ellipsis
parameter. This is done inside your own memicmp.cpp. You probably
want a using namespace std; in there as well. memicmp.h declares
only my::memicmp, which hides all this nastyness.

HTH,
Michiel Salters

Sep 8 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
8
by: Andreas Lagemann | last post by:
Hi, after browsing FAQ and archive for a while I decided that the following is a legal question. Consider this: Class Base { public: Base() {}
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
12
by: Steve Blinkhorn | last post by:
Does anyone know of a way of accessing and modifying variables declared static within a function from outside that function? Please no homilies on why it's bad practice: the context is very...
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
4
by: MichK | last post by:
Hello, I'm new in C#, and I'm working on some application to do direct audio recording and realtime plotting on screen. Now I'm having a lot of trouble to update the screen yet, it has been taking...
11
by: johnbrown105 | last post by:
Hello All, I am doing another exercise (I repeat, *exercise*). The (irrelevant to this discussion) point is to show that "You can inject a friend declaration into a namespace by declaring it...
28
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.