How do you organize your code?

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

So here's what i want to do.

I want to have a main code file, and then in this same project i want to have all the other code i will include into my main code..

So for example..

I'll have a Mainproject.c, LCD.c,Keypad.c

I want to make header files for LCD.c and Keypad.c.. then include them into my Mainproject.c, to make my project..

I'm hving trouble doing this.. i get a few different errors when i try to do this.. (an objectfile not found error) i get these errors as soon as i add another source file to my project...

I don't want to make a HUGE ass file with my project.. i want to organize this well

is there a way to do what i'm trying to do?

how should i do this??

This is basically a problem with my makefiles..

thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Presumably you are talking about a GCC project within Studio (because of the post in this forum) rather than a project where you use your own Makefile?

If the former then to add multiple source files to the project it should be as simple as switching the left hand window to the "AVR GCC" tab then right click on the entry that says "source files" and select the "Add existing source file(s)..." option. I'm guessing the project currently consists of the single "Maiproject.c" source file so you need to add LCD.c and Keypad.c to the project.

Then to make use of a routine that's in LCD.c from within Mainproject.c you'd write an lcd.h file that includes a declaration of the function - that is the header of the function but terminated with ; rather than the function body between { }. At the top of Mainproject.c just use <#include "LCD.h"> so that this file can "know" about the parameters to/from the routine in LCD.c

To share global variables between files put their defintion as usual in the file where they are used - so in LCD.c you might have:

unsigned int LCD_cursor_pos;

then to access this variable in Mainproject.c add an almost identical declaration of the variable to LCD.h but preceeded by the word extern. So LCD.h will contain:

extern unsigned int LCD_cursor_pos;

Then when LCD.h is #include'd in Mainproject.c this will allow it to make reference to the LCD_cursor_pos variable even though it "lives" in another file.

When the project is built the three .C files are compiled separately. When Mainproject.c is built to form Mainproject.o the compiler can't know the address of functions/variables in LCD.c yet so it just puts in a marker to say "need to fix these things later".

Finally, once all the .c files have been compiled to .o files, the linker is invoked to "glue them together" into the single, output, .elf file. It's at this moment that the linker will link up the accesses that Mainproject.c makes to functions/variables in LCD.c

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks a million man... i think this will do it.. I was fiddling with the program.. and i just kept getting these errors...

i still need to try the extern thing.. but thanks a lot...