Hello. I am receiving an 'undefined reference to' error in reference to a function that I have in a c file. It seems to only be affecting the functions that receive data from the C file, and not ones that don't return anything.
Because it's a C file, the function declarations in the header file includes the following:
#ifdef __cplusplus
extern "C" {
#endif
// function declarations here
#ifdef __cplusplus
}
#endif
In my code below, the function "stateInit()" found in the "setup()" function works properly, but the function "stateGetMachineState()" in the loop() function is the one that flags the error.
You'll find the function declarations in state.h at the bottom.
Additionally, I've tried putting extern "C" {//functions in c file} here but I get the error:
"expected identifier or '(' before string constantextern "C" {}
Any help is appreciated, thanks!
main file:
#include "state.h"
void setup() {
stateInit();
}
void loop() {
MachineState ms = stateGetMachineState();
delay(100);
}
state.h
#ifndef STATE_H
#define STATE_H
#include <Arduino.h>
#define NUMBER_OF_POTS 5
#define NUMBER_OF_CV_CONTROLLABLE_PARAMS 4
/*
*##############################
* MACHINE STATE
*##############################
*/
typedef enum StatePage {
PAGE_CONTROL,
PAGE_MENU,
NUMBER_OF_STATE_PAGES
} StatePage;
typedef enum ModulatorNumber {
MODULATOR_1,
MODULATOR_2,
MODULATOR_3,
MODULATOR_4,
NUMBER_OF_MODULATORS
} ModulatorNumber;
typedef struct MachineState {
StatePage current_page;
ModulatorNumber current_modulator;
} MachineState;
/*
*##############################
* MODULATOR STATE
*##############################
*/
typedef enum ModulatorType {
MOD_TYPE_LFO,
MOD_TYPE_ENV,
NUMBER_OF_MOD_TYPES
} ModulatorType;
typedef struct ModulatorData {
ModulatorNumber mod_number;
ModulatorType mod_type;
uint8_t cv_control_param;
bool follow_clock_in_1;
bool clock_sync;
bool LFO_reset;
bool env_loop;
uint8_t param[NUMBER_OF_POTS];
} ModulatorData;
/*
*##############################
* FUNCTIONS
*##############################
*/
#ifdef __cplusplus
extern "C" {
#endif
MachineState stateGetMachineState();
ModulatorData stateGetCurrentModulatorData();
ModulatorData stateGetModulatorData(ModulatorNumber n);
void stateInit();
void stateSetCurrentModulator(ModulatorNumber n);
void stateSetCurrentPage(StatePage s);
void stateSetModType(ModulatorType t, ModulatorNumber n);
void stateSetFollowClockIn1(bool f, ModulatorNumber n);
void stateSetClockSync(bool s, ModulatorNumber n);
void stateSetLFOReset(bool r, ModulatorNumber n);
void stateSetEnvLoop(bool l, ModulatorNumber n);
void stateSetParam(uint8_t amount, uint8_t p, ModulatorNumber n);
#ifdef __cplusplus
}
#endif
#endif
state.c
#include "state.h"
#include <Arduino.h>
MachineState machineState;
ModulatorData mod[NUMBER_OF_MODULATORS];
const static uint8_t mod_init_param[NUMBER_OF_MODULATORS][NUMBER_OF_POTS] = {
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
};
MachineState getMachineState() {return machineState;}
ModulatorData getCurrentModulatorData() {return mod[machineState.current_modulator];}
ModulatorData getModulatorData(ModulatorNumber n) {return mod[n];}
void stateInit() {
machineState = (MachineState) {
.current_page = PAGE_CONTROL,
.current_modulator = MODULATOR_1
};
for(int i = 0; i < NUMBER_OF_MODULATORS; i++) {
mod[i] = (ModulatorData) {
.mod_number = i,
.mod_type = MOD_TYPE_LFO,
.follow_clock_in_1 = true,
.clock_sync = true,
.LFO_reset = false
};
for(int j = 0; j < NUMBER_OF_POTS; j++) {
mod[i].param[j] = mod_init_param[i][j];
}
}
}
void stateSetCurrentModulator(ModulatorNumber n) {
machineState.current_modulator = n;
}
void stateSetCurrentPage(StatePage s) {
machineState.current_page = s;
}
void stateSetModType(ModulatorType t, ModulatorNumber n) {
mod[n].mod_type = t;
}
void stateSetFollowClockIn1(bool f, ModulatorNumber n) {
mod[n].follow_clock_in_1 = f;
}
void stateSetClockSync(bool s, ModulatorNumber n) {
mod[n].clock_sync = s;
}
void stateSetLFOReset(bool r, ModulatorNumber n) {
mod[n].LFO_reset = r;
}
void stateSetEnvLoop(bool l, ModulatorNumber n) {
mod[n].env_loop = l;
}
void stateSetParam(uint8_t amount, uint8_t p, ModulatorNumber n) {
mod[n].param[p] = amount;
}