Skip to content

Instantly share code, notes, and snippets.

@balazsgrill
Created January 14, 2019 14:45
Show Gist options
  • Save balazsgrill/bff964a93082749d57678aca69312f04 to your computer and use it in GitHub Desktop.
Save balazsgrill/bff964a93082749d57678aca69312f04 to your computer and use it in GitHub Desktop.
Compiling arduino code using CMake
set(ARDUINO_IDE_PATH ARDUINO_IDE_NOT_FOUND CACHE FILEPATH "")
set(CMAKE_SYSTEM_NAME "Generic")
set(CMAKE_C_COMPILER "${ARDUINO_IDE_PATH}/hardware/tools/avr/bin/avr-gcc.exe")
set(CMAKE_CXX_COMPILER "${ARDUINO_IDE_PATH}/hardware/tools/avr/bin/avr-g++.exe")
include_directories("${ARDUINO_IDE_PATH}/hardware/arduino/avr/cores/arduino")
include_directories("${ARDUINO_IDE_PATH}/hardware/arduino/avr/variants/standard")
set(CMAKE_CXX_FLAGS "-MMD -mmcu=atmega328p -Wall -DF_CPU=16000000L -Os")
set(CMAKE_C_FLAGS "-MMD -mmcu=atmega328p -Wall -DF_CPU=16000000L -Os")
add_library(core STATIC
"${ARDUINO_IDE_PATH}/hardware/arduino/avr/cores/arduino/main.cpp"
"${ARDUINO_IDE_PATH}/hardware/arduino/avr/cores/arduino/wiring.c"
"${ARDUINO_IDE_PATH}/hardware/arduino/avr/cores/arduino/wiring_digital.c"
"${ARDUINO_IDE_PATH}/hardware/arduino/avr/cores/arduino/wiring_analog.c"
"${ARDUINO_IDE_PATH}/hardware/arduino/avr/cores/arduino/hooks.c"
)
cmake_minimum_required(VERSION 3.12)
add_executable(myprogram myprogram.cpp)
target_link_libraries(myprogram core)
add_custom_command(
TARGET myprogram POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O ihex -R .eeprom myprogram myprogram.hex
COMMENT "OBJCOPY myprogram"
BYPRODUCTS myprogram.hex
)
#include <Arduino.h>
void setup() {
}
void loop(){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment