मैं यह काम करने की कोशिश कर रहा हूं (जीसीसी 4.6 में) मुझ पर भौंकने के बिना।
#define FOO ""
#define BAR ""
#if ....
#define FOO "Foo, good sir"
#endif
#if ...
#define BAR "Bar, my lady"
#endif
....
#define EVERYTHING FOO BAR ...
मेरे पास इनमें से बहुत कुछ होगा। तो इसके बजाय इसे इस तरह से कर रहे हैं:
#if ...
#define FOO "Foo"
#else
#define FOO ""
#endif
बहुत सारे कोड बचाता है, और इसे और अधिक पठनीय बनाता है। मुझे जो चेतावनी मिलती है वह है:
चेतावनी: "फू" फिर से परिभाषित [डिफ़ॉल्ट रूप से सक्षम]
क्या इस विशेष खंड के कोड में इस चेतावनी को अक्षम करने का कोई तरीका है? मुझे कुछ चेतावनियों को अक्षम करने के लिए Diagnostic Pragmas मिला, लेकिन मैं सक्षम नहीं हूं यह पता लगाने के लिए कि कौन सी चेतावनी (चेतावनी का अनुरोध करने या दबाने के विकल्प की इस सूची में है ) जिसे यहां अक्षम करने की आवश्यकता है।
क्या कोई जानता है कि इसे कैसे करना है? या उन सभी को खाली स्ट्रिंग में #else #define
रखने से बचने का एक अलग तरीका?
3 जवाब
यह चेतावनी gcc में "cccp.c" नाम की फ़ाइल से आती है (2.95 संस्करण के अनुसार; क्या यह फ़ाइल "सोवियत रूस" की है?), और इसे बंद नहीं किया जा सकता है। इस चेतावनी को व्यक्तिगत रूप से git हेड, gcc/libcpp/macro.c फ़ाइल (पंक्ति 2527 और लाइन 2994 उसी फाइल की)
मैं थोड़ा सूत्रों का हवाला दूंगा।
2525 /* Returns nonzero if a macro redefinition warning is required. */
2526 static bool
2527 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
2528 const cpp_macro *macro2)
2529 {
...
2537 /* Suppress warnings for builtins that lack the NODE_WARN flag. */
..
2545 /* Redefinitions of conditional (context-sensitive) macros, on
2546 the other hand, must be allowed silently. */
...
2550 /* Redefinition of a macro is allowed if and only if the old and new
2551 definitions are the same. (6.10.3 paragraph 2). */
...
2561 /* Check parameter spellings. */
...
2566 /* Check the replacement text or tokens. */
...
2573 for (i = 0; i < macro1->count; i++)
2574 if (!_cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i]))
2575 return true;
तो आपके मामले में warn_of_redefinition
फ़ंक्शन सही होगा। और यहाँ वास्तविक उपयोग है:
2989 if (node->type == NT_MACRO)
2990 {
2991 if (CPP_OPTION (pfile, warn_unused_macros))
2992 _cpp_warn_if_unused_macro (pfile, node, NULL);
2993
2994 if (warn_of_redefinition (pfile, node, macro))
2995 {
2996 const int reason = (node->flags & NODE_BUILTIN)
2997 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
2998 bool warned;
2999
3000 warned = cpp_pedwarning_with_line (pfile, reason,
3001 pfile->directive_line, 0,
3002 "\"%s\" redefined",
3003 NODE_NAME (node));
3004
3005 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3006 cpp_error_with_line (pfile, CPP_DL_NOTE,
3007 node->value.macro->line, 0,
3008 "this is the location of the previous definition");
3009 }
3010 }
तो, कोई विशिष्ट विकल्प नहीं है। और ग्रेग द्वारा उत्तर इस मामले के लिए अच्छा है, बस अपनी खाली स्ट्रिंग को फिर से परिभाषित करने से पहले परिभाषित करें।
#undef
का उपयोग करके देखें:
#define FOO ""
#if ....
#undef FOO
#define FOO "Foo, good sir"
#endif
या अगर और का उपयोग करने का प्रयास करें।
#if ...
# define FOO "Foo, doof sir"
#else
# define FOO ""
#endif
संबंधित सवाल
नए सवाल
gcc
जीसीसी जीएनयू कंपाइलर कलेक्शन है। यह लिनक्स पर C, C ++, Go, Fortran, और Ada के लिए वास्तविक मानक संकलक है और साथ ही कई अन्य भाषाओं और प्लेटफार्मों का समर्थन करता है।
-Wp,-w
का उपयोग कर सकते हैं