Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > declaring a function only if it isn't already declared

Reply
Thread Tools

declaring a function only if it isn't already declared

 
 
nospam_timur@tabi.org
Guest
Posts: n/a
 
      09-17-2004
I'm writing a Linux device driver that needs to compile with several
different Linux versions. In my code, I need to reference certain
functions by their address alone. Something like this:

int myfunc(char *x);
if (memory_test[x] == myfunc)
....

In other words, I don't care about the return values or the parameters
of myfunc(), I just need to reference it.

In my case, myfunc() isn't a function that I've defined, but it may be
declared in a header file that I'm including. The problem is that each
Linux version has different header files for myfunc(), so that I can't
really know which header file I need to include in order to get
myfunc() declared. Not only that, but the function declaration for
myfunc() isn't exactly the same, either.

Therefore, I was hoping for something like this:

#if !defined(myfunc)
void myfunc(void);
#endif

This doesn't work, of course, because myfunc() is not a macro, so
defined() doesn't work on it. All I want to do is create a prototype
for myfunc() that is guaranteed not to cause any function redefinition
errors.

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      09-17-2004
"" <> writes:

> In my case, myfunc() isn't a function that I've defined, but it may be
> declared in a header file that I'm including. The problem is that each
> Linux version has different header files for myfunc(), so that I can't
> really know which header file I need to include in order to get
> myfunc() declared. Not only that, but the function declaration for
> myfunc() isn't exactly the same, either.


If myfunc() always has the same return type, then you can write a
declaration for it without giving a prototype, e.g.:
int myfunc();
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
 
Reply With Quote
 
 
 
 
xarax
Guest
Posts: n/a
 
      09-18-2004
<> wrote in message
news:cifku3$...
> I'm writing a Linux device driver that needs to compile with several
> different Linux versions. In my code, I need to reference certain
> functions by their address alone. Something like this:
>
> int myfunc(char *x);
> if (memory_test[x] == myfunc)
> ...
>
> In other words, I don't care about the return values or the parameters
> of myfunc(), I just need to reference it.
>
> In my case, myfunc() isn't a function that I've defined, but it may be
> declared in a header file that I'm including. The problem is that each
> Linux version has different header files for myfunc(), so that I can't
> really know which header file I need to include in order to get
> myfunc() declared. Not only that, but the function declaration for
> myfunc() isn't exactly the same, either.
>
> Therefore, I was hoping for something like this:
>
> #if !defined(myfunc)
> void myfunc(void);
> #endif
>
> This doesn't work, of course, because myfunc() is not a macro, so
> defined() doesn't work on it. All I want to do is create a prototype
> for myfunc() that is guaranteed not to cause any function redefinition
> errors.


I just use header file for each function. Each
header file has something like:

// This is file name MyFunc.h
#if !defined(MY_FUNC)
#define MY_FUNC
extern int MyFunc(char * whatzit);
#endif //!defined(MY_FUNC)


I use #include to include the function's header
file in each compilation unit that wants to
refer to the function (either wanting to call
the function or just get the function's address).
The "MY_FUNC" preprocessor symbol is simply the
uppercase representation of the function name,
and the header file name is simply the function
name followed by ".h".

--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring static function returning pointer to extern function pembed2012 C Programming 1 02-27-2012 08:21 PM
Should an object declared in a function persist after function exits? Jeff Javascript 2 10-18-2006 05:58 AM
declared or not declared ? JohnZing ASP .Net 3 02-05-2006 08:38 PM
Declaring only public class members - doesn't work Chee Liang C++ 4 04-16-2004 03:13 AM
Re-forward declaration of types which were already forward declared qazmlp C++ 1 02-15-2004 07:00 PM



Advertisments