Pages: [1]   Go Down
Author Topic: Arduino and CMake  (Read 1661 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Introduction

I have been working on a project to compile the Arduino code into static libraries using the CMake build system.  This will allow more advanced developers to use any desired development environment, as the Arduino IDE tends to cater to people with little programming experience.  At the moment, it compiles, but there is still a lot to do.

Anyways, I thought I would document and share the details of getting CMake to work with the Arduino boards.  I should mention that I am using avr-gcc on Gentoo Linux, and I have no idea if everything here applies to other operating systems and compilers.

Linker Problems

The linker may complain about unresolved symbols if the __cxa_pure_virtual function is not included.  The function is an error handler that is invoked when a pure virtual function is called.  This should never happen, since it is impossible to instantiate a class that doesn't implement all virtual functions.
Code:
#define CXA_PURE_VIRTUAL_HACK \
extern "C" void __cxa_pure_virtual(void) \
{ \
}

The define makes it easier to add the function to a program.  All that is required is to add
Code:
CXA_PURE_VIRTUAL_HACK
at the beginning of the source file.

Gentoo avr-gcc Toolchain Bugs

On Gentoo, there is a bug with avr-gcc.  The error is
Code:
/usr/libexec/gcc/avr/ld: cannot open linker script file ldscripts/avr5.x: No such file or directory

A workaround is
Code:
sudo ln -s /usr/lib/binutils/avr/2.21.1/ldscripts /usr/avr/lib/ldscripts

Example CMake Build Files

arduino-common.cmake:
http://pastebin.com/xdRRs9pn

arduino-duemilanove.cmake:
Code:
set(ARDUINO_PROCESSOR "atmega328p")
set(ARDUINO_PROCESSOR_FREQ "16000000")
include(${CMAKE_SOURCE_DIR}/cmake/arduino-common.cmake)

Example CMake build script:
http://pastebin.com/L3e82ZCE

Note that the example script assumes there exists a library called libarduino-core, which is one of the libraries I built.

I forgot to document the F_CPU macro in the arduino-common.cmake file.  It is required by util/delay.h in the AVR libc library.  The functions available allow the specification of microsecond, and millisecond delays directly, using the application-supplied macro F_CPU as the CPU clock frequency (in Hertz).  The macro F_CPU is passed to the compiler in the cmake file.
Code:
-DF_CPU=${ARDUINO_PROCESSOR_FREQ}


References:
http://wiki.osdev.org/C_PlusPlus
http://www.arduino.cc/playground/Code/CmakeBuild
http://forums.gentoo.org/viewtopic-t-834097.html
http://stackoverflow.com/questions/920500/what-is-the-purpose-of-cxa-pure-virtual
http://zedcode.blogspot.com/2007/02/gcc-c-link-problems-on-small-embedded.html
http://www.nongnu.org/avr-libc/user-manual/group__util__delay.html
Logged

Pages: [1]   Go Up
Jump to: