Build/Link Order Question

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am writing some code to interface with a specific chip. As such, I have created two standalone files (chip.h and chip.c) to provide a set of functions ("library", but not in the official sense) that talk to the chip.

The idea is to incorporate these files into [any number of] other projects as needed. I want to define a parameter to be used in the chip.c file, but only if it is not defined elsewhere.

Currently I have:

projectDef.h: (chip.h actually included elsewhere)

#define RX_BUF_SZ  250

chip.h: (included in chip.c)

#include "projectDef.h"

#ifndef RX_BUF_SZ
	#define RX_BUF_SZ	100		
#endif

I believe that, as written, this will give me the behavior I am looking for (because the #include is before the #ifndef) - meaning, when referenced in the chip.c file, RX_BUF_SZ will be replaced by 250 (not 100). If this is wrong, please let me know.

Is there a better way to do this? By having the #include "projectDef.h" line, I have now made this file slightly less portable (i.e., that line would need to be changed for every project). From a logical standpoint, I don't see a way around this, but I am hoping my ignorance of the build process may be clouding my judgement.

Be gentle...

Science is not consensus. Science is numbers.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

If this is wrong, please let me know.

No that achieves what I think you want here but be very wary of making default choices. For example (which usually relies on the user to have define F_CPU to say what the chip speed is does this:

#ifndef F_CPU
/* prevent compiler error by supplying a default */
# warning "F_CPU not defined for "
# define F_CPU 1000000UL
#endif

true there is a #warning but these go easily unmissed and if the user (in this case) is really using a 4MHz CPU but just forgot to set F_CPU then the rouintes end up working on the wrong assumption that the CPU speed is 1MHz. IMHO this would far better as:

#ifndef F_CPU
/* create compiler error as a default is madness */
# error "sorry but  is not willing to play until you define F_CPU"
#endif

Now maybe your "100" above is a sensible default, but then again maybe the OP is going to get all kinds of "odd" behaviour because the buffer isn't big enough.

Most projects have some kind of "config.h" or "proj_def.h" or whatever, especially if "configurable" software is involved so I don't think it's onerous for the user to have to supply one. In fact it may even have:

#ifndef _proj_def_h
#define _proj_def_h

//other stuff

#endif

as many headers do to prevent multiple inclusion. So you could have in the other chip.h file:

#ifndef _proj_def_h
#error "you have to setup a proj_def.h to use this, otherwise all bets are off"
#endif

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good point. I think that the 100 default is reasonable in this case, but also think your suggestion of a compile error message is better and ultimately safer. I will incorporate that into the code.

How do I guarantee that the error message I want prints out (i.e., the compiler gets to my error before the macro is used somewhere else in the code).

Science is not consensus. Science is numbers.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you use

#if (condition)
#  error "error message"
#endif

Then the code will not compile while the condition is true and the error will be shown in the compilation log, a hex will not be created until the error is resolved.

Alex

"For every effect there is a root cause. Find and address the root cause rather than try to fix the effect, as there is no end to the latter."
Author Unknown