I am trying to build an Arduino project using CMake instead of the Arduino IDE. I understand that i have to link all the C and CPP files in the avr core except for main, so I've renamed /Users/david/Library/Arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/main.cpp
to main.cc
and I have the following CMake file.
cmake_minimum_required(VERSION 3.20)
set(HAVE_FLAG_SEARCH_PATHS_FIRST 0)
set(CMAKE_C_COMPILER avr-gcc)
set(CMAKE_CXX_COMPILER avr-g++)
set(CMAKE_C_COMPILER_FORCED 1)
set(CMAKE_CXX_COMPILER_FORCED 1)
set(CMAKE_CXX_STANDARD 20)
project(untitled)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_FLAGS "-DF_CPU=16000000 -D__AVR_ATmega16U2__=1 -mmcu=atmega2560 -O2")
set(CMAKE_C_FLAGS "-DF_CPU=16000000 -D__AVR_ATmega16U2__=1 -mmcu=atmega2560 -O2")
file(GLOB SOURCES "/Users/david/Library/Arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/*.cpp" "/Users/david/Library/Arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/*.c" "main.cpp")
#list(FILTER SOURCES EXCLUDE REGEX "*main\\.cpp")
#https://stackoverflow.com/a/49377205/983556
#list(REMOVE_ITEM SOURCES "main.cpp")
#file(GLOB SOURCES "./*.cpp")
add_definitions(-D__AVR_ATmega16U2__)
add_executable(untitled ${SOURCES})
# changes c++ standard from gnu++YEAR to c++YEAR
# seems to fix issues using bear and clangd
set_target_properties(untitled PROPERTIES CXX_EXTENSIONS OFF)
target_include_directories(untitled PUBLIC "/Users/david/Library/Arduino15/packages/arduino/hardware/avr/1.8.6/libraries/EEPROM/src/")
target_include_directories(untitled PUBLIC "/Users/david/Library/Arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/")
target_include_directories(untitled PUBLIC "/Users/david/Library/Arduino15/packages/arduino/hardware/avr/1.8.6/variants/standard")
target_include_directories(untitled PUBLIC "/Users/david/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/avr/include/")
target_include_directories(untitled PUBLIC "/Users/david/C/arduino/avr-libstdcpp/include")
And I'm getting the following error when trying to run CMake:
/Users/david/Library/Arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/USBCore.cpp:75:29: error: 'USB_VID' was not declared in this scope
75 | D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1);
Does anyone know how to fix this or where I can find decent information on how to build Arduino without the IDE?