0

Warning: My C++ is rusty as hell. It's been years since I've written some c++ and was years before that time. I've googled and looked at a bunch of issues and can't seem to find mine.

I'm using CLion for the first time and coming up speed on Arduino.

I'm getting this error: undefined reference to `WaterDispenser::getInstance() on the following line of code in my ino file:

WaterDispenser  *waterDispenser = WaterDispenser::getInstance();

I have the following in my header water-diospenser.h.

#ifndef PROCESSCONTROLLER_WATER_DISPENSER_H
#define PROCESSCONTROLLER_WATER_DISPENSER_H

#include "mega2560types.h"
#include "io-pins.h"

class WaterDispenser {
public:
    const UINT16 COUNT_1LITER = 1000;
    const UINT16 COUNT_5LITER = 5000;
    const UINT16 COUNT_20LITER = 20000;

    static WaterDispenser *getInstance();

    virtual void check();
    static void onDispenseFlowmeter();

private:
    WaterDispenser();
    virtual ~WaterDispenser();

    virtual void checkDispensing();
    virtual void checkNotDispensing();
    /*
     * See if we are currently dispensing service water. If so, return true.
     */
    virtual bool dispensingServiceWater();
    virtual void setCurrentFlow();
    virtual void setDispense(bool flowOn);

    bool dispensing = false;
    bool serviceDispensing = false;
    UINT16 dispenseCutoff = 0;
    UINT16 dispenseCount = 0;
    static WaterDispenser *myInstance;
};

WaterDispenser *WaterDispenser::myInstance = nullptr;


#endif //PROCESSCONTROLLER_WATER_DISPENSER_H

and this in watter-dispenser.cpp.

#include "water-dispenser.h"

WaterDispenser *WaterDispenser::getInstance() {
    if (!myInstance) {
        myInstance = new WaterDispenser;
    }
    return myInstance;
}

I'm new to cmake, but here's my CMakeLsits.txt.

cmake_minimum_required(VERSION 2.8.4)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake/ArduinoToolchain.cmake)
set(CMAKE_CXX_STANDARD 17)
set(PROJECT_NAME processcontroller)

set(${PROJECT_NAME}_BOARD megaADK)
# set(ARDUINO_CPU)
project(${PROJECT_NAME})

# Define additional source and header files or default arduino sketch files
# set(${PROJECT_NAME}_SRCS)
# set(${PROJECT_NAME}_HDRS)

### Additional static libraries to include in the target.
# set(${PROJECT_NAME}_LIBS)

### Main sketch file
set(${PROJECT_NAME}_SKETCH processcontroller.ino)

### Add project directories into the build
add_subdirectory(extern/googletest)

### Additional settings to add non-standard or your own Arduino libraries.
# For this example (libs will contain additional arduino libraries)
# An Arduino library my_lib will contain files in libs/my_lib/: my_lib.h, my_lib.cpp + any other cpp files
# link_directories(${CMAKE_CURRENT_SOURCE_DIR}/)

# For nested library sources replace ${LIB_NAME} with library name for each library
# set(_RECURSE true)

#### Additional settings for programmer. From programmers.txt
# set(${PROJECT_NAME}_PROGRAMMER avrispmkii)
# set(${PROJECT_NAME}_PORT /dev/cu.usbserial-00000000)
# set(megaADK.upload.speed 9600)

## Verbose build process
# set(${PROJECT_NAME}_AFLAGS -v)
set(SOURCE_FILES water-dispenser.cpp)

generate_arduino_firmware(${PROJECT_NAME})


print_board_list()
print_programmer_list()

Now I'm out of things to try and looking for some help.

Thom
  • 14,013
  • 25
  • 105
  • 185
  • 2
    Remove `static` from the implementation (it should be `WaterDispenser *WaterDispenser::getInstance() {`) – Yksisarvinen Feb 08 '21 at 14:05
  • tried it both ways, but now have the static keyword removed. No difference. Updated the question. – Thom Feb 08 '21 at 14:29
  • Do you get any other errors? Did you provide an implementation for `onDispenseFlowmeter` as well, as it's also static. – Devolus Feb 08 '21 at 14:31
  • No other errors. – Thom Feb 08 '21 at 14:33
  • By itself, setting `SOURCE_FILES` variable has no special meaning for CMake. Please, show (add to the question post) your `CMakeLists.txt`. – Tsyvarev Feb 08 '21 at 14:55
  • Well, I am not an expert in Arduino, but from the CMake view `SOURCE_FILES` doesn't look as proper way for adding source files for `generate_arduino_firmware`. According to [that documentation](https://github.com/queezythegreat/arduino-cmake/blob/master/README.rst#creating-firmware-images), sources should be added by modifying that line: `set(${PROJECT_NAME}_SRCS)`. But I am unsure how `SRCS` options interacts with `SKETCH` option. So your **primary** problem is how to add a source file for `generate_arduino_firmware`, so you may update the question accordingly. – Tsyvarev Feb 08 '21 at 15:30

0 Answers0