Arduino链接未定义参考

时间:2019-03-03 11:43:34

标签: c++ cmake compiler-errors arduino linker-errors

我正在尝试为一个小型Arduino项目创建一个cmake文件。

我已使用Arduino IDE的输出作为查找所需文件和命令的起点。现在,我正在编译Arduino IDE等所有源代码,然后尝试将它们链接在一起。

在CMake中创建可执行文件:

add_executable(${TARGET_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/src/buttontest.cpp)

set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS "-T${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/variants/mkrwifi1010/linker_scripts/gcc/flash_with_bootloader.ld \
-Wl,-Map,${TARGET_NAME}.ino.map --specs=nano.specs --specs=nosys.specs -mcpu=cortex-m0plus -mthumb -Wl,--cref -Wl,--check-sections \
-Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align")


target_link_libraries(
    ${TARGET_NAME}
    wiring_digital
    variant 
    uart    
    ...
)

当我尝试运行它时,总是会收到未定义的引用错误

../libuart.a(Uart.cpp.obj):(.rodata._ZTI4Uart+0x8): undefined reference to `typeinfo for HardwareSerial'
collect2: error: ld returned 1 exit status

在我看来,链接器无法从我编译的sercom库中找到某些内容,但我不明白为什么。

我们在github上找到了arduino-cmake存储库,但由于使用AVR控制器的主板需要使用该代码,因此我们无法使用该代码,而必须使用ARM处理器(Arduino MKR 1010 Wifi)。

任何有关链接器错误的建议将不胜感激!如果将CMake用于Arduino项目的方法存在根本缺陷,我也将感谢其他选择。

项目结构:

project
 |- build/
 |- buttontest/
    |- CMakeLists.txt
    |- src/buttontest.cpp
 |-cmake/MKRWIFI1010_Toolchain.cmake
 |-CMakeLists.txt

CMakeLists.txt:

set(CMAKE_TOOLCHAIN_FILE cmake/MKRWIFI1010_Toolchain.cmake)

cmake_minimum_required(VERSION 3.13)

project(buttontest)

set (ARDUINO_PACKAGES $ENV{ARDUINO_PACKAGES})

message(STATUS "${ARDUINO_PACKAGES}")

include_directories(
    ${ARDUINO_PACKAGES}/arduino/tools/CMSIS/4.5.0/CMSIS/Include/ 
    ${ARDUINO_PACKAGES}/arduino/tools/CMSIS-Atmel/1.1.0/CMSIS/Device/ATMEL/ 
    ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino 
    ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/variants/mkrwifi1010 
)

# Building common files

# Add persistent compile flags here
add_compile_options (-c -g -MMD -DF_CPU=48000000L -DARDUINO=10808 -DARDUINO_SAMD_MKRWIFI1010 -DARDUINO_ARCH_SAMD
-DUSE_ARDUINO_MKR_PIN_LAYOUT -D__SAMD21G18A__ -DUSB_VID=0x2341 -DUSB_PID=0x8054 -DUSBCON "-DUSB_MANUFACTURER=\"Arduino LLC\"" 
"-DUSB_PRODUCT=\"Arduino MKR WiFi 1010\"" -DUSE_BQ24195L_PMIC)

# Add temporal compile flags here
# FIXME: There must be a better way to do this, but anyway, there must be a better way to write an Arduino CMake Script...
set(CMAKE_C_FLAGS "-x assembler-with-cpp")

set_property(SOURCE ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/pulse_asm.S PROPERTY LANGUAGE C)
add_library(pulse_asm ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/pulse_asm.S)

# Remove temporal compile flags

add_compile_options (-mcpu=cortex-m0plus -mthumb -Os -Wall -Wextra -ffunction-sections -fdata-sections -nostdlib 
--param max-inline-insns-single=500 -Wno-expansion-to-defined)

# C Libraries

add_library(winterrupts ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/WInterrupts.c)

set(CMAKE_C_FLAGS "-std=gnu11")

add_library(itoa ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/itoa.c)
add_library(wiring_private ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/wiring_private.c)
add_library(wiring_digital ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/wiring_digital.c)
add_library(cortex_handlers ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/cortex_handlers.c)
add_library(wiring_shift ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/wiring_shift.c)
add_library(samd21_host ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/USB/samd21_host.c)
add_library(pulse ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/pulse.c)
add_library(startup ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/startup.c)
add_library(delay ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/delay.c)
add_library(wiring ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/wiring.c)
add_library(dtostrf ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/avr/dtostrf.c)
add_library(wiring_analog ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/wiring_analog.c)
add_library(hooks ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/hooks.c)

set(CMAKE_C_FLAGS "-std=gnu++11")
# CPP Libraries


add_library(ipaddress ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/IPAddress.cpp)
add_library(wmath ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/WMath.cpp)
add_library(new ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/new.cpp)
add_library(reset ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/Reset.cpp)
add_library(sercom ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/SERCOM.cpp)
add_library(cdc ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/USB/CDC.cpp)
add_library(stream ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/Stream.cpp)
add_library(usbcore ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/USB/USBCore.cpp)
add_library(print ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/Print.cpp)
add_library(tone ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/Tone.cpp)
add_library(pluggableusb ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/USB/PluggableUSB.cpp)
add_library(wstring ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/WString.cpp)
add_library(abi ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/abi.cpp )
add_library(uart ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/Uart.cpp)
add_library(main ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/cores/arduino/main.cpp)

add_compile_options(-fno-threadsafe-statics -fno-rtti -fno-exceptions)


add_library(variant ${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/variants/mkrwifi1010/variant.cpp)

add_subdirectory(buttontest)

cmake / MKRWIFI1010_Toolchain.cmake:

set(CMAKE_SYSTEM_NAME Generic)

set (ARDUINO_PACKAGES $ENV{ARDUINO_PACKAGES})

set(CMAKE_C_COMPILER   ${ARDUINO_PACKAGES}/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER ${ARDUINO_PACKAGES}/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-g++)

buttontest / CMakeLists.txt:

set(TARGET_NAME "buttontest")

link_directories(
    ${ARDUINO_PACKAGES}/arduino/tools/CMSIS/4.5.0/CMSIS/Lib/GCC/ 
    {CMAKE_BINARY_DIR} 
    {CMAKE_BINARY_DIR}/buttontest)

add_compile_options(-fno-threadsafe-statics -fno-rtti -fno-exceptions)

add_executable(${TARGET_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/src/buttontest.cpp)

set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS "-T${ARDUINO_PACKAGES}/arduino/hardware/samd/1.6.20/variants/mkrwifi1010/linker_scripts/gcc/flash_with_bootloader.ld \
-Wl,-Map,${TARGET_NAME}.ino.map --specs=nano.specs --specs=nosys.specs -mcpu=cortex-m0plus -mthumb -Wl,--cref -Wl,--check-sections \
-Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align")

target_link_libraries(
    ${TARGET_NAME} wiring_digital variant sercom uart arm_cortexM0l_math cdc pulse pulse_asm ipaddress
    cortex_handlers print reset stream tone usbcore pluggableusb samd21_host winterrupts wmath wstring
    abi dtostrf startup hooks itoa new wiring delay main sercom wiring_analog wiring_private wiring_shift)

1 个答案:

答案 0 :(得分:0)

听起来像您缺少HardwareSerial类的一个或多个虚拟成员的函数主体定义。也许您错过了定义,例如HAVE_HWSERIAL0?