Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

how can i use "#include" in MicroC

Status
Not open for further replies.

Hesam Kamalan

New Member
hi,

in MicroC compiler, i have a challenge with "#include".

i want to add one file to my main programs. my files are: main.c, ds1307.c .
i added ds1307.c file in main.c file with include:

#include "ds1307.c"

but after that i click build, cpp.err appeared with this purporst:
Code:
e:/mikroelektronika/mikroc v8.0/usser/project1/main.c:2: error: Can't open include file "ds1307.c"
    #include "ds1307.c"
    from e:/mikroelektronika/mikroc v8.0/usser/project1/ds1307.c: 1:    #include "main.c"
    from e:/mikroelektronika/mikroc v8.0/usser/project1/main.c: 2:    #include "ds1307.c"
    from e.......

i add ds1307.c to C Files in Project Summary.

how can i solve this problem?
 
i added ds1307.c file in main.c file with include:
It looks like you also have a #include "main.c" in the file ds1307.c which is a big no-no because you have created a infinite recursive loop. Remove the #include "main.c" statement from the file ds1307.c
 
another question,
i have a lot of variables that common between two files. i want to Declare those as global variable. how and where i must declare these variables?
 
I'm not familiar with MicroC, so I'd left the answer to others.
In C, one way to use global variables is to declare them in a separate file called globals.c and then use the extern keyword in header files to give main.c etc access to them. A quick Google turned up this:
Learn C++ - » 4.2 — Global variables
 
you can declare all variables in main.c before you do any #include

so for eg
Code:
char g1,g2,g3,g4;
int g5,g6,g7,g8;

#include "something.c"
#include "somethingelse.c"
#include "else.c"

void main(){
  ...
}

that will work in some C compilers (CCS for example), as for mikroC i believe you have to use "extern" keyword
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top