Strange LLVM warning: no previous prototype for function for

If I missed the prototype, Xcode (LLVM) will offer me an error

previous function prototype for exception Handler

But why are they needed in my code below?

void exceptionHandler(NSException * exception); // Why this Line is needed? void exceptionHandler(NSException * exception) { // .... } @implementation AppDelegate - (void) applicationDidFinishLaunching:(UIApplication *)application { NSSetUncaughtExceptionHandler(&exceptionHandler); ... 
+4
source share
4 answers

If you declare a function to be used only in this file, the declaration prefix with the keyword static and the warning disappears. Be that as it may, you are declaring a global function; theoretically, this could be called from anywhere in your application. But since you did not give him a prototype, no one could name him.

Therefore, the warning, as I understand it, is trying to make you clarify your intentions between static functions and global functions and to prevent you from declaring a global function when you want to declare only static.

+5
source

In the GCC manual:

-Wmissing-prototypes (C and Objective-C only)

Warn if a global function is defined without a prior prototype declaration. This warning is issued even if the definition itself provides a prototype. The goal is to discover global functions that cannot be declared in header files.

Clang borrowed this option for GCC compatibility and because it is useful (I would suggest this from Clang developers).

The option exists, so you can prevent you from making a general mistake that can be easily avoided. It's nice to be frank about visibility / anchor for clarity / intention.

In short, you asked the compiler to tell you when an unqualified definition does not match the declaration by enabling this option. You must either qualify it as extern and make it usable by others (for example, put it in the header), or declare it static . If using C ++ inline also an option.

Of course, implicit visibility is well known, but I usually find this option useful in these scenarios:

1) I made a typo:

 // file.h extern void MONExceptionHandler(NSException * exception); 

and

 // file.m void MONExceptionhandler(NSException * exception) { … 

2) I must clearly indicate the visibility of the symbol:

 // file.m static void MONExceptionHandler(NSException * exception) { … 

3) I forgot the #include header, which declared the function:

 // file.h extern void MONExceptionHandler(NSException * exception); 

Attention:

 // file.m void MONExceptionhandler(NSException * exception) { … 

No warning:

 // file.m #include "file.h" void MONExceptionHandler(NSException * exception) { … 

Thus, there is rationale, history, and some examples - again, -Wmissing-prototypes . If you trust yourself to work with it disabled, do it. My preferences should be clear and allow programs to detect potential and current problems, so I do not need to do this manually.

+15
source

I think this is most useful for C ++ code. For example, I have a title

 class MyClass { public: void hello(); }; 

and .cpp file

 void hello() { cout << "hello"; } 

And you will see a warning because there is no prototype for the void hello() function. If the correct implementation should be

 void MyClass::hello() { cout << "hello"; } 

Thus, this warning ensures that you are implementing a function that you know about (do not miss the typed name or other argument format).

-1
source

This warning warns that you cannot call your method from another method that is written above. In C, the order of declaration / implementation of minds is many and gives a difference between what you can access, or you cannot.

-1
source

Source: https://habr.com/ru/post/1390957/


All Articles