순전히 실용적인 수준에서, 나는 매일 이것을 처리합니다. 현재까지 가장 좋은 해결책은 전 처리기를 사용하는 것입니다. 내 공통 헤더 파일이 포함되어 사용하지 않은 사용의
//-------------------------------------------------------------------------
// Suppress nuisance compiler warnings. Yes, each compiler can already
// do this, each differently! VC9 has its UNREFERENCED_PARAMETER(),
// which is almost the same as the SUPPRESS_UNUSED_WARNING() below.
//
// We append _UNUSED to the variable name, because the dumb gcc compiler
// doesn't bother to tell you if you erroneously _use_ something flagged
// with __attribute__((unused)). So we are forced to *mangle* the name.
//-------------------------------------------------------------------------
#if defined(__cplusplus)
#define UNUSED(x) // = nothing
#elif defined(__GNUC__)
#define UNUSED(x) x##_UNUSED __attribute__((unused))
#else
#define UNUSED(x) x##_UNUSED
#endif
예는 다음과 같습니다
void foo(int UNUSED(bar)) {}
때때로 당신이 실제로 어설 션() 또는 디버그 문에, 예를 들어, 매개 변수를 참조 할 필요가있다. 당신이 할 수 있도록를 통해 : 또한
#define USED_UNUSED(x) x##_UNUSED // for assert(), debug, etc
, 다음은 유용합니다
#define UNUSED_FUNCTION(x) inline static x##_UNUSED // "inline" for GCC warning
#define SUPPRESS_UNUSED_WARNING(x) (void)(x) // cf. MSVC UNREFERENCED_PARAMETER
예 :
UNUSED_FUNCTION(int myFunction)(int myArg) { ...etc... }
과 :
void foo(int bar) {
#ifdef XXX
// ... (some code using bar)
#else
SUPPRESS_UNUSED_WARNING(bar);
#endif
}
멀티 - 언어 소스 파일 작성 (매우) 힘든 일입니다. 각 단일 소스 파일을 하나의 언어로만 유지하는 것이 좋습니다. – pmg
@pmg, 귀하의 조언에 감사드립니다.) – Alcott
C와 C++ 모두에서 컴파일 할 것이 있으면'printf'와'str :: cout'을 사용하는 별도의 함수를 작성할 필요가 없습니다. 'printf'는 두 언어 모두에서 작동합니다. 나는 그 기능이 동일하지 않을 수도 있기 때문에, 하나로서 쓰여질 수있는 두 가지 기능을 사용하는 것에 반대한다. –