Getting Error 1 when building [Solved]

Hey evreyone.
I think I need some help from You guys.
I’m doing a weather station project based on a Arduino Mega 2560 and a ethernet shield (v1).
I also added a LCD display controlled by a keypad.

The thing is when I’m Trying to build the project, I get the error.

collect2.exe: error: ld returned 1 exit status
*** [.pioenvs\megaatmega2560\firmware.elf] Error 1

I put all sources of my project on Github: https://github.com/Guisch/Station-Meteo
Heres all the log when building

Summary
[03/27/18 10:47:29] Processing megaatmega2560 (platform: atmelavr; lib_deps: Ethernet, EthernetUdp, SD, SPI, Arduino, Wire, Keypad, LiquidCrystal; board: megaatmega2560; framework: arduino)

Library Storage: C:\Users\ghcas\AppData\Roaming\SPB_16.6\Documents\PlatformIO\Projects\Station meteo\.piolibdeps
LibraryManager: Installing id=872
Ethernet @ 1.1.2 is already installed
Looking for EthernetUdp library in registry
Warning! Library `{'requirements': None, 'name': 'EthernetUdp'}` has not been found in PlatformIO Registry.
You can ignore this message, if `{'requirements': None, 'name': 'EthernetUdp'}` is a built-in library (included in framework, SDK). E.g., SPI, Wire, etc.
LibraryManager: Installing id=161
SD @ 1a24a9486d is already installed
Looking for SPI library in registry
Warning! Library `{'requirements': None, 'name': 'SPI'}` has not been found in PlatformIO Registry.
You can ignore this message, if `{'requirements': None, 'name': 'SPI'}` is a built-in library (included in framework, SDK). E.g., SPI, Wire, etc.
Looking for Arduino library in registry
arduino

=======
#ID: 2171
arduino library for mbed



Keywords: arduino

Compatible frameworks: mbed

Compatible platforms: Atmel SAM, Freescale Kinetis, Maxim Integrated MAX32, Nordic nRF51, Nordic nRF52, NXP LPC, Silicon Labs EFM32, ST STM32, Teensy, WIZNet W7500

Authors: SE HUI PARK



Arduino

=======
#ID: 2172
Library to allow the use of Arduino specific language with the mbed



Keywords: mbeduino, arduino

Compatible frameworks: mbed

Compatible platforms: Atmel SAM, Freescale Kinetis, Maxim Integrated MAX32, Nordic nRF51, Nordic nRF52, NXP LPC, Silicon Labs EFM32, ST STM32, Teensy, WIZNet W7500

Authors: Chris Dick



Arduino

=======
#ID: 3194
Arduino functions millis, micros and eventually some more to come



Keywords: micros, millis, arduino

Compatible frameworks: mbed

Compatible platforms: Atmel SAM, Freescale Kinetis, Maxim Integrated MAX32, Nordic nRF51, Nordic nRF52, NXP LPC, Silicon Labs EFM32, ST STM32, Teensy, WIZNet W7500

Authors: Eduardo de Mier



Found: platformio .org/lib/show/2171/arduino
LibraryManager: Installing id=2171
arduino @ 3b83fc30bb is already installed
Looking for Wire library in registry
Warning! Library `{'requirements': None, 'name': 'Wire'}` has not been found in PlatformIO Registry.
You can ignore this message, if `{'requirements': None, 'name': 'Wire'}` is a built-in library (included in framework, SDK). E.g., SPI, Wire, etc.
LibraryManager: Installing id=165
Keypad @ 3.1.1 is already installed
LibraryManager: Installing id=136
LiquidCrystal @ 1.3.4 is already installed
PLATFORM: Atmel AVR > Arduino Mega or Mega 2560 ATmega2560 (Mega 2560)
SYSTEM: ATMEGA2560 16MHz 8KB RAM (248KB Flash)
Library Dependency Finder -> bit .ly/configure-pio-ldf
LDF MODES: FINDER(chain) COMPATIBILITY(light)
Collected 29 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <Ethernet> v1.1.2 (C:\Users\ghcas\AppData\Roaming\SPB_16.6\Documents\PlatformIO\Projects\Station meteo\.piolibdeps\Ethernet_ID872)
|   |-- <SPI> v1.0 (C:\Users\ghcas\AppData\Roaming\SPB_16.6\.platformio\packages\framework-arduinoavr\libraries\__cores__\arduino\SPI)
|-- <SD> (C:\Users\ghcas\AppData\Roaming\SPB_16.6\Documents\PlatformIO\Projects\Station meteo\.piolibdeps\SD_ID161)
|   |-- <SPI> v1.0 (C:\Users\ghcas\AppData\Roaming\SPB_16.6\.platformio\packages\framework-arduinoavr\libraries\__cores__\arduino\SPI)
|-- <SPI> v1.0 (C:\Users\ghcas\AppData\Roaming\SPB_16.6\.platformio\packages\framework-arduinoavr\libraries\__cores__\arduino\SPI)
|-- <Wire> v1.0 (C:\Users\ghcas\AppData\Roaming\SPB_16.6\.platformio\packages\framework-arduinoavr\libraries\__cores__\arduino\Wire)
|-- <Keypad> v3.1.1 (C:\Users\ghcas\AppData\Roaming\SPB_16.6\Documents\PlatformIO\Projects\Station meteo\.piolibdeps\Keypad_ID165)
|-- <LiquidCrystal> v1.3.4 (C:\Users\ghcas\AppData\Roaming\SPB_16.6\Documents\PlatformIO\Projects\Station meteo\.piolibdeps\LiquidCrystal_ID136)
|   |-- <Wire> v1.0 (C:\Users\ghcas\AppData\Roaming\SPB_16.6\.platformio\packages\framework-arduinoavr\libraries\__cores__\arduino\Wire)
avr-g++ -o .pioenvs\megaatmega2560\firmware.elf -Os -mmcu=atmega2560 -Wl,--gc-sections -flto -fuse-linker-plugin .pioenvs\megaatmega2560\src\date.cpp.o .pioenvs\megaatmega2560\src\enregistrement.cpp.o .pioenvs\megaatmega2560\src\main.cpp.o .pioenvs\megaatmega2560\src\menu.cpp.o .pioenvs\megaatmega2560\src\serveur.cpp.o -L.pioenvs\megaatmega2560 -Wl,--start-group .pioenvs\megaatmega2560\lib5db\libSPI.a .pioenvs\megaatmega2560\liba0f\libEthernet_ID872.a .pioenvs\megaatmega2560\libe7d\libSD_ID161.a .pioenvs\megaatmega2560\lib854\libWire.a .pioenvs\megaatmega2560\lib381\libKeypad_ID165.a .pioenvs\megaatmega2560\libed3\libLiquidCrystal_ID136.a .pioenvs\megaatmega2560\libFrameworkArduinoVariant.a .pioenvs\megaatmega2560\libFrameworkArduino.a -lm -Wl,--end-group

