4

I have several drivers using a resource in my code, of which only one can be defined. eg if I have the following defines: USB_HID, USB_SERIAL, USB_STORAGE. and I want to test that only one is defined, is there a simple way to do this? Currently I am doing it this way:

#ifdef USB_HID
  #ifdef USB_INUSE
    #error "Can only have one USB device"
  #else
    #define USB_INUSE
  #endif
#endif

#ifdef USB_SERIAL
  #ifdef USB_INUSE
    #error "Can only have one USB device"
  #else
    #define USB_INUSE
  #endif
#endif

... with one of these blocks for each USB_XXX driver. Is there more elegant way of doing this?

3 Answers 3

15
#if defined(USB_HID) + defined(USB_SERIAL) + defined(USB_STORAGE) != 1
#error Define exactly one of USB_HID, USB_SERIAL, USB_STORAGE
#endif
Sign up to request clarification or add additional context in comments.

3 Comments

Nice solution. I probably would have ended up with a nasty mash of && || operators.
Really nice, +1. @Mike Weller: me too :)
Legend answer, I was awash in operators before I saw this.
0

Yes, use the define operator such as:

#if defined (USB_HID) && defined (USB_INUSE)

Comments

0

Why not #elif ?

#if defined(USB_HID)
   #define USB_INUSE
#elif defined(USB_SERIAL)
   #define USB_INUSE
#endif

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.