Library project fails to compile ("undefined reference to setup/loop")

Hi all,

I’m a noob to PlatformIO. After installing PIO Core, I tried to migrate from Arduino IDE a minimal library project of mine (which works just fine in Arduino IDE) – this is its structure:

examples/MyExample/MyExample.ino
src/MyClass.h
src/MyClass.cpp

I configured this project adding platformio.ini at its root:

[platformio]
default_envs = uno

[env]
framework = arduino

[env:uno]
platform = atmelavr
board = uno

When I execute pio run, the linker grumbles about the lack of setup/loop functions:

Processing uno (platform: atmelavr; board: uno; framework: arduino)
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/uno.html
PLATFORM: Atmel AVR 2.2.0 > Arduino Uno
HARDWARE: ATMEGA328P 16MHz, 2KB RAM, 31.50KB Flash
DEBUG: Current (simavr) On-board (simavr)
PACKAGES: 
 - framework-arduino-avr 5.0.0 
 - toolchain-atmelavr 1.50400.190710 (5.4.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 5 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio/build/uno/src/MyClass.cpp.o
Archiving .pio/build/uno/libFrameworkArduinoVariant.a
Indexing .pio/build/uno/libFrameworkArduinoVariant.a
Compiling .pio/build/uno/FrameworkArduino/CDC.cpp.o
. . .
Archiving .pio/build/uno/libFrameworkArduino.a
Indexing .pio/build/uno/libFrameworkArduino.a
Linking .pio/build/uno/firmware.elf
/tmp/ccM7EOrd.ltrans0.ltrans.o: In function `main':
<artificial>:(.text.startup+0x86): undefined reference to `setup'
<artificial>:(.text.startup+0x8e): undefined reference to `loop'
collect2: error: ld returned 1 exit status
*** [.pio/build/uno/firmware.elf] Error 1

To my understanding, the examples subfolders should be automatically traversed for compilation by default (am I wrong?); I examined the documentation and skimmed this forum, but couldn’t find any appropriate clue for tweaking my configuration…

Trying to sort out my issue by analogy, I therefore looked for existing PlatformIO-enabled projects which had a structure similar to my own library (src+examples): https://github.com/jyberg/Enhanced-Nextion-Library.git seemed to fit my case, but when I executed pio run on it, it FAILED the SAME WAY (??)

Processing uno (platform: atmelavr; board: uno; framework: arduino)
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/uno.html
PLATFORM: Atmel AVR 2.2.0 > Arduino Uno
HARDWARE: ATMEGA328P 16MHz, 2KB RAM, 31.50KB Flash
DEBUG: Current (simavr) On-board (simavr)
PACKAGES: 
 - framework-arduino-avr 5.0.0 
 - toolchain-atmelavr 1.50400.190710 (5.4.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 6 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <SdFat> 1.1.4
|   |-- <SPI> 1.0
|-- <SoftwareSerial> 1.0
Building in release mode
Compiling .pio/build/uno/src/NexButton.cpp.o
. . .
Compiling .pio/build/uno/lib632/SPI/SPI.cpp.o
. . .
Compiling .pio/build/uno/lib4c3/SdFat/FatLib/FatFile.cpp.o
. . .
Compiling .pio/build/uno/FrameworkArduino/CDC.cpp.o
. . .
Linking .pio/build/uno/firmware.elf
/tmp/ccd8nyUa.ltrans0.ltrans.o: In function `main':
<artificial>:(.text.startup+0x86): undefined reference to `setup'
<artificial>:(.text.startup+0x8a): undefined reference to `loop'
collect2: error: ld returned 1 exit status
*** [.pio/build/uno/firmware.elf] Error 1

Assuming that Enhanced-Nextion-Library is properly configured, what is the reason preventing my compilations?

setup() and loop() are the counterparts for int main(int argc, char ** argv) from classic standalone C/C+±programs.

The library you have mentioned does not contain any file with functions called loop() or setup() - which is absolutely correct, because the user of such a library must define (and implement :wink: ) these functions in his own code. They can’t be supplied by the library. I assume that this is also true for your lib.

Thanks for replying, but that was already clear to me – citing my examples subfolder I purposely intended that its .ino sketch (examples/MyExample/MyExample.ino) contained the required setup/loop implementations, I was just wondering why it hadn’t been included in the compilation.

So, my whole point is: given my library project structure (src+examples), how can I compile successfully its source code via PIO Core? (I stress again that examples/MyExample/MyExample.ino contains setup/loop functions and has already been successfully compiled in Arduino IDE)

That is: given my library project structure (src+examples), how can I instruct PIO to compile its examples (examples/MyExample/MyExample.ino)? Is platformio.ini configuration enough? Is there anything else I’m overlooking?

More generally speaking: what’s the standard way to compile a library (along with its example sketches) via PIO Core?

thanks for your help!

I have had the same issue for my library and used the following platformio.ini setup:

[platformio]
lib_dir = src ;This points to the library sources itself
src_dir = examples/TrainHub ;this points to a single example *.ino sketch which includes the setup/loop functions

[env:heltec_wifi_kit_32]
platform = espressif32
board = heltec_wifi_kit_32
framework = arduino
lib_deps = h2zero/NimBLE-Arduino @ ^1.0.2

The lib_dir is pointing to the library sources itself in the src directory. The example which uses the library and contains the setup and loop functions is defined in the src_dir property pointing to the examples folder. It was important, that the lib_dir and src_dir properties are part of the [platformio] section and not located in the [env] section. I have used it first in the env section but this was not read in or used.

After an additional check the above configuration does not work. I have to change it to the following one:

[platformio]
src_dir = examples/TrainHub ;this points to a single example *.ino sketch which includes the setup/loop functions

[env:heltec_wifi_kit_32]
platform = espressif32
board = heltec_wifi_kit_32
framework = arduino
lib_extra_dirs = ${workspacedir} ;this points to the root directory where the arduino library is located
lib_deps = h2zero/NimBLE-Arduino@^1.0.2 ;this is a dependend library of the arduino library

with this settings i could get it up an running from a clean state