Thanks !

Your problem is not with PIO but with your code. You are having a header file which instantiates VARIABLES in every CPP file it is included, thus duplicating variables like crazy.

Look at your enregistrement.h file:


#ifndef ENREGISTREMENT
#define ENREGISTREMENT

/* .. */ 

File fichierSD;

unsigned long position_fin_fichier;

float compteur;

float tempMinObs;
unsigned long tempMinTime;
float tempMaxObs;
unsigned long tempMaxTime;
float tempMoyObs;
float temperature
float tempSomme;

Now every time your #include "enregistrement.h that file (e.g. in main.cpp and in enregistrement.cpp), your file gets a copy of these variables.

What you most probably want is that these are global variables which only exist once. For that, your header file must declare each variable to be extern. You then have to define these variables once in one CPP file.

So, e.g. in enregistrement.h the line File fichierSD; becomes extern File fichierSD;, while in some .cpp file (say, e.g. enregistrement.cpp), you place File fichierSD; as a global variable on top of the file. Do that with all your variables until the linker doesn’t complain about duplicate definitions anymore.

2 Likes

Indeed ! That worked for me. Thanks a lot ! :slight_smile:

can you help me too? i have similar error to my code the error is:

.pio/build/teensy40/libFrameworkArduino.a(main.cpp.o): In function `main':
main.cpp:(.text.startup.main+0x2): undefined reference to `setup'
main.cpp:(.text.startup.main+0x6): undefined reference to `loop'
collect2: error: ld returned 1 exit status
*** [.pio/build/teensy40/firmware.elf] Error 1

this is my source for my project GitHub - gartina5/SGU-Linorobot2_hardware

this is the code that i already stuck like a week

Processing teensy40 (board: teensy40; framework: arduino; platform: teensy)
--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: 
PLATFORM: Teensy (4.17.0) > Teensy 4.0
HARDWARE: IMXRT1062 600MHz, 512KB RAM, 1.94MB Flash
DEBUG: Current (jlink) External (jlink)
PACKAGES: 
 - framework-arduinoteensy @ 1.157.220801 (1.57) 
 - tool-teensy @ 1.157.0 (1.57) 
 - toolchain-gccarmnoneeabi @ 1.50401.190816 (5.4.1)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Installing pyyaml with pip at PlatformIO environment
/usr/bin/python3 -m pip install pyyaml
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pyyaml in /usr/lib/python3/dist-packages (5.4.1)
Installing markupsafe==2.0.1 with pip at PlatformIO environment
/usr/bin/python3 -m pip install markupsafe==2.0.1
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: markupsafe==2.0.1 in /usr/lib/python3/dist-packages (2.0.1)
Configuring teensy40 with transport serial
micro-ROS already built
Found 99 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Linking .pio/build/teensy40/firmware.elf
.pio/build/teensy40/libFrameworkArduino.a(main.cpp.o): In function `main':
main.cpp:(.text.startup.main+0x2): undefined reference to `setup'
main.cpp:(.text.startup.main+0x6): undefined reference to `loop'
collect2: error: ld returned 1 exit status
*** [.pio/build/teensy40/firmware.elf] Error 1
========================== [FAILED] Took 2.73 seconds ==========================

Environment    Status    Duration
-------------  --------  ------------
teensy40       FAILED    00:00:02.726
==================== 1 failed, 0 succeeded in 00:00:02.726 ====================

So in which .cpp file in src/ do you define setup() and loop() functions?