1

I tried to add my USART library to my project but I am still failing to properly add it so it will be recognized.

I created an USART.c and USART.h file, which I want to add. This is what I tried:

1) Right Click on the Solution / Properties / Toolchain / Directories 2) Adding the Path where I got these two files

When I try to build the project, it did not work. I get the message undefined reference to 'initUSART'.

How do I add my own libraries to projects then?

enter image description here enter image description here

sesc360
  • 3,155
  • 10
  • 44
  • 86

3 Answers3

2

The screenshot in your question shows that you arranged for the compiler to find the header files for your library. But you also need to use the compiler to compile your library functions (e.g. initUSART) and create a static library file (with a lib prefix and a .a extension). You would need a separate Atmel Studio project for that, or learn how to use the AVR GCC toolchain outside of the IDE to compile libraries. Then you need to put that file in a directory that is in the linker's search path for libraries, and then you need to pass the appropriate -l argument to the linker. For example, if your library is called libuart.a, you need to pass -luart to the linker. The Project Properties for an Atmel Studio project has the relevant settings you need to configure.

GCC has a standard way to compile, create, and link to static libraries, which I outlined above. You can learn about that from any tutorial on GCC static libraries. You then would need to apply that knowledge to the AVR GCC toolchain, and find the appopriate options inside Atmel Studio that you need to set.

Aside: Atmel Studio does not make it easy to use libraries at all. The Arduino IDE does a much better job because you just put the source files for the library in the right place and it compiles them for you. There are a huge number of Arduino libraries too; you wouldn't have to write your own UART driver if you could use the Arduino platform.

The simple alternative: If you don't know much about compiling and linking to C libraries and configuring your IDE, you would have a much easier time just copying the library files into your project, adding them as source files, and letting Atmel Studio compile them just like any other source file in your project.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
2

Another simple way of adding folders to your project is to copy/paste the folder into your project and then open Atmel Studio.

On the right side (where is by default Solution Explorer) you'll see all your files except the ones that you just added. Now press the Show all files and search for you folder which should appear grayed out. Right click on it and Include in project. That should be all!

This image should help

0

I got another solution that might help . i found Include Directories in this path for MegaAvr(8bit) :

C:\Program Files (x86)\Atmel\Studio\7.0\packs\atmel\ATmega_DFP\1.6.364\include

Just Puts All your Library in one Folder And Copy All of It in this path , then include it like another library . For example I created a folder named "ali" in that path , then i copied all my libraries in this folder (like alcd.h , usart.h) and then included in my programes with this :

#include <ali/usart.h>

and done ! just remember to backup your folder before Windows Installation (Drive Format) . Also you can find your libraries (.h and .c) in Solution Explorer -> Dependencies after Code Compilation . GoodLuck ...

inside Folder

including xio.h in folder ali

xio.h in dependencies after compilation

my folder in specified path

ALI
  • 29
  • 3