Compile / linking error

I am trying to program a ESP32 to replace a controller that failed on my solar tracker. The original controller is not currently available due to the chip shortage. There is a algorithm written by NREL that does a great job calculating the current sun position. Their test program runs great on CodeBlocks IDE but I get compile errors when I try to run it in PlatformIO.

Here is the output when trying to compile:

LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 32 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\esp32dev\src\main.cpp.o
Linking .pio\build\esp32dev\firmware.elf
c:/users/bdrma/.platformio/packages/toolchain-xtensa-esp32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio\build\esp32dev\src\main.cpp.o:(.literal._Z5setupv+0x50): undefined reference to `spa_calculate(spa_data*)'
c:/users/bdrma/.platformio/packages/toolchain-xtensa-esp32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio\build\esp32dev\src\main.cpp.o: in function `setup()':
C:\Users\bdrma\Documents\PlatformIO\Projects\spatest/src/main.cpp:42: undefined reference to `spa_calculate(spa_data*)'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\esp32dev\firmware.elf] Error 1

I put “spa.h” in the project include folder and “spa.c” in the src folder.
Can someone Please point out what I am doing wrong?


Here is the main.c file:

#include <Arduino.h>

#include <stdlib.h>

#include <stdio.h>

#include "spa.h"  //include the SPA header file

int result;

spa_data spa;  //declare the SPA structure

void setup()


//enter required input values into SPA structure

    spa.year          = 2022;

    spa.month         = 8;           = 4;

    spa.hour          = 13;

    spa.minute        = 14;

    spa.second        = 0;

    spa.timezone      = -5.0;

    spa.delta_ut1     = 0;

    spa.delta_t       = 67;

    spa.longitude     = 90.7;

    spa.latitude      = 41.513;

    spa.elevation     = 990.0;

    spa.pressure      = 820;

    spa.temperature   = 11;

    spa.slope         = 30;

    spa.azm_rotation  = -10;

    spa.atmos_refract = 0.5667;

    spa.function      = SPA_ZA;

result = spa_calculate(&spa);

    if (result == 0)  //check for SPA errors


        //display the results inside the SPA structure

        printf("Epsilon:       %.6f degrees\n",spa.epsilon);

        printf("Zenith:        %.6f degrees\n",spa.zenith);

        printf("Azimuth:       %.6f degrees\n",spa.azimuth);

    } else printf("SPA Error Code: %d\n", result);  


void loop()




As you wrote here, the file is written in .c. So, since you want to interact with this from a C++ file (main .cpp), you have to use extern "C" to disable name-mangling, so that the C++ code will expect C-style function names, not C++ ones.
Instead of


extern "C" {
#include "spa.h"
1 Like

Problem Solved.
I can’t thank you enough!!!

Very much appreciated.