Choosing compatible header files for Arduino and Atmel Studio IDEs

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

I am trying to build a code base that works on both the Arduino IDE (and Visual Micro - an Arduino plugin for Visual Studio) and Atmel Studio. Some code will be specific to Arduino but most of the code is reusable in both environments.

 

I am having trouble with selecting the correct header files that will be recognized in both IDEs.

For instance when I wrote some code in Atmel Studio the 'NULL' is defined in stddef.h. But in the Arduino IDE that header file is not found (actually I tested that with Visual Micro).

 

So what is the best approach? Should I make my own header file that I use in all my code and have two versions of that (one for Arduino and one for Atmel Studio)?

Or are there other (better?) options?

 

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

 

 

In the C standard (section 7.17 paragraph 3), NULL is defined in stddef.h , which AVR-Libc does not implement. Since Arduino is based on AVR-Libc, there is no stddef.h in Arduino. That could be fixed, but don't expect it to happen anytime soon.

 

NULL IS defined elsewhere, I know, but that doesn't really help you. I would say your best approach would be to create your own 'backup' header files (#include last ), and use preprocessor magic to define things which are missing from one platform, or the other, or both. For example, 'missing_stuff.h", partially listed below...

 

#ifndef NULL
#define NULL ( (void *) 0)
#endif

That way, if in the future  stddef.h were implemented by Arduino (avr-libc), your file names and definitions would not conflict.

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

NULL is defined in stddef.h , which AVR-Libc does not implement.

 I thought that Atmel Studio was also pretty strictly avr-libc based.   Where did IT get stddef.h?

(Arduino 1.6.5 seems to allow #include <stddef.h>; it has it in "hardware/tools/avr/lib/gcc/avr/4.8.1/include/" on my Mac.)

 

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

NULL is defined in stddef.h , which AVR-Libc does not implement.

Eh?

~/windows/Program Files/arduino-1.6.3/hardware/tools/avr$ find . -name stddef.h -exec grep -w -H NULL {} ";"
./lib/gcc/avr/4.8.1/include/stddef.h:#undef NULL		/* in case <stdio.h> has defined it. */
./lib/gcc/avr/4.8.1/include/stddef.h:#define NULL __null
./lib/gcc/avr/4.8.1/include/stddef.h:#define NULL ((void *)0)
./lib/gcc/avr/4.8.1/include/stddef.h:#define NULL 0
./lib/gcc/avr/4.8.1/include/stddef.h:#endif	/* NULL not defined and <stddef.h> or need NULL.  */

Or was this a question of semantics? I guess a system .h under ./gcc/avr is really a "compiler header" rather than a "libc header" ?

 

As you can see both compiler and libc headers are part of the standard #include <> search paths:

~$ cat avr.c
#include <stddef.h>

int main(void) {
}
~$ avr-gcc -mmcu=atmega16 -H avr.c -o avr.elf
. /usr/lib/gcc/avr/4.5.3/include/stddef.h
Multiple include guards may be useful for:
/usr/lib/gcc/avr/4.5.3/include/stddef.h
$

and yes my two examples are using different versions of the compiler. The find above is looking at an Arduino 1.6.3 installation with a 4.8.1 compiler.

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

Sorry for the late response...

 

 

I have decided to make a specific Arduino library structure (compatible with the Arduino IDE Library standard) and a separate AVR structure for plain AVR use (Atmel Studio). These structures are generated from the source project automatically (powershell). I had to fiddle with the includes (and relative paths) between the files in my lib but I think I now have it working. Visual Micro still shows some errors in the IDE (Visual Studio) but it compiles fine now. I think that is part of what threw me off earlier. I also started making Arduino .ino examples for the library which also helped me to work out some of the (compile) problems that were in there. 

 

So indeed it seems the avr-gcc include files can be used in both AVR (Atmel Studio) and Arduino environments. 

Thanks for the responses.

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

So indeed it seems the avr-gcc include files can be used in both AVR (Atmel Studio) and Arduino environments. 

It's not just "can", they are. The "unseen" main used for an Arduino .ino sketch is this:

~/windows/Program Files/arduino-1.6.3/hardware/arduino/avr/cores/arduino$ cat main.cpp 
/*
  main.cpp - Main loop for Arduino sketches
  Copyright (c) 2005-2013 Arduino Team.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <Arduino.h>

//Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (*func)()) { return 0; }

// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }

int main(void)
{
	init();

	initVariant();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

Note the single include there. But that is actually:

~/windows/Program Files/arduino-1.6.3/hardware/arduino/avr/cores/arduino$ grep "#include" Arduino.h 
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <avr/pgmspace.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "binary.h"
#include "WCharacter.h"
#include "WString.h"
#include "HardwareSerial.h"
#include "USBAPI.h"
#include "pins_arduino.h"

All of the <> style includes within this are standard avr-gcc or AVR-LibC headers. So any .ino you write is already including all those headers (and the ones that they in turn include).

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

Yes, I noticed the same avr-gcc tools in both environments - which of course if very logical, I just did not think of it in advance.

 

I include the standard header files for the definitions I use in my library files locally in each file. This may be redundant in the Arduino environment but I believe in self-containment like that. In the AVR environment this excludes the program from having to know the dependencies.