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? Thanks all !

In the code

#ifdef IS_LORA

In platformio.ini

-DIS_LORA

Try in platformio.ini

-D IS_LORA=1
1 Like

Hi @beegee
Nice to mee you !
I built it as per your command but I still don’t see that error change

[env:rak4631_lorawan]
extends = nrf52840_base
board = wiscore_rak4631
framework = arduino
upload_protocol = jlink
build_flags = 
	${nrf52840_base.build_flags}
	-D IS_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
	-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/>
	+<main.cpp>
	-<main_mesh.cpp>
	-<*>
src_filter = +<main.cpp>
lib_deps = 
	beegee-tokyo/SX126x-Arduino
	bblanchon/ArduinoJson@^7.1.0

[nrf52_base]
platform = platformio/nordicnrf52@^10.7.0
extends = arduino_base
platform_packages = 
	platformio/framework-arduinoadafruitnrf52 @ https://github.com/meshtastic/Adafruit_nRF52_Arduino.git#e13f5820002a4fb2a5e6754b42ace185277e5adf
	platformio/toolchain-gccarmnoneeabi@~1.90301.0
build_type = debug
build_flags = 
	-include arch/nrf52/cpp_overrides/lfs_util.h
	${arduino_base.build_flags}
	-DSERIAL_BUFFER_SIZE=1024
	-Wno-unused-variable
	-Isrc/platform/nrf52
;	-DLFS_NO_ASSERT
	-DMESHTASTIC_EXCLUDE_AUDIO=1
	-DMESHTASTIC_EXCLUDE_PAXCOUNTER=1
	-DMAX_NUM_NODES=80
build_src_filter = 
	${arduino_base.build_src_filter} -<platform/esp32/> -<platform/stm32wl> -<nimble/> -<mesh/wifi/> -<mesh/api/> -<mesh/http/> -<modules/esp32> -<platform/rp2xx0> -<mesh/eth/> -<mesh/raspihttp>
lib_deps = 
	${arduino_base.lib_deps}
	${radiolib_base.lib_deps}
	rweather/Crypto@^0.4.0
lib_ignore = 
	BluetoothOTA
	lvgl

[nrf52840_base]
extends = nrf52_base
build_flags = ${nrf52_base.build_flags}
lib_deps = 
	${nrf52_base.lib_deps}
	${environmental_base.lib_deps}
	https://github.com/Kongduino/Adafruit_nRFCrypto.git#e31a8825ea3300b163a0a3c1ddd5de34e10e1371
debug_init_break = tbreak setup
debug_extra_cmds = 
	echo Running .gdbinit script
	commands 1
	set useSoftDevice = 0
	end
debug_load_mode = modified
debug_tool = stlink
debug_speed = 4000

Then remove the

#ifdef IS_LORA

from your code. I am guessing that it has to do with the define.

1 Like

Hi @beegee
I will try tomorrow. I will reply you as soon as possible. Thanks !

Hi @beegee

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. Thanks !

should already throw a compilation error. the correct syntax is #ifndef IS_LORA

I see only warnings in the compilation output, no errors:

A warning does not stop the compilation, your final error is still linked to the #defines.

Other than that, I can’t tell you why it doesn’t work.

Thanks Sir !
Actually a bug in #ifdef IS LORA and I changed the other solution to make it available
Thanks Sir for supporting me !

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.