Error[Pe020]: identifier "FILE" is undefined in IAR Workbench - file

// Preprocessor directive mention in <> formate :
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "string.h"
#include "limits.h"
#include "stddef.h"
#include "stdint.h"
...
int main()
{
FILE *fin, *fout; //Error[Pe020]: identifier "FILE" is undefined
...
...
...
fprintf(stderr, "%s\n", opus_get_version_string());
//Error[Pe020]: identifier "stderr" is undefined
}
As per above code I got an two Errors :
1) "Error[Pe020]: identifier "FILE" is undefined"
when I found stdlib.h then there are no any kind of "FILE" directive define or typedef.
So,please tell me which changes needed in stdlib.h or stdio.h
2) Error[Pe020]: identifier "stderr" is undefined
So,In which header file stderr define ?
Please tell me How to solve above errors ?
Thanks in advance.

First, FILE is in stdio.h not in stdlib.h. Second, the default library does not include FILE support since this feature uses a lot of space and is seldom needed in embedded systems. To use FILE you must switch to full libraries in the configuration dialog (if you use the IDE) or by using the command line switch --dlib full (if you use the compiler from the command line).
Note that the full library is much larger than the normal library so if the only use of FILE is to print diagnostic messages to stderr I suggest that you use some other way of presenting the messages.

FILE is defined in stdio.h standard header file. As Johan mentioned the default library configuration in IAR IDE does not include full Dlib support.
We can change to Full Dlib support under project options.
Project options shall be opened with ALT + F7 Hotkey or project tab and then options. Check this Image.
Project Options

Related

GCC throws error upon compilation: error: unknown type name ‘FILE’

