Forcing GCC 4.x to handle - Return type as error without including -Werror?

Suppose we have the following code:

#if !defined(__cplusplus)
#  error This file should be compiled as C++
#endif

#include <stdio.h>
#include <string>

//#define USE_CXX_CLASS
#ifdef USE_CXX_CLASS
class SomeClass
{
public:
    SomeClass() {}
    ~SomeClass() {}
    std::string GetSomeString()
    {
        // case #1
    }
};
#endif // USE_CXX_CLASS

int foo()
{
    // case #2
}

int
main (int argc, char *argv[])
{
    (void)argc;
    (void)argv;
#ifdef USE_CXX_CLASS
    SomeClass someInstance;
    someInstance.GetSomeString();
#endif // USE_CXX_CLASS
    foo();
    return 0;
}

Suppose he was to compile a C ++ compiler (and not a C compiler) from GCC version 4.2.1 with parameters -Wreturn-type -Werror=return-type. If the above code is compiled, since it is not the first to uncomment the line //#define USE_CXX_CLASSabove, then you will see a warning , but without errors :

.../gcc-4.2.1/bin/g++   -g    -fPIC -Wreturn-type -Werror=return-type    test.cpp -c -o test.o
test.cpp: In function 'int foo()':
test.cpp:26: warning: control reaches end of non-void function

But if the line is //#define USE_CXX_CLASSuncommented, a warning treated as an error:

.../gcc-4.2.1/bin/g++   -g    -fPIC -Wreturn-type -Werror=return-type    test.cpp -c -o test.o
test.cpp: In member function 'std::string SomeClass::GetSomeString()':
test.cpp:18: error: no return statement in function returning non-void [-Wreturn-type]
gmake: *** [test.o] Error 1

, - , ( # 2), - ++ ( # 1). , . , , -Werror -Wall (, , ).

:

  • - GCC, , ? (, #pragma.)
  • , GCC?

, , :

+5
2

USE_CXX_CLASS. g++ - , - . g++ (GCC) 4.4.3 20100127 (Red Hat 4.4.3-4)

+1

, script gcc.

  • - gcc-wrapper g++-wrapper.
  • Makefile CC CXX .
  • GCC , .
  • , .
0

All Articles