I hope this is the right part of the forum for this. I want to create a C library for use in my programs using atmel studio 7 I first chose static C library at the intro screen. I wrote a very simple library containing one simple function. Apr 22, 2015 Often it's better to add an include directory where the compiler looks for include files (specifically those included with marks). This can be done in Atmel Studio 6: Project properties - Toolchain - ARM/GNU C Compiler. Directories - Add Item (button with green plus sign).will be AVR/GNU C Compiler if using an AVR board.
I know there are a lot of questions on this topic, and I've looked through a fair number of them. However I am still having problems.
I started writing a test program for a prototype PCB, and now that it's grown to nearly 1000 lines I'm trying to break it up into libraries that I can use for particular functions.
I thought this would be very simple. Make .c and .h files for each library that I need. I.e. I would have OLED.h and OLED.c for functions that control an OLED display. Copy the appropriate functions/definitions into each file. Then copy these files into the solution in Atmel Studio. I put them into the src folder under the project name.
However, this doesn't work! I get an exceedingly long list of errors. All of the things that are defined in the .h file are apparently undefined as far as the compiler is concerned. I also get many error messages of the type 'unknown type name int16_t/uint16_t/uint8_t/etc...' That part is really baffling to me. Why should it matter that functions are in an external library, now the compiler doesn't understand what those data types mean?
So, this is probably a stupid problem to have. I don't want Atmel Studio to control my libraries by wrapping them up in some 'library project' or somethig, I want to put them in a folder of my choosing and add them when I need them. I've searched for answers to this problem and I find long tutorials about changing the compiler settings for the project, the linker settings, etc... I tried this tutorial and still no dice: http://www.engblaze.com/tutorial-using-avr-studio-5-with-arduino-projects/#setup
I also can't find a way to add something by right clicking the project and clicking 'Add.' It wants me to find .a files. The 'Add Library' dialog box in Atmel Studio is awful, it seems.
Surely it can't be that convoluted to just add a library to an existing project and have it function normally?! I've used PICs in the past and coming to Atmel I've found horrible documentation and a weird super-slick super-fly whizz bang interface that can't leave well enough alone and obfuscates simple function. What can I do to add these libraries?
UPDATE: Seemed to answer my own question. Turns out I needed to include all of the libraries to recognize data types and whatnot into the .c file. I somehow assumed this only had to be done in the main file but obviously I was mistaken. Adding asf.h seems to work well as it includes all of the MCU specific port definitions/names and all of that. All good for now!
Adding library files to a solution should be simple. Go to the Solution Explorer, right-click on your solution, and go to 'Add->Existing Item'. If you want to add a pre-existing library and keep it in a separate folder from your solution, click the arrow next to 'Add' and choose 'Add as link'. That saves many headaches due to having a duplicate copy of your library in your solution folder, and files not staying up-to-date.
You are right in saying that you need to include the necessary header files in the .c
files where they are used.
The compiler compiles each C file separately, and then links them together at the end, so you got the error unknown typename int_*
because the compiler had not seen the relevant header in the context of compiling that C file.
You also seem to be in some confusion as to the difference between definition and declaration.
A function is:
int some_func(char some_var);
which tells the compiler that the function exists, but does not tell it what it is. This is necessary because the compiler only looks at one C file at a time, so needs to be told that other functions exist.int some_func(char some_var) { do_stuff(some_var); }
. After compilation of each individual C file in isolation, the linker is called to put all the pieces together and give you your final binary, which you flash to the device.A function can be (and must be) defined only once, but may be declared many times - even in the same file, so long as the declarations are not conflicting.
Atmel stated in their website:
Atmel Studio 7 features seamless one-click import of projects created in the Arduino development environment. Your sketch, including any libraries it references, will be imported into Studio 7 as a C++ project. Once imported, you can leverage the full capabilities of Studio 7 to fine-tune and debug your design. Atmel Studio 7 fully supports the powerful embedded debugger on the Arduino Zero board. For other Arduino boards, shield-adapters that expose debug connectors are available, or switch to one of the many available Xplained-Mini/PRO boards to fully leverage the Atmel HW eco-system. Regardless of what you choose, you will surely make something amazing.
I wonder how it works? Is it just a plug-in (visual-micro) meaning that we still have to install Arduino software? Or do they have their own compiler and debugger?
sepp2kTo complete Danny_ds answer :
The 'standard' Atmel compiler for Arduino (and 8-bit AVR) is nowaday avr-gcc
, GCC standing for GNU Compiler Collection (so, a free software tool).
It is the toolchain used by Arduino IDE, as well as Atmel Studio. Note that Atmel Studio is configurable, it can use another toolchains/compilers (someone told me that it exist at least 8 AVR compilers).
To understand how importing an Arduino sketche in Atmel Studio is possible, better to understand what an arduino is :
#include <Arduino.h>
formerly Program.h IIRC)Let's pop the stack :
First, you can wipe out the Arduino IDE by using your own editor and makefiles. See Arduino Makefile on github for easy switching to this.Doing this, you may have to add the Arduino.h inclusion in your sketche. But you have full control of the source tree processing. That was my motivation, when quit Arduino IDE early, because by that time it was impossible to use 2 libraries in the same sketche, what Arduino-Makefile allowed.
Secondly, if you don't plan to use the Serial
class (driving the UART/USB interface for console text communication with the Duino), it comes a temptation to remove the dependencies... I made the try, and i come to the conclusion that rewriting functions like setMode()
, digitalRead()
and write, etc... is just THE obvious : just open the PDF datasheet side by side with your code and set the bits accordingly.
ADC convertion, timer/counter management, eeprom read/write and even UART connexion driving, are all more tricky, as they imply to drive AVR I/O registers directly, and understanding the subsystem you're interacting with... But not impossible !!
Further, it is more than likely that (free) libraries are available, other than Arduino, to drive those jobs.
After this step, your source tree can be imported AS IT IS in Studio, and (assuming your compiler is still set on GCC, and Atmel Studio knows about your dependencies), it will compile seamlessly.
So Atmel Studio has just to import the Arduino Library in the project (and maybe adding some header inclusion as we have to do by hand) to compile it as a native project.
NOTE that inserting some existing files and particularily whole existing directories is a pain-in-the-ass with Studio.
Arduino code is just plain C++ code (including some C++ libraries).
The difference is that in the Arduino IDE, you don't see all the code. For example, main()
is hidden and compiled behind the scenes.
In an Arduino sketch, only setup()
and loop()
are visible, but those are called from the hidden main()
(which calls loop()
repeatedly).
So, it should be no problem for the Atmel C++ compiler to compile a sketch created in the Arduino IDE since it's a full C++ project already.
Danny_dsDanny_ds