1

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?

David Carpenter
  • 1,389
  • 2
  • 16
  • 29
  • possibly related: https://electronics.stackexchange.com/q/44577/326729, https://community.platformio.org/t/pro-micro-atmega32u4-usb-vid-was-not-declared-in-this-scope/29949, https://github.com/tobozo/WUD-Ducky/issues/4 – starball Apr 08 '23 at 23:02
  • Your handling device is odd. If you want code for an ATmega2560, use `-mmcu=atmega2560`. For an ATmega16U2, use `-mmcu=atmega16u2`. These devices have different capabilities, different `` attached to them, and belong to different multilib-sets. – emacs drives me nuts Apr 18 '23 at 14:46

1 Answers1

1

Looking at Debian's package for Arduino arduino-core-avr, there is a file /usr/share/arduino/hardware/arduino/avr/platform.txt, containing:

build.usb_flags=-DUSB_VID={build.vid} -DUSB_PID={build.pid}

which in turn is referenced in a file called /usr/share/arduino/hardware/arduino/avr/boards.txt

At the top of platform.txt is a link Platform specification describing both platform.txt and boards.txt. One section is about Board VID/PID

Board VID/PID

USB vendor IDs (VID) and product IDs (PID) identify USB devices to the computer.

So it looks like you can just add the appropriate defines to your compiler flags, e.g.

-DUSB_VID=0x1234 -DUSB_PID=0x5678

The specific ids depend on the USB device you have on your Arduino board.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198