Skip to content Skip to sidebar Skip to footer

Declared but Its Value Is Never Read

Understanding and fixing compiler and linker errors


By Alex Allain

It'southward your first C (or C++) program--it's non that long, and you're about to compile it. Y'all hit compile (or enter the build command) and wait. Your compiler spits out l lines of text. You pick out words like "warning and "error". Does that mean information technology worked? you wonder. You expect for the resulting executable. Goose egg. Damn, you lot call up, I guess I have to figure out what this all means...

The Types of Compilation Errors

Commencement, let's distinguish between the types of errors: most compilers will give three types of compile-time alerts: compiler warnings, compiler errors, and linker errors.

Although yous don't want to ignore them, compiler warnings aren't something severe enough to actually keep your program from compiling. Usually, compiler warnings are an indication that something might become wrong at runtime. How tin the compiler know this at all? Y'all might exist making a typical mistake that the compiler knows near. A common instance is using the assignment operator ('=') instead of the equality operator ('==') inside an if argument. Your compiler may likewise warn you most using variables that haven't been initialized and other similar mistakes. Mostly, you can set the alert level of your compiler--I like to keep information technology at its highest level then that my compiler warnings don't turn in to bugs in the running program ('runtime bugs').

Nevertheless, compiler warnings aren't going to stop you from getting your program working (unless you tell your compiler to treat warnings every bit errors), so they're probably a bit less frustrating than errors. Errors are weather condition that prevent the compiler from completing the compilation of your files. Compiler errors are restricted to unmarried source code files and are the upshot of 'syntax errors'. What this really means is that you've done something that the compiler cannot understand. For example, the argument "for(;)" isn't correct syntax because a for loop always needs to take three parts. Although the compiler would have expected a semicolon, it would also accept expected a conditional expression, so the error message you get might be something like "line 53, unexpected parenthesis ')'". Note, also, that compiler errors will always include a line number at which the fault was detected.

Even if you make information technology through the compilation process successfully, y'all may see linker errors. Linker errors, dissimilar compiler errors, have nothing to do with incorrect syntax. Instead, linker errors are usually problems with finding the definitions for functions, structs, classes, or global variables that were declared, simply never actually divers, in a source lawmaking file. Mostly, these errors will be of the form "could not find definition for Ten".

Usually, the compilation procedure volition begin with a serial of compiler errors and warnings and, once y'all've fixed all of them, you lot'll so be faced with whatsoever linker errors. In turn, I'll first embrace dealing with compiler errors and and then with linker errors.

Compiler Errors - Where do you start?

If you lot're faced with a list of fifty or sixty mistake and warning messages, information technology can be daunting to even endeavour to figure out where to start. The best identify, though, is at the beginning--as in, the beginning of the list. In fact, y'all should almost never start trying to prepare errors from the end of the file to the beginning for one simple reason: you lot don't know if they're actually errors!

A single error well-nigh the top of your program can cause a cascade of other compiler errors because those lines might rely on something early in the programme that the compiler couldn't understand. For instance, if you lot declare a variable with improper syntax, the compiler volition complain virtually that syntax fault and that information technology cannot find a proclamation for the variable. Leaving off a semicolon in the incorrect identify can effect in an astonishing number of errors. Things similar this can happen considering C and C++ syntax allows for things like declaring of a type immediately subsequently the type definition:

struct  {         int ten;         int y; } myStruct;                

This would create a variable, myStruct, with room to store a struct containing 2 integers. Unfortunately, this means that if you lot leave off a semicolon, the compiler will interpret it as though the next thing in the program is intended to be a struct (or return a struct). Something like this

struct MyStructType {         int x;         int y; }  int foo() {}                

can upshot in an surprising number of errors (mayhap including a complaint about an inapplicable "int" being ignored). All this for a single character! best to commencement at the top.

Dissecting an Error Message

Nearly letters from the compiler will consist of at least four things: the type of bulletin--alert or error--source code file in which the error appeared, and the line of the mistake, and a brief description of what was wrong. Output from g++ for the above plan might await something like this (your results with other compilers may vary):

foo.cc:7: mistake: semicolon missing later on struct declaration                

foo.cc is the proper noun of the file. seven is the line number in question, and it is clear that this is an error. The brief message hither is quite helpful considering information technology says exactly what was wrong. Observe, however, that the message makes sense just in the context of the program. It doesn't say which struct was missing a semicolon.

More cryptic was some other error bulletin from the same compilation attempt: "extraneous 'int' ignored". Information technology'south upwards to the programmer to figure out exactly why it was extraneous. Notice once more that this was an error caused by a trouble earlier in the programme, non on line eight, but earlier, when the struct lacked a semicolon terminator. Fortunately, it'due south pretty articulate that the function definition for foo was OK; this tells us that the fault must have been caused somewhere else in the program. In fact, information technology had to be before in the programme--y'all won't go an error message that indicates a syntax error prior to the line on which the error really occurred.

This brings up some other guiding principle of hunting down compiler errors: when in dubiousness, look earlier in the program. Since syntax errors can have mysterious repercussions later, information technology's possible that the compiler was giving a line number that doesn't really have a syntax mistake! Worse, many times, the compiler won't exist equally friendly in telling you exactly what happened earlier in the plan. Even the outset compiler mistake you go might be due to something several lines earlier the indicated alarm.

Treatment Ambiguous or Baroque Letters

There are several types of compiler errors that are especially frustrating. The first is the case of an undeclared variable that you swear you declared. Often times, y'all can actually point out exactly where the variable was declared! The problem is often that the variable is simply misspelled. Unfortunately, this tin can exist very hard to encounter since the mind typically reads what information technology expects rather than what is actually in that location. Worse, there are other reasons why this could be a problem too--scoping issues for instance!

To sort through the possible bug, i trick I like to use is to get to the line of the supposedly undeclared variable and have my text editor perform a search for the word under the cursor (alternatively, yous could copy the variable name and perform a search); this guarantees that if I spelled information technology incorrectly, it will not find a match for my search. This too keeps me from having to blazon the word, which could result in my correctly spelling the variable proper name.

A second cryptic message is the "unexpected end of file". What's going on here? Why would the stop of the file be "unexpected"? Well, the fundamental here is to recall like the compiler; if the end of the file is unexpected, and then it must be that it's waiting for something. What could information technology be waiting for? The answer is usually "closure". For instance, closing curly braces or closing quotes. A good text editor that performs syntax highlighting and automatic indentation should help fix some of these issues by making information technology easier to spot issues when writing lawmaking.

Ultimately, when a message is ambiguous, the manner to approach the trouble is to call up about how the compiler is trying to interpret the file. This tin be hard when you're just starting out, but if you pay attention to the letters and try to pick out what they could mean, yous'll quickly become used to the general patterns.

Finally, if naught else works, you tin can ever but rewrite a few lines of code to clear out whatever hidden syntax errors that might be hard for the eye to grab. This can be dangerous if you lot don't terminate up rewriting the correct section of code, but it can be helpful.

Linker Errors

One time yous've finally cleaned upward all those frustrating syntax errors, taken a nap, had a repast or two, and mentally prepared yourself for the program to build correctly, you may nevertheless demand to deal with linker errors. These can oftentimes be more than frustrating because they aren't necessarily the consequence of something written in your program. I'll briefly cover some of the typical types of linker errors you can await and some of the ways to prepare them.

Y'all may take issues with how yous fix your compiler. For instance, even if you include the correct header files for all of your functions, you withal need to provide your linker with the correct path to the library that has the bodily implementation. Otherwise, you will get "undefined office" error messages. Be careful that your compiler doesn't actually back up these functions at all (this could happen if you include your own declaration of a function to get around a compile-time error). If your compiler should back up the function, and so fixing this problem commonly requires compiler-specific settings. You'll by and large want to expect for how to tell the compiler where to await for libraries and make sure that the libraries were actually installed correctly.

Linker errors can also come about in functions that you have declared and defined if you neglect to include all of the necessary object files in the linking process. For example, if you write your class definition in myClass.cc, and your main role is in myMain.cc, your compiler will create two object files, myClass.o and myMain.o, and the linker will need both of them to finish the creation of the new program. If you exit out myClass.o, so it volition not accept the form definition even if yous correctly included myClass.h!

A sometimes subtle mistake is when the linker complains about at that place being more i definition for a class, function, or variable. This issue tin can come upwards in one of several ways: first, there might actually be two definitions of an object--for example, two global variables both alleged as external variables to be attainable outside of the source code file. This is a legitimate business concern for both functions and variables, and information technology definitely tin happen. On the other manus, sometimes the problem is with the directives to the linker; on more than one occasion, I've seen people include multiple copies of the same object file in the linking procedure. And bingo, you've got multiple definitions. A typical giveaway for this problem is that a whole host of functions have multiple definitions.

The concluding bizarre type of linker error is a complain about an "undefined reference to principal". This particular linker error differs from the other in that it may have nothing to do with including object files or having the correct paths to your libraries. Instead, information technology means that the linker tried to create an executable and couldn't figure out where the chief() function was located. This can happen if you forget to include the main function at all, or if you attempt to compile lawmaking that was never meant to be a stand up-alone executable (for instance, if you tried to compile a library).


Related articles

What'due south the difference between declaring and defining something in C and C++? Learn about the distinction between declaring a variable, class or function--and defining it--and why it matters when you have trouble compiling or linking your lawmaking

Learn more most dealing with compiler warnings Compiler warnings can indicate future bugs!

Compiling and Linking A cursory description of the compiling and linking process

The Static Keyword Covers the static keyword and how it can modify the accessibility of global variables

Using Namespaces Larn how namespaces can hide role and variable declarations

madridsart1973.blogspot.com

Source: https://www.cprogramming.com/tutorial/compiler_linker_errors.html

Enregistrer un commentaire for "Declared but Its Value Is Never Read"