Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] MikroC Compiler #include question

Status
Not open for further replies.

WStevens_sa

Member level 2
Joined
Jan 5, 2011
Messages
47
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
South Africa
Activity points
1,695
Hi guys. I know this is a stupid question.

I know this can be done in other languages. But I am not sure how to do it in C.

I would like to know if I can do the following. I would like to take all my functions from my test.c file and then put them into a function.c file. But they must still be able to be called from test.c. Basically to keep my code neat and readable.

I have tried adding a file to the project called function.c and then adding my functions to this file. I have then referenced the file using #include "function.c". But this does not work on compile as it cannot find the functions.

Is this possible in Mikro C Pro
 

It's a problem with the scope of C functions and variables. Basically, each of the files you include, and the one containing the 'main()' function are compiled to separate object files then linked together to make the single hex file you need to program the IC. Because at the time the compiling takes place the addresses of the other modules and the functions in them hasn't yet been decided (the linker does that) it is impossible to put the addresses in the call instructions in the hex file. Instead, the compiler puts a temporary fake address is put in place which the linker then changes when it knows what the real one should be.

The reason why you can't get it to compile is that without some knowledge of what the function or variable is, the compiler can't tell if a single byte or two byte address or page selection is needed and whether it should reserve memory for passing values to and from the function. The fix is simple, you tell the compiler the function exists but is external to the module you are currently in. It's a little more complicated than can be explained here but if you look in the help file for the key word 'extern' it should explain how you tell the compiler to deal with functions and variables it can't find. It is exactly what is done when for example you use a built-in function from MikroC's libraries which you can use within your own code.

Brian.
 
Hi betwixt.

Thank you for helping me figure it out. The code below might help someone else with the same problem.

How to use multiple .c files in your mikro c project. Lets say you have 2 files for argument sake test1.c and test2.c in your project. Test1.c contains your void main();. Test2.c contains your function. Now to call the function in Test2.c from test1.c which returns a value the following must be in each file.

test1.c

extern int calc(int a, int b);

int val;

void main()
{
val = calc(2,3); // val will be set to the value returned from the function.
}


test2.c

int calc(int d, int e)
{
return (d + e) + 100;
}

Function call without returning a value

test1.c

extern void calc(int a, int b); //declare function

int val;

int myvalue = 25;

void main() {

calc(2,3); // will not return any values

}

test2.c

extern int myvalue;

int x;

void calc(int a, int b){

x = (a + b) + myvalue;

}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top