Jump to content

Using #ifdef In A Library. Is This Possible?


Recommended Posts

i usually put functions that i use for several projects into libraries instead of adding the c-code to each project.

 

now i have some functions that have to behave different, depending on the pic i use, i.e. there are some lines that are different for slow or fast processors.

 

i used to have a

 

#ifdef slowpic

<code for slow version>

#else

<code for fast version>

#endif

 

construct in my source code, while having a

 

#define slowpic

 

in the globaldefs.h file when using a slow processor.

 

now i think about moving the whole stuff into a .lib file.

is it possible to still have this construct inside the c source, or how can is solve this to still have the possibility to switch between a slow and a fast version?

 

thanx for your help!

Link to post
Share on other sites

Of course you can use the #ifdef, but then you will be building processor specific libraries. This is perfectly valid if they use different toolchains like PIC16 and PIC18 targets, but you would do better to have two versions of the function, MyFunc_Fast() and MyFunc_Slow() and move the #ifdef to a header file which you are going to have to make anyway for the function prototype.

 

In the header, conditionally define the prototype and a

#define MyFunc MyFunc_xxxx

 

where xxxx is Fast or Slow according to the #ifdef.

 

That way the linker removes the unused function, and the correct version gets used.

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...