Undefined reference to `setup' and 'loop'

Hello I’m new here. I’m using VSCode with PlatformIO. Now my Problem: Every Time I create a new project and try it to run, it don’t work
main.cpp:

#ifdef IS_LORA
#include <Arduino.h>
#include <SX126x-Arduino.h>
#include "main.h"
#include "flash.h"
#include "lora.h"

void setup() {   
    Serial.begin(115200);
	LOG("APP", "test set up=================");
// logic setup

void loop() {
	// logic loop
}
#endif

platformio.ini:

[env:rak4631_lorawan]
extends = nrf52840_base
board = wiscore_rak4631
framework = arduino
upload_protocol = jlink
build_flags = 
	${nrf52840_base.build_flags}
	-DIS_LORA
	-Ivariants/rak4631
	-DARDUINO_ARCH_NRF52840
	-DARDUINO_NRF52840_FEATHER
	'-D DEVICE_EUI_INFO="AC1F09FFFE1EF0F2"'
	'-D DEVICE_APP_KEY_INFO="2d97fc13fdcd1132ed9a000bb3982451"'
	-D DEVICE_REGION=9
	-D INTERVAL_SAMPLE_S=600
	-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_ERROR
	-DLIB_DEBUG=0
	-Wno-maybe-uninitialized
    -std=c++17
    -Llib/RAK1906B/src/cortex-m4/fpv4-sp-d16-hard
build_unflags = 
	-std=gnu++11
build_src_filter = 
	+<../variants/rak4631/>
	+<src/main.cpp>
	-<mqtt/>
	-<graphics/>
	-<src/Power*>
	-<src/RedirectablePrint*>
src_filter = +<src/main.cpp>
lib_deps = 
	beegee-tokyo/SX126x-Arduino
	bblanchon/ArduinoJson@^7.1.0

Error:

Linking .pio/build/rak4631_lorawan/firmware.elf
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libFrameworkArduino.a(main.cpp.o): in function `loop_task(void*)':
/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/cores/nRF5/main.cpp:60: undefined reference to `setup'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: /Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/cores/nRF5/main.cpp:69: undefined reference to `loop'
collect2: error: ld returned 1 exit status
*** [.pio/build/rak4631_lorawan/firmware.elf] Error 1

Does anyone knows how to solve it and can help me?

I don’t know the used platform and I don’t know the used board, but the platformio.ini is way to complex and has a lot of unnecessary settings for that little piece of code.

I suggest to start with the most minimalistic settings.
There must be some examples available for the used platform.

1 Like

Hi @sivar2311 !
I’m using RAK4631 and I’m trying to build LoRaWAN for RAK4631 using platformIO.
I’m setting flags for 2 features. if IS_LORA flag is called in platformIO then firmware will execute code contained in IS_LORA flag and so on.

But the platformio.ini still looks way to complex for the code.
Do you know every setting in your platformio.ini an it’s impact?

Try to keep things simple as possible to reduce error sources.

Usually for your code there should be just a few entries like

  • Platform
  • Framework
  • Board
  • Monitor speed

Something like this:

[env:rak4631_lorawan]
platform = ? <-- Put your used platform in here.
board = wiscore_rak4631
framework = arduino
monitor_speed = 115200

Then try to build the code again.

src_filter is an outdated way of saying build_src_filter. All the paths in src_filter are relative to the src/ directory. Thus, if you have src/main.cpp, then there is no file src/src/main.cpp which the src_filter tries to build.

Just remove the src_filter = .. expression and stick to build_src_filter while changing all src/xxx paths to be just xxx. Don’t use src_filter at all.

All of this is clearly documented.

Hi everyone ! @sivar2311 @maxgerhardt
My project structure:
src/
├── main_lora.cpp
├── main_mesh.cpp

code Main_lora.cpp:

# indef IS_LORA
.... code lora
# endif 

code main_mesh.cpp:

