TX T
TX T

Reputation: 817

CLion Arduino undefined reference

I have tried most of the suggestion regarding this issue to no avail.

I have created an Arduino project in CLion(version 2017.3.2) with Arduino plugin (version 1.2.3). I kept getting "undefined reference" during build.

My main Sample.ino file is:

#include <Arduino.h>
#include "Hello.h"

void setup() {
}

void loop() {
    Hello::world();
}

and the Hello.h is simply:

#ifndef SAMPLE_HELLO_H
#define SAMPLE_HELLO_H


struct Hello {
    static void world();
};


#endif //SAMPLE_HELLO_H

the Hello.cpp is #include "Hello.h"

void Hello::world() {
}

and the CMakeLists.txt is:

cmake_minimum_required(VERSION 2.8.4)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake/ArduinoToolchain.cmake)
set(PROJECT_NAME Sample)
project(${PROJECT_NAME})

set(${CMAKE_PROJECT_NAME}_SKETCH src/Sample.ino)

#include_directories(include)
include_directories(src)

#### Uncomment below additional settings as needed.
set(${CMAKE_PROJECT_NAME}_BOARD mega)
set(${CMAKE_PROJECT_NAME}_PORT /dev/ttyACM0)
set(mega.build.mcu atmega2560)
# set(mega.upload.protocol wiring)
set(mega.upload.speed 115200)

generate_arduino_firmware(${CMAKE_PROJECT_NAME})

All source files are under the folder src/,

Any suggestion to what have I missed?

Upvotes: 1

Views: 1054

Answers (3)

staad
staad

Reputation: 846

The Arduino support plugin uses arduino-cmake which has a bug causing your SRCS and HDRS to be erased when compiling a sketch.

You can read my bug report and proposed fix here. Basically you want to modify cmake/Platform/Arduino.cmake

  1. Change line 1808 to avoid overwriting your sources

set(ALL_SRCS ${SKETCH_SOURCES})

set(ALL_SRCS ${ALL_SRCS} ${SKETCH_SOURCES})

  1. Change line 1821 to recompile when a source changes

DEPENDS ${MAIN_SKETCH} ${SKETCH_SOURCES}

DEPENDS ${MAIN_SKETCH} ${ALL_SRCS}

Upvotes: 3

Ilya Panin
Ilya Panin

Reputation: 91

I had the same problem and spent several hours to find a solution. Arduino plugin uses arduino-cmake to build project. According to its documentation, to use generate_arduino_firmware command you should specify SKETCH or SRCS option.

If I define SKETCH option , I has no success to link additional files (hello.cpp). So I try to use SRCS. Seems that if I define SKETCH and SRCS simultaneously, SRCS is be ignored. Another problem is when I specify src/sample.ino in SRCS option I have the same error. So I do the following to compile the project successfully:

  1. Renamed src/sample.ino into src/sample.cpp.
  2. Commented out SKETCH and add SRCS option to list all source files:
set(${PROJECT_NAME}_SRCS src/sample.cpp src/hello.cpp)

Full CMakeList.txt is

cmake_minimum_required(VERSION 2.8.4)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake/ArduinoToolchain.cmake)
set(CMAKE_CXX_STANDARD 14)
set(PROJECT_NAME Sample)
project(${PROJECT_NAME})

set(${PROJECT_NAME}_BOARD uno)
set(${PROJECT_NAME}_SRCS src/sample.cpp src/hello.cpp)
set(${PROJECT_NAME}_PROGRAMMER arduinoasisp)
set(${PROJECT_NAME}_PORT COM17)
set(${PROJECT_NAME}_AFLAGS -v)

generate_arduino_firmware(${PROJECT_NAME})

Upvotes: 5

I had that problem too. Adding this to the CMakeLists made it work:

include_directories(${PROJECT_SOURCE_DIR})
file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/*.cpp)
file(GLOB HDR_FILES ${PROJECT_SOURCE_DIR}/*.h)

set(PROJ_SRC ${SRC_FILES})
set(PROJ_HDR ${HDR_FILES})

generate_arduino_firmware(${CMAKE_PROJECT_NAME}
    SRCS ${PROJ_SRC}
    HDRS ${PROJ_HDR}
    )

I'm not sure if that's the best solution but it worked in my case.

Upvotes: 2

Related Questions