Objective-C warning)

Source: Internet
Author: User
Compilation warning

Now the compiler is sometimes very noisy, and the warning given by the compiler is very useful to developers. The warning will not stop compilation and linking, nor cause the program to be unable to run, but many times the compiler will first discover the problem, especially for objective-C. Clang not only provides warnings for obvious errors (for example, a method or interface is not implemented), but also prompts for many potential problems (for example, methods have been discarded or problematic conversion ), in many cases, these problems may become potential fatal errors and must be taken seriously.

Dynamic languages such as Ruby and PHP do not have the so-called compilation warning, while many of the warnings in C # or Java are discarded methods that have to be taken care, many developers are used to Ignoring warnings for development. OC is now maintained by Apple, and clang's llvm is also being implemented by Apple. It can be said that the entire process from language to compiler to SDK is in progress, therefore, warnings during OC development are often more useful than warnings in other languages. Enable as many warning prompts as possible and avoid generating warnings as much as possible during program development. This is required for building a robust and efficient program.

Enable additional warning in xcode

The xcode Project template has enabled some default and common warning prompts for our settings. These default settings are designed to be compatible with some projects of the previous year and are not opened much, only warnings are given to the most dangerous and common parts. This is not enough for a new project (at least not enough for me). Under the guidance of countless predecessors, the first thing to do is to open as many warnings as possible.

The simplest way is to open the warning through the UI. In xcode, the build setting option reserves some enable warnings for us. Find and directly select the corresponding options to enable warnings. In most of the time, the option itself is sufficient to describe the role of the warning and the time to generate the warning. If you do not understand it clearly, you can provide more details in the quick help panel on the right. Warnings specific to OC development areApple LLVM compiler 4.2 - Warnings - Objective CIn the column, whether you decide to open them or not, it is worthwhile to take a look at them, because they are the most avoided when writing OC programs. OthersApple LLVM compiler 4.2 - Warnings - …(All Versions ages and C ++) also contains a large number of options to facilitate control the generation of warnings.


Of course, it is simple to click activation warning one by one in the UI, but it is not interesting to do so every time, especially when you know what they are and decide to open them. Add the appropriate flag in the compilation option to enable or disable it. Warning: add the shape to other C flags in build setting.-W.... You can enter any number-W...To switch some warnings. For example, enter-Wall -Wno-unused-variableYou can enable the "all" warning (not all, but a majority of severe warnings), but do not enable the "no variables" Warning. Use-W...In the UI, rather than selecting the UI, a major benefit is that when the compiler version is updated, the newly added warning is-WallIf you do not need to modify the project, the new warning will take effect. In this way, you can immediately notice the potential risks of the same project when the compiler version is updated. Another more important reason is that the xcode UI does not provide all the warnings ==||| ..

As mentioned earlier, note that,-WallAlthough the name is all, it is really just a confusing word. In fact-WallOnly one subset of all warnings is covered. On stackexchange, a clang developer working on Google answered the question, explaining some important warning groups:

  • -Wall andNoAll warnings. This alert group enables warnings that compiler developers are highly confident about the proposition "there are problems with your code. If there is a warning in your code under this set, it is basically that your code has serious problems. But at the same time, it doesn't mean that everything is fine when you open wall, because wall only targets a few problems in the classic code library, so some fatal warnings cannot be captured by it. However, whatever the case, because Wall warnings provide credibility and high priority warnings, this set of warnings is opened for all projects (at least for all new projects, it should be a good habit.
  • -Wextra as its name,-WextraThe Group provides an "additional" Warning. This group and-WallGroups are almost equally useful, but in some cases code is too strict. A common example is,-WextraContains-Wsign-compareThis warning identifier checks the type of signed and unsigned during comparison. When both sides of the comparison operator are signed and unsigned, a warning is generated. In fact, many codes do not particularly care about such comparisons. In most cases, comparing signed and unsigned is not a big problem (of course, there will be fatal errors ). Note that,-WextraAnd-WallThey are two independent warning groups. Although some of the warning identifiers opened in them are duplicated, they do not have a contained relationship. To use it at the same time, you must add
  • -Weverything: All real warnings. However, developers generally do not choose to use this identifier because it contains warning prompts for bugs that may still exist in development. This identifier is generally used by compiler developers for debugging. If you want to enable it in your project, the warning will surely burst and you want to hit the wall ..


For instructions on which warnings are enabled for a group, refer to the GCC manual. Although Apple now uses llvm, this part of content should inherit the GCC settings.

Control warning, partial addition or closure

Clang provides our own methods to add or temporarily disable warnings.

Force a warning:

//Generate a warning
#pragma message "Warning 1"

//Another way to generate a warning
#warning "Warning 2"

The results of the two methods of force warning are the same in terms of visual effects, but the warning types are slightly different.-W#pragma-messagesAnd the other is-W#warnings. For the second method, replace warning with error to force the compilation to fail. For example, when publishing a class library that requires an API key, you can use this method to prompt other developers not to forget to enter necessary information.

//Generate an error to fail the build.
#error "Something wrong"

If you want to disable a warning globally, write it directly in other C flags.-Wno-...That's it, for example-Wextra -Wno-sign-compareIs a common combination. If the warning is enabled or disabled for a few files, add the corresponding compilation identifier to the file corresponding to the compile source of build phases. If you only want to disable a warning in a few lines, you can temporarily change the diagnostic compilation flag to suppress the warning of the specified type, as shown below:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"

int a;

#pragma clang diagnostic pop

If a is not used after a, no warning will be given for unused variables. You can view the names of the warning types you want to suppress in the build log after the warning is generated by build. In xcode, press the shortcut key cmd + 7 and click the latest build log to go to the details page.


What warnings should I enable?

Different personal preferences (Code Cleansing) have different requirements. My suggestion is to first enable-WallAnd-WextraBuild the project on this basis and avoid all warnings. If you encounter some problems that cannot be solved during the development process or are sure that your practice is correct (in fact, your practice is generally not wrong, is not so correct), you can selectively close some warnings. In general, the closed warning item should not exceed the number that can be counted by one hand, otherwise there must be something wrong ..

Whether to make warning equal to error

A common practice and code cleanliness is to mark warnings as errors and interrupt the compilation process. This forces developers to fix these warnings to keep the code clean and tidy. In xcode, you can select the appropriate treat warnings as errors to enable or add-WerrorId. I personally do not like this setting because it always interrupts the development process. In many cases, it is not possible to compile and debug all the Code. On the contrary, I prefer to compile and run the code to check the results. It is not surprising that a warning will appear during the debugging compilation process. In addition, a large number of normal warnings may occur during TDD development. If there is a warning that compilation is not allowed, the development efficiency may be compromised. A good practice is to treat the warning as an error only during release build, because xcode can specify the identifiers for debug and release separately, so this is easy to do.

In addition, some warnings can be considered as errors,-Werror=...You can also-WerrorUsed when activated-Wno-error=...To prevent some warnings from being errors. Using these compilation identifiers can achieve good control.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.