# indef IS_MESH
.... code mesh
# endif

I built the rak4631_lorawan environment in platformIO:

[env:rak4631_lorawan]
platform = nordicnrf52
extends = nrf52840_base
board = wiscore_rak4631
framework = arduino
upload_protocol = jlink
monitor_speed = 115200
build_flags = 
	${nrf52840_base.build_flags}
	-DIS_LORA=1
	-Ivariants/rak4631
	-DARDUINO_ARCH_NRF52840
	-DARDUINO_NRF52840_FEATHER
	'-D DEVICE_EUI_INFO="AC1F09FFFE1EF0F2"'
	'-D DEVICE_APP_KEY_INFO="2d97fc13fdcd1132ed9a000bb3982451"'
	-D DEVICE_REGION=9
	-D INTERVAL_SAMPLE_S=600
	-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_ERROR
	-DLIB_DEBUG=0
build_src_filter = 
	+<../variants/rak4631/>
	+<main_lora.cpp>
	-<*>
lib_deps = 
	beegee-tokyo/SX126x-Arduino
	bblanchon/ArduinoJson@^7.1.0

But when I build with IS_LORA flag it error:

.pio/libdeps/rak4631_lorawan/SX126x-Arduino/src/boards/mcu/rak11300/SimpleTimer.cpp: In member function 'void SimpleTimer::run()':
.pio/libdeps/rak4631_lorawan/SX126x-Arduino/src/boards/mcu/rak11300/SimpleTimer.cpp:67:40: warning: comparison of integer expressions of different signedness: 'long unsigned int' and 'volatile long int' [-Wsign-compare]
   67 |    if (current_millis - prev_millis[i] >= delays[i])
      |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
.pio/libdeps/rak4631_lorawan/SX126x-Arduino/src/boards/mcu/rak11300/SimpleTimer.cpp: In member function 'bool SimpleTimer::check()':
.pio/libdeps/rak4631_lorawan/SX126x-Arduino/src/boards/mcu/rak11300/SimpleTimer.cpp:139:42: warning: comparison of integer expressions of different signedness: 'long unsigned int' and 'volatile long int' [-Wsign-compare]
  139 |    if ((current_millis - prev_millis[i]) >= delays[i])
.pio/libdeps/rak4631_lorawan/SX126x-Arduino/src/mac/LoRaMac.cpp: In function 'LoRaMacStatus_t LoRaMacMcpsRequest(McpsReq_t*)':
.pio/libdeps/rak4631_lorawan/SX126x-Arduino/src/mac/LoRaMac.cpp:3470:19: warning: 'datarate' may be used uninitialized in this function [-Wmaybe-uninitialized]
 3470 |  datarate = T_MAX((uint8_t)datarate, phyParam.Value);

main error:

Compiling .pio/build/rak4631_lorawan/FrameworkArduino/wiring_private.c.o
Compiling .pio/build/rak4631_lorawan/FrameworkArduino/wiring_shift.c.o
Archiving .pio/build/rak4631_lorawan/libFrameworkArduino.a
Indexing .pio/build/rak4631_lorawan/libFrameworkArduino.a
Linking .pio/build/rak4631_lorawan/firmware.elf
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libFrameworkArduino.a(main.cpp.o): in function `loop_task(void*)':
/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/cores/nRF5/main.cpp:60: undefined reference to `setup'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: /Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/cores/nRF5/main.cpp:69: undefined reference to `loop'
collect2: error: ld returned 1 exit status
*** [.pio/build/rak4631_lorawan/firmware.elf] Error 1

Can you help me check the workflow? And give me solution if you have. Have a nice day. Thanks !

There is no setup() and no loop() function in your current code.

This is either due to the conditional compilation:

or due to the exclude of source files:

Without seeing the complete code, it is difficult to say what is making it unnecessarily difficult to answer your questions. Next time, please show the complete platformio.ini and all necessary code parts. Or share a link to your complete project. (Thank you in advance).

1.The conditions looking a bit weird.

“If the macro IS_LORA is not defined, define the code for lora” ?!
“If the macro IS_MESH is not defined, define the code for mesh” ?!
Do you want this to be a conditional compilation or an include guard?

2. Wrong order

The meaning of the lines above:
+<../variants/rak4631/>: “Include all files in ../variants/rak4631/ into the build”
+<main_lora.cpp>: “Include the file main_lora.cpp into the build”
-<*>: "Forget what I said before, remove all files from the list of files to build

The last line contradicts the lines above it!

I’m building source code in src/lorawan with environment “rak4630_lorawan” in platformIO.
Can you help me check why I’m building source code with error:

Archiving .pio/build/rak4631_lorawan/libFrameworkArduino.a
Indexing .pio/build/rak4631_lorawan/libFrameworkArduino.a
Linking .pio/build/rak4631_lorawan/firmware.elf
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libc77/libAdafruit_LittleFS.a(lfs.c.o): in function `lfs_cache_read':
/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:18: undefined reference to `lfs_assert'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libc77/libAdafruit_LittleFS.a(lfs.c.o): in function `lfs_dir_fetch':
/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:493: undefined reference to `logLegacy'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libc77/libAdafruit_LittleFS.a(lfs.c.o): in function `lfs_cache_prog':
/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:156: undefined reference to `lfs_assert'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: /Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:183: undefined reference to `lfs_assert'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libc77/libAdafruit_LittleFS.a(lfs.c.o): in function `lfs_init':
/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:2068: undefined reference to `lfs_assert'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: /Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:2069: undefined reference to `lfs_assert'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: /Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:2080: undefined reference to `lfs_assert'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libc77/libAdafruit_LittleFS.a(lfs.c.o):/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:2081: more undefined references to `lfs_assert' follow
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libc77/libAdafruit_LittleFS.a(lfs.c.o): in function `lfs_mount':
/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:2233: undefined reference to `logLegacy'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: /Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:2224: undefined reference to `logLegacy'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libc77/libAdafruit_LittleFS.a(lfs.c.o): in function `lfs_alloc':
/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:313: undefined reference to `logLegacy'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libc77/libAdafruit_LittleFS.a(lfs.c.o): in function `lfs_file_relocate':
/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:1414: undefined reference to `logLegacy'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libc77/libAdafruit_LittleFS.a(lfs.c.o): in function `lfs_ctz_extend':
/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:1164: undefined reference to `lfs_assert'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: /Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:1244: undefined reference to `logLegacy'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: /Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:1235: undefined reference to `lfs_assert'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libc77/libAdafruit_LittleFS.a(lfs.c.o): in function `lfs_deorphan':
/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:2510: undefined reference to `logLegacy'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: /Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:2561: undefined reference to `logLegacy'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: /Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:2568: undefined reference to `logLegacy'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: /Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:2526: undefined reference to `logLegacy'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libc77/libAdafruit_LittleFS.a(lfs.c.o): in function `lfs_relocate':
/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:2449: undefined reference to `logLegacy'
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libc77/libAdafruit_LittleFS.a(lfs.c.o):/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:617: more undefined references to `logLegacy' follow
/Users/huydf/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/rak4631_lorawan/libc77/libAdafruit_LittleFS.a(lfs.c.o): in function `lfs_file_sync':
/Users/huydf/.platformio/packages/framework-arduinoadafruitnrf52/libraries/Adafruit_LittleFS/src/littlefs/lfs.c:1555: undefined reference to `lfs_assert'
collect2: error: ld returned 1 exit status
*** [.pio/build/rak4631_lorawan/firmware.elf] Error 1

I removed the solution using the “IS_LORA” flag
Thanks Sir !

I solved the problem. Thanks everyone!
Have a nice day @sivar2311 @maxgerhardt