Multiple Definitions of Same #define Macro in Requ

2019-08-20 09:54发布

问题:

Two of the libraries I am including share the same definition of a macro in each of their respective .h files.

#define MAX <some value>       //first definition of MAX in a file
#define MAX <some other value> //second definition of MAX in a *different* file

and in compilation I get

.../httpd.h:43:1: warning: "MAX" redefined

and

.../opencv2/core/types_c.h:272:1: warning: this is the location of the previous definition

I've checked each of these headers, and they have the #include guards.

What is the best way to fix this error (failing that, suppress the warning with a different -W flag)?

回答1:

The only bad part about this situation is dependencies on MAX in your code, if any. If you don't have any, adding an #undef MAX between the two #includes is probably the fastest fix. If you do have dependencies on MAX you might need to figure out which one (I guess it's the last :-) and do something appropriate.



回答2:

FYI, Nick, I ended up fixing this by changing the source, as AoeAoe mentioned in a comment above. Turns out that the only place httpd.h's MAX() and MIN() macros get used is in httpd.cpp, so I just moved those #defines into httpd.cpp, where they really should have been in the first place.

In fact, they may have been there in the original MJPG-Streamer code, and perhaps Robotis just mangled it when they picked out the pieces they wanted to use for the Darwin framework. At any rate, the current MJPG-Streamer code has #ifndef guards around MAX and MIN and also has them in a separate utils.h file here that just gets included in httpd.c, here, not httpd.h.



回答3:

Neither include guards nor compiler flags will help you here. You have approximately two possible solutions:

  1. Don't #include both headers into the same source file.
  2. Add an #undef MAX in-between the two #includes.