Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
smileymicros
PostPosted: Jun 15, 2008 - 12:21 AM
Raving lunatic


Joined: Nov 17, 2004
Posts: 6496
Location: Great Smokey Mountains.

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
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
dharper
PostPosted: Jun 15, 2008 - 12:44 AM
Posting Freak


Joined: Dec 30, 2002
Posts: 1358
Location: Fairview, Texas USA

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
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
Broxbourne
PostPosted: Jun 15, 2008 - 12:49 AM
Hangaround


Joined: Nov 16, 2005
Posts: 341
Location: Calgary, Alberta, Canada

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.
 
 View user's profile Send private message  
Reply with quote Back to top
smileymicros
PostPosted: Jun 15, 2008 - 05:09 AM
Raving lunatic


Joined: Nov 17, 2004
Posts: 6496
Location: Great Smokey Mountains.

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
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
skeeve
PostPosted: Jun 15, 2008 - 05:26 AM
Raving lunatic


Joined: Oct 29, 2006
Posts: 3210


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?

_________________
Michael Hennebry
"Religious obligations are absolute." -- Relg
 
 View user's profile Send private message  
Reply with quote Back to top
kevin123
PostPosted: Jun 15, 2008 - 05:59 AM
Hangaround


Joined: Jan 20, 2008
Posts: 210
Location: Canada

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.
 
 View user's profile Send private message  
Reply with quote Back to top
dharper
PostPosted: Jun 15, 2008 - 06:22 AM
Posting Freak


Joined: Dec 30, 2002
Posts: 1358
Location: Fairview, Texas USA

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
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
js
PostPosted: Jun 15, 2008 - 08:21 AM
10k+ Postman


Joined: Mar 28, 2001
Posts: 22638
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

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. Shocked

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. Smile

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
Code:
#include "project_definitions.h"
#include "dxio-7_definitions.h"
#include "Vmusic_icu_monitor.h"
#include "twi.h"

#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <avr/io.h>

#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/eeprom.h>

#include "usart0.h"
#include "usart1.h"
...If I can do it anyone can. Laughing

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

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
curtvm
PostPosted: Jun 15, 2008 - 10:14 AM
Raving lunatic


Joined: Sep 21, 2005
Posts: 2318


I never touch a makefile.

simple example I just tested-
Code:
//TestUSART.c
#include <avr/io.h>
#include "myUSART.h"
int main(void){
    tx_byte(0x55);
}

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

//myUSART.c
#include <avr/io.h>
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').
 
 View user's profile Send private message  
Reply with quote Back to top
ahmad.elsaeed
PostPosted: Jun 15, 2008 - 10:45 AM
Hangaround


Joined: Mar 15, 2007
Posts: 228
Location: Egypt

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
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 15, 2008 - 01:51 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 22054
Location: Lund, Sweden

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.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: Jun 15, 2008 - 04:09 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 71253
Location: (using avr-gcc in) Finchingfield, Essex, England

Joe,

It really should be as simple as adding all the .c files to the project tree (right click - "add existing source files...") but to find out why that's not working use the option on the bottom of the build menu to "export makefile" then post a copy of that here so we can see why it's not "seeing" your files.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
smileymicros
PostPosted: Jun 15, 2008 - 05:23 PM
Raving lunatic


Joined: Nov 17, 2004
Posts: 6496
Location: Great Smokey Mountains.

Thanks guys, the examples given work just fine. Mine doesn't, but at least I know it isn't AVRStudio or WinAVR. I'll report back when I figure out what I did wrong.

Smiley

_________________
FREE TUTORIAL: 'Quick Start Guide for Using the WinAVR C Compiler with ATMEL's AVR Butterfly' AVAILABLE AT: http://www.smileymicros.com
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
curtvm
PostPosted: Jun 15, 2008 - 05:37 PM
Raving lunatic


Joined: Sep 21, 2005
Posts: 2318


One more idea- in my above example, if I make the function in myUSART.c a 'static' function, you get- undefined reference to 'tx_byte'. So maybe the functions are static when they shouldn't be.
 
 View user's profile Send private message  
Reply with quote Back to top
smileymicros
PostPosted: Jun 15, 2008 - 07:15 PM
Raving lunatic


Joined: Nov 17, 2004
Posts: 6496
Location: Great Smokey Mountains.

I just came back to report figuring out what happened and curtvm beat me to the punch. I'd had a single module that I broke into two parts and I didn't remove the static from the function definitions.

Live and learn,
Smiley

_________________
FREE TUTORIAL: 'Quick Start Guide for Using the WinAVR C Compiler with ATMEL's AVR Butterfly' AVAILABLE AT: http://www.smileymicros.com
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
skeeve
PostPosted: Jun 15, 2008 - 07:22 PM
Raving lunatic


Joined: Oct 29, 2006
Posts: 3210


What is the output of
Code:
avr-nm --print-file-name  *.o | grep missing_symbol

_________________
Michael Hennebry
"Religious obligations are absolute." -- Relg
 
 View user's profile Send private message  
Reply with quote Back to top
skeeve
PostPosted: Jun 15, 2008 - 07:43 PM
Raving lunatic


Joined: Oct 29, 2006
Posts: 3210


smileymicros wrote:
I just came back to report figuring out what happened and curtvm beat me to the punch. I'd had a single module that I broke into two parts and I didn't remove the static from the function definitions.
I think that if you had included the .h file in the corresponding .c file,
you would have received an error message.
It's a good way to make sure that your .h file doesn't lie.

_________________
Michael Hennebry
"Religious obligations are absolute." -- Relg
 
 View user's profile Send private message  
Reply with quote Back to top
js
PostPosted: Jun 16, 2008 - 02:12 AM
10k+ Postman


Joined: Mar 28, 2001
Posts: 22638
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Quote:
You see make (and makefiles) as something exotic.
No I don't. I know that they are no more than glorified batch files and just as I never had to mess around with a batchfile with my beloved Atmel assembler (I just opened up a .bat file now to see what was inside it) I don't want to mess around with a gcc makefile and in fact I don't need to in a modern environment.

No makefile with CVAVR, Image Craft or IAR compilers, not that anyone needs to know about anyway, just click on a few project options and it all happens, pretty much the same way as winAvr is heading now. You can do it all from within Studio, the user/beginner does not need to know that anything he/she does in the config option is in fact modifiying a batchfile or makefile.

An unneccessary burden untill one knows what one is doing.

We are getting off topic again and Smiley ain't smiling.. Smile but as a "beginner" that's how I would like to see things, the perfect dummy to test his stuff.

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits