View previous topic :: View next topic |
Author |
Message |
Guest
|
undefined identifier |
Posted: Tue Jun 10, 2008 2:40 am |
|
|
I have noticed that if I call a function placed after main in the edit window, I get an 'undefined identifier' error at compile time.
If I move the function to before main, it compiles ok.
This seems to be something that has just started happening.
I am also trying to call functions in other .h or .c files but this is not working reliably.
I wonder if I have changed the compiler configuration in some way.
Version 4.071
pic18f8722 |
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 10, 2008 3:32 am |
|
|
This is totally normal, and expected.
Functions _must_ (and always have had to be) defined, _before_they are used. You can use a 'prototype' definition, so that you can place the actual code latter. So:
Code: |
int8 function(int8 val);
void main(void) {
int8 rval;
rval=function();
}
int8 function(int8 val) {
//Actual function code
}
|
This is normal C.....
Best Wishes |
|
|
Guest
|
|
Posted: Tue Jun 10, 2008 3:47 am |
|
|
Thanks for the reply.
I know about prototypes, I have them in my code.
If I start a new project then I don't have a problem. With this project I am adding routines to main is now causing a problem.
It is if something has changed in the compiler/project.
I will start a new project and gradually add the files and see what happens.
I started a similar thread a little while trying to add files .h and .c to my project and the problem may be related as I have not resolved this.
Regards, |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Jun 10, 2008 4:01 am |
|
|
I have had to do the following:-
I create .h and .c files, prototypes go in .h, code in .c
Each .c file (except the main one) includes its own .h and any other .h it requires.
Each .h has #ifdef to see if it has already been included.
Now this won't actually work until you do the next bit.
In your main file, include the .h for itself but you must include the .c files of any of the others it needs and not their .h files!
This is a pain, but basically this compilers doesn't seem to have a linker. So ALL the code has to be in one main file and the #include (.c) does this for you.
Enjoy |
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 10, 2008 9:30 am |
|
|
The compiler has a linker.
It does now work.
You need to flag all functions in the linkable modules, that you want to use, using #EXPORT. In the main, you need to #IMPORT all the modules you want to use.
This is very much the same as in every other linker...
Best Wishes |
|
|
|