Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using macros to share code between two projects

Status
Not open for further replies.

jsnes

Programmer
Jul 31, 2004
2
US
I need merge two nearly identical .c files so that the new file remains 100% compatible with the first project (changing the actual code as little as possible), but will accomodate useage from the second project where the function calls are similar, but (for instance) have more parameters. I need to use macros in the .h as much as possible.

How do I make (.c file)

void foo (int x){...

merge with this

void foo (int x, int x){...

what's the best way to do this with a macro in the header?
 
Maybe something like this.
Code:
/* mycode.h */
#if   defined(PROJECTA)
void foo (int x);
#elif defined(PROJECTB)
void foo (int x, int y);
#else
#error Please #define either PROJECTA or PROJECTB.
#endif
Code:
/* mycode.c */
#include "mycode.h"

#if   defined(PROJECTA)
void foo (int x)
{
   /* ... */
}
#elif defined(PROJECTB)
void foo (int x, int y)
{
   /* ... */
}
#endif
Code:
/* main.c */
#include "mycode.h"

void bar(void)
{
#if   defined(PROJECTA)
   foo(1);
#elif defined(PROJECTB)
   foo(1,2);
#endif
}
Or maybe this.
Code:
/* mycode.h */
#if   defined(PROJECTA)
void foo (int x);
#define FOO(x,y) foo(x)
#elif defined(PROJECTB)
void foo (int x, int y);
#define FOO(x,y) foo(x,y)
#else
#error Please #define either PROJECTA or PROJECTB.
#endif
Code:
/* main.c */
#include "mycode.h"

void bar(void)
{
   FOO(1,2);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top