Using multiple .c files in AVRStudio with AVR-GCC

18 posts / 0 new
Last post
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay, what's the trick to using multiple C files in AVRStudio with AVRGCC? I'm pretty sure the linker options aren't set up correctly, but I can't find any documentation on the Linker Options.

Let's say I write myUSART.c, myUSART.h, and TestUSART.c. I include myUSART.h in TestUSART.c. All these files are in the AVRGCC window. When I compile I get errors indicating that the calls from TestUSART.c of functions in myUSART.c are 'undefined reference'.

What am I missing?

Smiley

FREE TUTORIAL: 'Quick Start Guide for Using the WinAVR C Compiler with ATMEL's AVR Butterfly' AVAILABLE AT: http://www.smileymicros.com

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

I just checked this to make sure it still worked after installing the latest Studio & WinAVR - an old project that has four C source files and three headers. What are you using for your Makefile? The internal Studio version or an external version? If internal, try creating your own Makefile using the Mfile utility that comes with WinAVR. It allows you to easily specify all of the source files. Then simply go into the Project->Configuration Options menu and check the Use External Makefile option in the General section (you will need to select the Makefile using the ellipse button).

Dave

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

Dumb question but...
did you right click on source files in the project tree and add the files? That is all I have ever done to add files and build.

Edit: re-reading, sounds like you already did that, so dumb question indeed.

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

The source files are there and they compile to object files.

The problem with using mfile and/or writing my on makefile is that I am writing some stuff for novices and I was hoping that AVRStudio would have a simple way to set the linker options for multiple files without having to do anything to taxing. I can use makefile, but it is an extra burden that I don't want to put on the reader if at all possible.

It will be simpler for the reader if I just put everything in a single module and instead of using multiple C files or libraries just have the user cut and paste the functions needed. Easy, but lame if there is a simple way to use multiple files in AVRStudio.

Smiley

FREE TUTORIAL: 'Quick Start Guide for Using the WinAVR C Compiler with ATMEL's AVR Butterfly' AVAILABLE AT: http://www.smileymicros.com

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

smileymicros wrote:
Okay, what's the trick to using multiple C files in AVRStudio with AVRGCC? I'm pretty sure the linker options aren't set up correctly, but I can't find any documentation on the Linker Options.
The problem isn't a linker option.
It might not be getting both .o files.
You might be mispelling something.
IIRC there are gcc options that will tell you whether a global
function is defined without a preceding declaration.
The definition would normally be in a .c file,
the preceding declaration in a .h file.
Quote:
Let's say I write myUSART.c, myUSART.h, and TestUSART.c. I include myUSART.h in TestUSART.c. All these files are in the AVRGCC window. When I compile I get errors indicating that the calls from TestUSART.c of functions in myUSART.c are 'undefined reference'.
Is myUSART.h included in myUSART.c ?
When you click on "Build all", what commands does AVR Studio show you?

Is it racist to discriminate against someone who changes species?

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

Using multiple C files in AVR Studio is pretty straight forward. You can just use the default settings and add all the C files to the project. You don't need to mess with the makefile at all.

Attached is a quick project I just created with just two .c files. The main function is in test1.c and calls a function in test2.c. It compiles without any errors for me.

Attachment(s): 

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

smileymicros wrote:
The problem with using mfile and/or writing my on makefile is that I am writing some stuff for novices and I was hoping that AVRStudio would have a simple way to set the linker options for multiple files without having to do anything to taxing.

Fair enough - for the final solution. For debugging though, how about giving Mfile a try and see if you can get things to compile & link correctly this way? If you can, you can then export the internal Makefile from the Build->Export Makefile option. Then you can compare the two to see what the differences are. From your description of how you have things set up (ie: including myUSART.h in TestUSART.c) and the fact that everything seems to be correctly compiling into .o files, it really sounds to me like a linker problem.

Dave

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

The best thing about makefile for a beginner, or pretty much anyone else but the most advanced user as far as I can tell, is of NOT KNOWING of it's existence at all. Let alone messing around with it or using mfile.

Won't the beginner be shocked when he/she goes and starts using another brand of C compiler and looks for makefile. :shock:

It is certainly not necessary knowing about it for a multifile project, as modern winAvr/Studio seems to be doing a good job by simply setting up the processor type and clock frequency.

From above

Quote:
Using multiple C files in AVR Studio is pretty straight forward. You can just use the default settings and add all the C files to the project. You don't need to mess with the makefile at all.
Amen. :)

Just let the other program that needs to use functions from another file of the existence of those function by including the header file(s)

ie in main

#include "project_definitions.h"
#include "dxio-7_definitions.h"
#include "Vmusic_icu_monitor.h"
#include "twi.h"

#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include  

#include "usart0.h"
#include "usart1.h"

...If I can do it anyone can. :lol:

edit and just to be sure this is what the gcc window looks like for the above project. Makefile? What's that?

Attachment(s): 

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I never touch a makefile.

simple example I just tested-

//TestUSART.c
#include 
#include "myUSART.h"
int main(void){
    tx_byte(0x55);
}

//myUSART.h
void tx_byte(uint8_t byte);

//myUSART.c
#include 
void tx_byte(uint8_t byte){
    while(UCSR0A & (1<<UDRE0));
    UDR0 = byte;
}

which works fine.

I think there is some 'simple' mistake Smiley is making, as I don't think its an avr studio/gcc problem.

One problem that that the newest version of studio no longer has, is header file changes were not saved unless you saved them. When doing a build, if you forgot to save the h file, you would still be using the unchanged header file. The newest version of studio is saving the changes made to header files whenever a build is done. I think (but could be 'misrembering').

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

i faced this problem nowadays ... i solved this by one of 2 options

either you modify the SRC in the makefile and add your sources beside it.

or you may add your output object file in the beside the OBJ like:

OBJ = main.o USART.o .... etc

Regards,
A.El-Saeed

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

Quote:

Won't the beginner be shocked when he/she goes and starts using another brand of C compiler and looks for makefile.

Thats interesting. You see make (and makefiles) as something exotic. I see them as commonplace.

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington]

Pages