By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,543 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,543 IT Pros & Developers. It's quick & easy.

declaring a function only if it isn't already declared

P: n/a
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.

Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"no**********@tabi.org" <no**********@tabi.org> 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_
Nov 14 '05 #2

P: n/a
<no**********@tabi.org> wrote in message
news:ci********@odak26.prod.google.com...
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!

Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.