How to un-define a library function?

Lets say there is this library function : float foo(int anInt, float aFloat);

What is the syntax to un-define it?

#if defined foo
#undefine foo
#endif

Or is it.. #undefine float foo()?

or.. #undefine float foo(int,float)?

Never was all that zippy with the fancy #define stuff.

-jim lee

There is NO way to "undefine" a function. Why on earth would you even want to?

I didn't know you could undefine a function. Never even thought about it in several decades. If there is a conditional compilation statement around that function you could exclude it from the compilation using a define.

If your problem is a name collision you can use a namespace.

No.. I want to erase something from existence.

But, alas.. Seems I can't

-jim lee

jimLee:
No.. I want to erase something from existence

If the function is used, the linker will error (undefined reference). If the function isn't used it won't be included in the code. Why do you want to "erase it from existence"?
You have the source code to the library. You could make your own copy that does not include the function.

jimLee:
No.. I want to erase something from existence.

But, alas.. Seems I can't

-jim lee

But WHY??? If you USE the function, it MUST be defined, or the link will fail. If you don't USE the function, the linker will NOT include it in the object file. So what is the point? You're not saving space. You're certainly not improving performance. What do you imagine you will gain?

I was going to un-define delay() from one of my library files. It was just a nutty idea to try out.

-jim lee

You can however redefine a macro function:

#define delay(s) //delay(s)

:slight_smile:

A better way would be this:

#define delay(s) void

, but that throws a compiler warning. Not an error, so the code will run...

#define delay(x) ;

should not throw an error or warning.

But I still fail to see the point....

But I still fail to see the point....

A guess:

@jimLee wanted to see if the code would run differently without any delays, and to make his life easy, without going through the manual work of commenting them out

just an idea :slight_smile:

Jimmy Lee spends a lot of time trying to tech microcontrollers to young people. I suspect he is trying to permanently stop them from trying to use "delay()" in their coding.
The first thing is determine if "delay()" is a macro or an actual library.
Paul

interesting idea :slight_smile:

source code is right there

=> Not a macro.

J-M-L:
A guess:

@jimLee wanted to see if the code would run differently without any delays, and to make his life easy, without going through the manual work of commenting them out

just an idea :slight_smile:

If so, would have been nice for him to say so. The #define option above will do that, if used properly, but it is not, technically "undefining" the underlying function itself. So, if he had explained better, WHY, he would have gotten a suitable answer much more quickly.

Haha!

Well @Paul_KD7HB got the closest to what I was thinking. I was thinking that I could un-define delay() in one of my library files so that, once you decided to include it, delay() wold vanish. Therefore causing a compile fail if you had it in your code somewhere.

Jimmy lee - Now that's a name I've not heard in a long time..

-jim lee

then define this#define delay(x) JimLee_says_delay

and the compiler will bark with

[color=orange]
exit status 1
'JimLee_says_delay' was not declared in this scope
[/color]

:slight_smile:

OK, so that's yet another twist. You WANT the compile to fail if the function is referenced. Don't think you mentioned that before. THAT you can do by simply creating a #define that renames the function:

#define delay(x) xyzzy(x)

If you use delay, the compile will fail, unless you have a function named xyzzy() somewhere.

BUT.... There are a couple of caveats:

You will have to include that #define into EVERY "compile unit" you want to "monitor" for delay() calls. That likely means, for non-trivial projects, putting the #define into multiple files.

You MUST ensure the #define is parsed AFTER the delay() function is actually defined, or it won't work.

May be others....

J-M-L:
then define this#define delay(x) JimLee_says_delay
and the compiler will bark with

[color=orange]

exit status 1
'JimLee_says_delay' was not declared in this scope
[/color]




:)

Eww! I like it! I'll give that a try.

thanks!

@RayLivingston Its just a lark, it's Ok of it doesn't work. And, Its not for locating delay(), its more of keeping one honest. So it only has to fail the compile if it finds any dealy()s.

-jim lee

See also “pragma poison” or something like that.

Wow! Gone from impossible all the way to a #pragma directive.

-jim lee