[solved] how to add own library in atmelstudio6.2

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

hi all,

 

  Good day. I am using atmelatudio6.2. In that i added my own library. UART_routines.c(source file) and UART_routines.h(header file). 

In code i used this library #include "UART_routines.h". getting error message  UART_routines.h: No such file or directory 

 

but no error in avr studio 4 and working fine.

 

Thanks & Regards

Sasi

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

Last Edited: Fri. Oct 10, 2014 - 09:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

A traditional C compiler will translate C and S files into relocatable O files

It then links with pre-built O files that are stored in an A library.   e.g. libc.a

 

The C compiler generally only links with libc.a

 

If you want to link with other pre-built libraries,  you use the -l switch.   e.g. -lm to link with libm.a

 

Pre-built libraries mean that you have to build a different library for every memory model and avr core.

Look on your disk.   There is more than one libc.a

 

If you want to link with any other libraries,   you have to add their names and location to the AS6 Project Properties.

 

You can pre-build your UART_routines.o or even make an archive UART_archive.a

 

In practice,   your PC is so quick that you might just as well add your "UART_routines.c" to your project files.

And re-compile every time.

 

The Arduino  (and Codevision C) works differently.

If your C file has included a H file,   it makes a note of each H file.

 

Then builds or links with this list of "libraries"

Unless you tell Arduino or CV differently,  it will only look in the system library path for <library.h> libraries.

Arduino also looks in the user libraries path for user added libraries.    i.e. ../libraries

 

David.

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

hi david,

 

    Good day. Yes i added path of that library, but still i have the same error :"Error    1    UART_routines.h: No such file or directory"

 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

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

State these four things:

 

  • The path, in the file system, to UART_routines.c
  • The path, in the file system, to UART_routines.h
  • The path, in the file system, to the project file.
  • The name and path to the source file where you do the
#include "UART_routines.h"

 

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"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] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

hi Johan,

 

UART_routines.c  -  C:\Documents and Settings\Dev5\Desktop\SASI\AtmelStudio_6.2\serial1

 

 UART_routines.h  -  C:\Documents and Settings\Dev5\Desktop\SASI\AtmelStudio_6.2\serial1

 

 serial1.atsln  -  C:\Documents and Settings\Dev5\Desktop\SASI\AtmelStudio_6.2\serial1

 

 serial1.c   -  C:\Documents and Settings\Dev5\Desktop\SASI\AtmelStudio_6.2\serial1\serial1\serial1.c

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

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

In which case UART_routines.h is at ..\..\ from where serial1.c is. The simple solution then is to put:

#include "..\..\UART_routines.h"

in serial1.c and it will be found.

 

But sometimes it is "cumbersome" to have lots of "..\..\.." in the #includes, especially when it's something like:

#include "..\..\..\..\mculib\comms\serial\uart1.h"

or similar. So the other solution is to give the compiler a hint about where to look for .h files in "" marks. You do that with the -I compiler command. In your case you could use:

-I ..\..

or you could use:

-I C:\Documents and Settings\Dev5\Desktop\SASI\AtmelStudio_6.2\serial1

and then in the serial1.c you could simply use:

#include "UART_routines.h"

The compiler effectively "glues" the string of each -I onto the name of the given file when it tries to find it. The way you tell Studio 6 what -I's to use is here:

 

Last Edited: Fri. Sep 12, 2014 - 09:24 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi clawson,

 

 #include "C:\Documents and Settings\Dev5\Desktop\SASI\AtmelStudio_6.2\serial1\UART_routines.h"

or by adding path in Toolchain->directories and then i used #include <UART_routines.h>

 

if i added like any one of the above command, the errors are

Error    1    cannot find -lUART_routines.c        1    1    serial1

Error    2    cannot find -lUART_routines.h        1    1    serial1

Error    3    ld returned 1 exit status    collect2.exe    0    0    serial1

 

 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

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

The simplest solution is to copy all files into your Project directory.

This is the default when you use [Add Files] in AS6.2

 

You don't need to add extra paths,   library names, ... anything.

AS6.2 simply compiles main.c uart_routines.c and builds your project.hex

 

You end up with several Project directories e.g. each new project has a fresh copy of uart_routines.c and uart_routines.h

If you ever change uart_routines.c,   you would have to duplicate the changes over every copy by hand.

 

An improvement is to [Add a link] rather than [Add a copy] when you add a debugged file to your project.

It will still build ok.    If you ever change uart_routines.c all the other projects that use this file will see the changes.    And need to re-build themselves.

 

When you start getting projects with tens or hundreds of C files,   you need to use a more sophisticated approach.

 

David.

Last Edited: Fri. Sep 12, 2014 - 10:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi david,

 

 As per your point i added UART_routines.c (c file) and UART_routines.h(header file) in project directory file-new-c file(for .c) , file-new-header file(for .h). below one is my code serial1.c. And now the errors are:

Error    2    ld returned 1 exit status    collect2.exe    0    0    serial1

Error    1    undefined reference to `transmitString'   

//UART_routines.c

//**************************************************************
//******** FUNCTIONS FOR SERIAL COMMUNICATION USING UART *******
//**************************************************************
//Controller:	ATmega128 (Crystal: 14.7456Mhz)
//Compiler:		AtmelStudio 6.2
//********************************************************

#include <avr/io.h>
#include <avr/pgmspace.h>
#include "UART_routines.h"


//**************************************************
//Function initialize UART1
//desired baud rate: 9600
//**************************************************
void uart1_init(void)
{
    UCSR1A=0x00;
    UCSR1B=0x18;
    UCSR1C=0x06;
    UBRR1H=0x00;
    UBRR1L=0x5F;
}
//**************************************************
//Function to receive a single byte
//*************************************************
unsigned char receiveByte( void )
{
	unsigned char data, status;

	while(!(UCSR1A & (1<<RXC1))); 	// Wait for incomming data

	status = UCSR1A;
	data = UDR1;

	return(data);
}

//***************************************************
//Function to transmit a single byte
//***************************************************
void transmitByte( unsigned char data )
{
	while ( !(UCSR1A & (1<<UDRE1)) )
		; 			                /* Wait for empty transmit buffer */
	UDR1 = data; 			        /* Start transmition */
}

//***************************************************
//Function to transmit a string in Flash
//***************************************************
void transmitString_F(char* string)
{
  while (pgm_read_byte(&(*string)))
   transmitByte(pgm_read_byte(&(*string++)));
}

//***************************************************
//Function to transmit a string in RAM
//***************************************************
void transmitString(char* string)
{
    while (*string)
		transmitByte(*string++);
}
//UART_routines.h

// **************************************************************
// ***  UART_ROUTINES HEADER FILE ********
// **************************************************************

#define TX_NEWLINE		{transmitByte(0x0d); transmitByte(0x0a);}

void uart1_init(void);
unsigned char receiveByte(void);
void transmitByte(unsigned char);
void transmitString_F(char*);
void transmitString(char*);

 

//serial1.c

#define F_CPU 14745600UL

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h> 
//#include "UART_routines.h"
#include "C:\Documents and Settings\Dev5\Desktop\SASI\AtmelStudio_6.2\serial1\UART_routines.h"

int main(void)
{
    DDRF = 0x7E;
	
	while(1)
    {
        //TODO:: Please write your application code 
		
		transmitString("Start Atmel Studio 6.2 Serial comm");
		
		PORTF = 0x7E;
		_delay_ms(1000);
		
		PORTF = 0x00;
		_delay_ms(1000);
    }
}

 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

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

Look just zip up your project and post it here and someone can show you how to do this - it's not rocket science - really it isn't.

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

hi clawson,

 

  here by attach serial1 project zip file. In this i am getting errors. 

Please help me to find out errors.

Attachment(s): 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

Last Edited: Fri. Sep 12, 2014 - 11:24 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Fixed. I did exactly what I told you to do above and it worked. That is to set "..\.." as a -I path and then just use:

#include "UART_routines.h"

in the code. The other thing I had to do to make it actually build was add UART_routines.c as a build target.

 

Update attached.

Attachment(s): 

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

Ok.   I right-clicked on "serial1" project.   [Add Existing item] to your project.

 

Browsed for Uart_routines.c and uart_routines.h.  Clicked on [Add]

Changed your header include to:

#include "UART_routines.h"

Then built your project.

Afterwards I looked at your Project Properties.

I removed the library paths.

 

Everything is in the same source directory as the "serial1.c"

 

I have attached the amended "serial1.zip"

 

Please study the way that I have done this.

Once you understand it,   we can show you how to use a single "usart_routines.c" with several different projects.

 

David.

Attachment(s): 

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

hi david and clawson,

 

  Thank you so much both of you and thanks to johan. Now i got the way and i am very clear now. Thanks for your great help. now it works fine. Thanks to all.

 

 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

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

hi david,

 

  how to use a single "usart_routines.c" with several different projects.

 

david.prentice wrote:

Ok.   I right-clicked on "serial1" project.   [Add Existing item] to your project.

 

Browsed for Uart_routines.c and uart_routines.h.  Clicked on [Add]

Changed your header include to:

#include "UART_routines.h"

Then built your project.

Afterwards I looked at your Project Properties.

I removed the library paths.

 

Everything is in the same source directory as the "serial1.c"

 

I have attached the amended "serial1.zip"

 

Please study the way that I have done this.

Once you understand it,   we can show you how to use a single "usart_routines.c" with several different projects.

 

David.

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

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

how to use a single "usart_routines.c" with several different projects.

the "trick" is that when you "add existing" to add the UART_routines.c to your new project you will note that the [Add] button has a drop arrow to the right. Select that and you will see there is a second add option that is "Add as link". This makes the .c file visible to your project but does NOT copy it from the original location. So you keep just one master copy on your drive and all your projects "link" to it. This is much better than having the same file copied to each project because if you later find a fault in one of your UART routines you don't have to go round fixing it in 10 different places. You just fix the master copy that the projects link to and they all "see" the fix at the same time.

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

Just to add to Cliff's advice:

 

1.   [Add as link] any common source files when you [Add existing item]  to your project.

2.   add the Include search path for any user H files.    I think that AS6 will let you Browse to find the directories.

 

That's it.

You can pre-compile all the source files and put them in a real library.a

If you do this,   you may need different archives for different Tinies, Megas etc.

and you need to:

 

3.  add the librarysearch path to your libXYZ.a directory.

 

David.

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

I think that AS6 will let you Browse to find the directories.

It does. Always take the option to create it as a relative path then the project still works if you move it to another location.

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

Hi David & Clawson,

 

  Good day. As per your guide i followed your points. and now i got a diamond way. and  i  used this  single "usart_routines.c" with several different projects. Thanks  both of you great help. Today i learned it better. Thanks alot.

 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

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

Hi Sasi,

 

Steps to be followed to use a library in Atmel Studio,

 

1. Create a project in Atmel Studio.

2. Go to Project Properties.

3. Select the option "Toolchain -> AVR/GNU Linker -> Libraries", Add the library under "Libraries" section and its directory in "Library search path"

4. Select the option "AVR/GNU C Compiler -> Directories", Add the directory of header files of the library in "Directories" section

5. Add the line #include "UART_routines.h" in the source file where you would like to call the library function

 

Please refer the attachment which has all the above steps followed in it. Build and see if it works.

 

-Deena

 

P.S: For the sake of creating a library called libroutine.a, I added a new project to the solution. It has no significance in resolving this issue, you can just add your own library following the above described steps.

Attachment(s): 

Regards,
Deena

Last Edited: Mon. Sep 15, 2014 - 07:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

hi deena,

 

   Thanks for your valuable points. I will follow your steps. Tomorrow i will do it.

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

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

hi deena,

 

    I tried as you  told. but i got an error. 

 

Error    1    cannot find -lroutine       

 

Error    2    ld returned 1 exit status  

 

in your attachment rountine there with debug folder. how you did it. have u created two solution/project

 

 

 

 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

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

Yes Sasi, I have created two projects in a single solution.

 

Open the attached solution and build the solution atleast once. After opening the solution, "Build -> Build Solution" menu.

 

The "debug" folder will be created as a result of the "Build Solution" action.

 

-Deena

Regards,
Deena

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

hi deena,

 

   i tried but not getting.  I also added project named routine and then renamed as UART_routines. then i copied function of UART_routines.c. How you added UART_routines.h

and libroutine in library.

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

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

Sorry for the inconvenience!!

Please try this attachment and build the solution everything shall work.

 

> Adding libroutine to serial1 project

Go to the properties page, select Toolchain tab, selection "Linker -> Libraries", click the "Add Item" button that is located on the right corner of the list of "Libraries (-Wl,-l)".

 

-Deena

Attachment(s): 

Regards,
Deena

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

To be honest, in these days of -ffunction-sections, -gc-sections and even -flto (oh and AS6 "Add as link") I don't see there's much point in avr-ar and .a files except at the system level. AVR projects just don't really warrant the two level development of libs then linking against those libs unless you are producing system libraries.

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

hi deena,

 

  Good day. Thanks for great help. I will try in this way you said.

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN