0

I want to create library files (.a) of the arduino core library, eventually also the other libraries (SPI, ...), with a makefile, but I can't get it to work!

This is my makefile:

CC=avr-gcc
CPP=avr-g++
MCU=-mmcu=atmega328p
CPU_SPEED=-DF_CPU=16000000UL
CFLAGS=$(MCU) $(CPU_SPEED) -g2 -gstabs -Os -Wall \
-ffunction-sections -fdata-sections -fno-exceptions
INCLUDE=-I./arduinoCORE

CFILES=$(wildcard ./arduinoCORE/*.c)
CPPFILES=$(wildcard ./arduinoCORE/*.cpp)

OBJ=$(CFILES:.c=.o) $(CPPFILES:.cpp=.o)

default: $(OBJ)
    avr-ar -r libarduinoUNO.a $^

%.o : %.c
    $(CC) $< $(CFLAGS) -c -o $@

%.o : %.cpp
    $(CPP) $< $(CFLAGS) -c -o $@

(all header and source files are in arduinoCORE; even pins_arduino.h)

After $ make in directory above arduinoCORE I get this error message:

avr-g++ arduinoCORE/CDC.cpp -mmcu=atmega328p -DF_CPU=16000000UL -g2 -gstabs -Os -Wall -ffunction-sections -fdata-sections -fno-exceptions -c -o arduinoCORE/CDC.o
In file included from arduinoCORE/Print.h:27:0,
                 from arduinoCORE/Stream.h:26,
                 from arduinoCORE/HardwareSerial.h:28,
                 from arduinoCORE/Arduino.h:193,
                 from arduinoCORE/Platform.h:15,
                 from arduinoCORE/CDC.cpp:19:
arduinoCORE/Printable.h:23:17: fatal error: new.h: No such file or directory
 #include <new.h>
                 ^
compilation terminated.

make: *** [arduinoCORE/CDC.o] Error 1  

The problem is, that new.h is actually in arduinoCORE! Does anyone know how to manage this?

Fabio
  • 191
  • 12
  • You don't actually want to do it this way. Each architecture that Arduino uses has multiple subarchitectures, and each need the libraries built slightly differently. – Ignacio Vazquez-Abrams Aug 18 '15 at 05:56
  • For the moment I want to compile only for atmega328p, if that's the problem. – Fabio Aug 18 '15 at 12:30
  • How would you do it, then? – Fabio Aug 18 '15 at 12:31
  • Rebuild for each subarch into a separate directory, and then add the appropriate directory via `-L` when linking. – Ignacio Vazquez-Abrams Aug 18 '15 at 12:47
  • Actually I don't want to link the files now, I only want to compile them to object files and then archive them. Later I want to use this archive as an argument for the linker with programs I write (adding the container directory with the `-L` flag and the library itself with `-l`). – Fabio Aug 18 '15 at 17:16
  • Being still more concise: I want to create a libXXX.a file of the Arduino core library, so that after compiling a sketch I can just link it with this file. (if I have understood well: you need the library source files (.c and .cpp) for compiling a sketch and a compiled library archive file (.a) for linking it) – Fabio Aug 18 '15 at 18:20

1 Answers1

0

I had a different error with your code. It displayed Arduino.h as not being present. I fixed it after actually adding the INCLUDE variable to CFLAGS and CPPFLAGS (yours was defined but not added).

I also used CFLAGS and CPPFLAGS as per the Arduino Specification. The code is:

CC=avr-gcc
CPP=avr-g++
MCU=-mmcu=atmega328p
CPU_SPEED=-DF_CPU=16000000UL
INCLUDE=-I./

CFLAGS = -c -g -Os -w -ffunction-sections -fdata-sections -MMD $(MCU) $(CPU_SPEED) $(INCLUDE) 
CPPFLAGS = -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD $(MCU) $(CPU_SPEED)  $(INCLUDE)

CFILES=$(wildcard ./*.c)
CPPFILES=$(wildcard ./*.cpp)

OBJ=$(CFILES:.c=.o) $(CPPFILES:.cpp=.o)

default: $(OBJ)
    avr-ar rcs core.a $^

%.o : %.c
    $(CC) $< $(CFLAGS) -c -o $@

%.o : %.cpp
    $(CPP) $< $(CPPFLAGS) -c -o $@

Which creates the archive successfully (not tested, just created).

garvamel
  • 16
  • 2