In my previous compiles I have received errors if the correct #include file was not present. Now I do not receive any compile errors, but the code results are not what is expected. One example is the use of the atof function (C code). This function should require the presence of the stdlib. In the past it did and if the include was not there a compiler error was generated. Now, the code compiles, but the result of the atof is garbage. I have searched for the correct setting to get it back as it was, but have not been able to discover it. Can anyone help?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
PS: I just checked the ISO C99 standard, and implicit function declarations were removed, so if you set the compiler option -std=c99 you should find that the omission generates an error rather than a warning.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In C if a function is used without a declaration, it is assumed to return an int and compiles in any case, you would not get an error because it is allowed in the language specification. It is not a good idea, but it is valid. You should get a warning at least, although that can be suppressed. Since warnings should be regarded as errors because they usually are, I suggest setting -Wall and -Werror. -Wformat is useful as well while you are at it.
As ever, even if you think it shows nothing, you should post the compile log and associated code.
If you have preciously compiled such code as C++ you would indeed have got an error, because in C++ it is not allowed.
It would have been garbage because the compiler would genereated code that assumed an int return and interpreted the bit pattern returned by atof incorrectly.
So the solution is to set the warning level appropriately and make warnings errors, or to compile as C++ (where you should still set the warning options in any case). Compiling as C++ is a good idea since the compiler is far more stringent, and in most cases good C code is valid C++ code. Moreover C++ has some useful features that you just might choose to use even if you don't go the wholel OO hog.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
However, if a function is used and is not included, C reports an unknown function. Without the include, the atof should be reported as an unknown function that cannot be resolved by the compiler. Instead, the compiler is allowing its use. Even the early C compilers didn't allow the use of a function without actual code being found.
I will see about setting the parameters you suggest as well.
Scott
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
No, you are confusing the compiler and the linker. The code for atof() is not in the header file, it is in the stanadrad library - header files are not libraries, they contain only the function prototypes. The standard library is linked by default, but without the prototype, the compiler cannot check the data types and assumes an int, which in this case is incorrect, but the linker does not have the information to detect that, which is why the compiler needs the prototype.
The compiler can detect the error is it later finds a conflicting definition like this:
int main()
{
int x = f() ; // implicit declaration of int f( void).
}
However the atof() error cannot be detected because the atof() implementation is object code in the library and is dealt with by the linker not the compiler.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In my previous compiles I have received errors if the correct #include file was not present. Now I do not receive any compile errors, but the code results are not what is expected. One example is the use of the atof function (C code). This function should require the presence of the stdlib. In the past it did and if the include was not there a compiler error was generated. Now, the code compiles, but the result of the atof is garbage. I have searched for the correct setting to get it back as it was, but have not been able to discover it. Can anyone help?
PS: I just checked the ISO C99 standard, and implicit function declarations were removed, so if you set the compiler option -std=c99 you should find that the omission generates an error rather than a warning.
Clifford
In C if a function is used without a declaration, it is assumed to return an int and compiles in any case, you would not get an error because it is allowed in the language specification. It is not a good idea, but it is valid. You should get a warning at least, although that can be suppressed. Since warnings should be regarded as errors because they usually are, I suggest setting -Wall and -Werror. -Wformat is useful as well while you are at it.
As ever, even if you think it shows nothing, you should post the compile log and associated code.
If you have preciously compiled such code as C++ you would indeed have got an error, because in C++ it is not allowed.
It would have been garbage because the compiler would genereated code that assumed an int return and interpreted the bit pattern returned by atof incorrectly.
So the solution is to set the warning level appropriately and make warnings errors, or to compile as C++ (where you should still set the warning options in any case). Compiling as C++ is a good idea since the compiler is far more stringent, and in most cases good C code is valid C++ code. Moreover C++ has some useful features that you just might choose to use even if you don't go the wholel OO hog.
Clifford
However, if a function is used and is not included, C reports an unknown function. Without the include, the atof should be reported as an unknown function that cannot be resolved by the compiler. Instead, the compiler is allowing its use. Even the early C compilers didn't allow the use of a function without actual code being found.
I will see about setting the parameters you suggest as well.
Scott
No, you are confusing the compiler and the linker. The code for atof() is not in the header file, it is in the stanadrad library - header files are not libraries, they contain only the function prototypes. The standard library is linked by default, but without the prototype, the compiler cannot check the data types and assumes an int, which in this case is incorrect, but the linker does not have the information to detect that, which is why the compiler needs the prototype.
The compiler can detect the error is it later finds a conflicting definition like this:
int main()
{
int x = f() ; // implicit declaration of int f( void).
}
float f( void ) // error: conflict with earlier implicit declaration
{
return 0.0f
}
However the atof() error cannot be detected because the atof() implementation is object code in the library and is dealt with by the linker not the compiler.
Clifford