I am making a function which is just writes "hello" to a file. I have put it in a different file and included its header in the program. But gcc is giving an error namely:
error: unknown type name ‘FILE’.
The code is given below
app.c:
#include <stdio.h>
#include <stdlib.h>
#include "write_hello.h"
int main() {
FILE* fp;
fp = fopen("new_file.txt", "w");
write_hello(fp);
return 0;
}
write_hello.h:
void write_hello(FILE*);
write_hello.c:
void write_hello(FILE* fp) {
fprintf(fp, "hello");
printf("Done\n");
}
when compiling by gcc the following occurs:
harsh#harsh-Inspiron-3558:~/c/bank_management/include/test$ sudo gcc app.c
write_hello.c -o app
write_hello.c:3:18: error: unknown type name ‘FILE’
void write_hello(FILE* fp) {
FILE is defined in stdio.h and you need to include it in each file that uses it. So write_hello.h and write_hello.c should both include it, and write_hello.c should also include write_hello.h (since it implements the function defined in write_hello.h).
Also note that it is standard practice for every header file is to define a macro of the same name (IN CAPS), and enclose the entire header between #ifndef, #endif. In C, this prevents a header from getting #included twice. This is known as the "internal include guard" (with thanks to Story Teller for pointing that out). All system headers such as stdio.h include an internal include guard. All user defined headers should also include an internal include guard as shown in the example below.
write_hello.h
#ifndef WRITE_HELLO_H
#define WRITE_HELLO_H
#include <stdio.h>
void write_hello(FILE*);
#endif
write_hello.c
#include <stdio.h>
#include "write_hello.h"
void write_hello(FILE* fp){
fprintf(fp,"hello");
printf("Done\n");
}
Note that when you include system files, the header name is placed within <>'s. This helps the compiler identify that these headers are system headers which are stored in a central place based on your development environment.
Your own custom headers are placed in quotes "" are typically found in your current directory, but may be placed elsewhere by including the path, or adding the directory to your list of directories that the compiler searches. This is configurable within your project if you are using an IDE like NetBeans, or using the -I compiler option is building it directly or through a makefile.

I have 2 issues with includes

I am programming in CCS (based on Eclipse) to learn to use microcontrollers.
I'm having some problems with includes.
I have 4 files:
GPIO.h - macros and prototypes of GPIO functions
GPIO.c - implementation of GPIO functions declared in GPIO.h
main.c - main program
util.h - macros and typedefs essential to all other files
In each of the programs put the includes, I ctrl + c / ctrl + v of my code:
I really try with " ", I would like to make my code run, it would be rewarding.
GPIO.h - #include "util.h"
GPIO.c - #include "GPIO.h"
main.c - #include "GPIO.c"
util.h - (no includes)
As in eclipse all files are placed in the project folder. Already checked manually by accessing the folder, and they are there.
When I compile and run, there are 2 errors referring to include:
"../GPIO.c", Line 9: fatal error # 1965: Can not open source file "GPIO.h"
"../main.c", Line 1: fatal error # 1965: Can not open source file "GPIO.c"
I do not understand what's wrong!
I made the edit so that people understand that even with "" the error continues (# mame98). I made it clear that I am using the CCS IDE based on Eclipse and now my suspicion is with the operating system. I will have the opportunity to test on Windows only now.
You should only include H files as Eugene Sh. Points out... Also, use #include "util.h" and #include "gpio.h" as they are local files and they are not in the default search path of your compiler. If you want to include 'global' headers (which are in the search path) you have to use #include <file.h>.
Maybe also note, that it is possible to add your local folder to the search path with using the -I. option for GCC (should work with other compilers too).
For more infos about the search path, see here.
<> is for libraries like #include <stdio.h>
"" is used for your own files #include "GPIO.h"
Be careful including .c! If GPIO.h is included in GPIO.c, too, you could get errors..(multiple inclusion protection is useful here!)

variable "using" is a not a type name?

I am currently trying to create an application in C as an assignment for school, and my professor requires me to use the following to make it compatible with his compiler:
#pragma warning(disable: 4996)
#include<string>
#include<stdlib.h>
#include<time.h>
(I am using visual studio)
And for some reason #include <string> throws up a bunch of run time errors mainly consisting of the one in the title of the question. It pops up within a bunch of other dependencies ranging from cmath, cstudio, cstdlib, cstring, etc. So the bottom line is, can anyone tell me how to fix it? Thanks!
In visual studio you can get this error if you extension is '.c' VS studio will assume the file is type c and not c++ and use the c compiler and not the c++.
In the project settings under advanced options you can select compile as C++ and then files will all be compiled as C++. If you scroll down you will find the flag '-x' on the command line as added to it.
If you are using the gcc compiler this is the same as adding the following to the compiler command '-x c++'
Check the gcc reference for this flag.
The #include <string> is a C++ header file and therefore incompatible with C.
As others have suggested, you can change this to #include <string.h> so that you have a valid C header, however if your professor dictated that you use that specific set of headers, then you need to change your source file to a .ccp file instead of a .c file.
You're including C++ header file. Use #include <string.h>
You are including a C++ library in C code. The header files without any extension are C++ header files. Having ".h" extension are C header files. C++ also accepts C header files still they have no extension. They have given same names having "c" prefix and no ".h" extension.
for example
<string>
is a C++ header
<string.h>
is a C header
<cstring>
is a C header but in a C++ code.

Eclipse project cannot include tchar.h

I am trying to build an lzmat_lib compression library using Eclipse with Cygwin gcc. I downloaded the library from the link http://www.matcode.com/lzmat_lib.zip. The file has the following include files:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <tchar.h>
#include "lzmat.h"
It cannot find the tchar.h header file. I do not understand how to add this header file. Please suggest a solution.
Your options are to install a Windows development environment, like Visual Studio or mingw along with the Windows SDK, or to port the code to your cygwin (posix) environment.
To port the code, you'd just do this:
Remove #include <tchar.h>.
Search and replace _TCHAR to char.
Search the file for all strings beginning with _t and remove that prefix. E.g., _tfopen becomes just fopen. _tprintf becomes printf.
Search for the text _T and remove it. You could also remove the extra parentheses that will then surround your string.
Deal with any other issues as they come up by removing the dependency on tchar.h and using a standard function instead.

stdio inclusion in a header file

I'm not totally sure but this looks wrong:
I have a header file named fraction.h in which I store a fraction structure and the methods to handle it, one method is used to write a fraction in a file and in the signature of this function one argument is a FILE pointer.
fraction.h:
...
const Fraction * fraction_fwrite(const Fraction * f, FILE * file);
...
fraction.c:
#include <stdio.h>
#include "fraction.h"
...
Now when I try to compile a program that uses a fraction, I get an error,
here is what I have in my Makefile:
program_test: fraction.o program_test.o
and I include fraction.h in program_test.c of course.
but I keep getting this error :
fraction.h:34:54: error: unknown type name 'FILE'
someone could explain the different steps through which the compiler includes files ?
because <stdio.h> is in fraction.c so why does it strike this unfound-type error ?
should I include <stdio.h> in fraction.h ? which looks not really appropriate from my measly experience.
When you compile program_test, the compiler isn't looking at the other .c files, only the files you #include in the file you actually compile.
So, you either have to #include <stdio.h> in the test program, just like in fraction.c, or include it in the header file.
Even though the C standard says that the standard library files will not include each other, there is nothing saying that user defined files cannot do that. In fact it is usually much easier to use them if they do.
When the preprocessor processes a file, then it will copy the content of an included file into the file that is preprocessed. The reason why you don't get that error in fraction.c is that the content of stdio.h is included before fraction.h is included.
So the preprocessed file fragments.c looks like this:
stdio.h contents
fragment.h contents
fragment.c contents
Notice that the FILE definition will be included through stdio.h before it is referenced in fragment.h.
You should include stdio.h in fraction.h to get rid of this error.
Include stdio.h in program_test.c. If you dont include stdio.h in program_test.c (which includes fraction.h), the compiler doesn't know the definition of FILE used by fraction.h, and generates error.
And, you really should include stdio.h in fraction.h

Resources