Author Topic: Undefined reference to main - first arduino project in code::blocks  (Read 16289 times)

Offline Windy

  • Single posting newcomer
  • *
  • Posts: 3
Hi there,

I got sick of the arduino IDE, so I decided to switch to codeblocks. I installe codeblocks 13.12 and the arduino plugin form scottdky on my ubuntu 14.04 machine. I quiet liked the idea of different compiler settings for debugging and finaly building a release and upload it. If I use my "debugging" settings, the code compiles allright. Unfortunately it failes if I use my "compile and upload" settings.

To test the settings I try to compile the example below. I set the project up using scottdky's arduino project wizard, although I changed several settings and search directories...
Code
#include <Arduino.h>
/*
  Turns on an LED on for one second, then off for one second, repeatedly.
  This example code is in the public domain.
*/

void setup()
{
// initialize the digital pin as an output.
pinMode(5, OUTPUT);
}

void loop()
{
digitalWrite(5, HIGH);   // set the LED on
delay(1000);              // wait for a second
digitalWrite(5, LOW);    // set the LED off
delay(1000);              // wait for a second
}

Here the output if i use the debugging settings:
Code
-------------- Build: Simulator - Debug in Test (compiler: GNU GCC Compiler for AVR)---------------
Scanned 0 files for #includes, cache used 0, cache updated 0
avr-g++ -Wall -fno-exceptions -ffunction-sections -fdata-sections -x c++ -s -DARDUINO=101 -DUSE_EEPROM=0 -DUSE_ETHERNET=0 -DUSE_FIRMATA=0 -DUSE_LCD=0 -DUSE_LCD4884=0 -DUSE_OBD=0 -DUSE_SD=0 -DUSE_SERVO=0 -DUSE_SOFTSERIAL=0 -DUSE_SPI=0 -DUSE_STEPPER=0 -DUSE_TINYGPS=0 -DUSE_WIRE=0 -g -DARDUSIM -DENABLE_API_NAME -D__AVR_ATmega2560__ -DF_CPU=16000000L -I../libraries/ArduinoHardware/arduino/avr/cores/arduino -I../libraries/ArduinoHardware/arduino/avr/variants/mega -I../libraries/ArduinoHardware/arduino/avr/libraries/Wire -c sketch.cpp -o obj/Debug/sketch.o
In file included from ../libraries/ArduinoHardware/arduino/avr/cores/arduino/USBAPI.h:27:0,
                 from ../libraries/ArduinoHardware/arduino/avr/cores/arduino/Arduino.h:226,
                 from sketch.cpp:1:
/usr/lib/avr/include/util/delay.h:95:3: warning: #warning "Compiler optimizations disabled; functions from <util/delay.h> won't work as designed" [-Wcpp]
 # warning "Compiler optimizations disabled; functions from <util/delay.h> won't work as designed"
   ^
avr-g++  -o Test obj/Debug/sketch.o  -Wl,--gc-sections 
Output file is Test with size 17,37 KB
CWD for depslib was: /media/Data/Arduino_Workspace/Test.
CWD for depslib is: /media/Data/Arduino_Workspace/Test.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /media/STICK/Daten/Hubschrauber/Eigenbau/Arduino_Workspace/Test.
CWD for depslib is: /media/Data/Arduino_Workspace/Test.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /media/Data/Arduino_Workspace/Test.
CWD for depslib is: /media/Data/Arduino_Workspace/Test.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /media/Data/Eigenbau/Arduino_Workspace/Test.
CWD for depslib is: /media/Data/Arduino_Workspace/Test.
Scanned 0 files for #includes, cache used 0, cache updated 0
CWD for depslib was: /media/Data/Arduino_Workspace/Test.
CWD for depslib is: /media/Data/Arduino_Workspace/Test.
Scanned 0 files for #includes, cache used 0, cache updated 0
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 1 warning(s) (0 minute(s), 0 second(s))

And here the output of my attempt using the "compile and upload" settings:
Code
-------------- Build: Arduino Mega 2560/ADK in Test (compiler: GNU GCC Compiler for AVR)---------------
Scanned 0 files for #includes, cache used 0, cache updated 0
avr-g++ -Wall -fno-exceptions -ffunction-sections -fdata-sections -x c++ -s -DARDUINO=101 -DUSE_EEPROM=0 -DUSE_ETHERNET=0 -DUSE_FIRMATA=0 -DUSE_LCD=0 -DUSE_LCD4884=0 -DUSE_OBD=0 -DUSE_SD=0 -DUSE_SERVO=0 -DUSE_SOFTSERIAL=0 -DUSE_SPI=0 -DUSE_STEPPER=0 -DUSE_TINYGPS=0 -DUSE_WIRE=0 -Os -mmcu=atmega2560 -D__AVR_ATmega2560__ -DF_CPU=16000000L -DCODE_BLOCKS -I../libraries/ArduinoHardware/arduino/avr/cores/arduino -I../libraries/ArduinoHardware/arduino/avr/variants/mega -I../libraries/ArduinoHardware/arduino/avr/libraries/Wire -c sketch.cpp -o .objs/sketch.o
avr-g++  -o bin/Release/Test.elf .objs/sketch.o  -Wl,--gc-sections -s -mmcu=atmega2560 
/usr/lib/gcc/avr/4.8.2/../../../avr/lib/avr6/crtm2560.o: In Funktion `__bad_interrupt':
../../../../crt1/gcrt1.S:195: Undefined reference to `main'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Here the folder structure of /media/Data/Arduino_Workspace/. i got rid of most irrelevant folders...
Code
.
├── libraries
│   ├── ArduinoHardware
│   │   ├── arduino
│   │   │   └── avr
│   │   │       ├── boards.txt
│   │   │       ├── bootloaders
│   │   │       ├── cores
│   │   │       │   └── arduino
│   │   │       │       ├── abi.cpp
│   │   │       │       ├── Arduino.h
│   │   │       │       ├── binary.h
│   │   │       │       ├── CDC.cpp
│   │   │       │       ├── Client.h
│   │   │       │       ├── HardwareSerial0.cpp
│   │   │       │       ├── HardwareSerial1.cpp
│   │   │       │       ├── HardwareSerial2.cpp
│   │   │       │       ├── HardwareSerial3.cpp
│   │   │       │       ├── HardwareSerial.cpp
│   │   │       │       ├── HardwareSerial.h
│   │   │       │       ├── HardwareSerial_private.h
│   │   │       │       ├── HID.cpp
│   │   │       │       ├── hooks.c
│   │   │       │       ├── IPAddress.cpp
│   │   │       │       ├── IPAddress.h
│   │   │       │       ├── main.cpp
│   │   │       │       ├── new.cpp
│   │   │       │       ├── new.h
│   │   │       │       ├── Printable.h
│   │   │       │       ├── Print.cpp
│   │   │       │       ├── Print.h
│   │   │       │       ├── Server.h
│   │   │       │       ├── Stream.cpp
│   │   │       │       ├── Stream.h
│   │   │       │       ├── Tone.cpp
│   │   │       │       ├── Udp.h
│   │   │       │       ├── USBAPI.h
│   │   │       │       ├── USBCore.cpp
│   │   │       │       ├── USBCore.h
│   │   │       │       ├── USBDesc.h
│   │   │       │       ├── WCharacter.h
│   │   │       │       ├── WInterrupts.c
│   │   │       │       ├── wiring_analog.c
│   │   │       │       ├── wiring.c
│   │   │       │       ├── wiring_digital.c
│   │   │       │       ├── wiring_private.h
│   │   │       │       ├── wiring_pulse.c
│   │   │       │       ├── wiring_pulse.S
│   │   │       │       ├── wiring_shift.c
│   │   │       │       ├── WMath.cpp
│   │   │       │       ├── WString.cpp
│   │   │       │       └── WString.h
│   │   │       ├── firmwares
│   │   │       ├── libraries
│   │   │       │   ├── EEPROM
│   │   │       │   ├── SoftwareSerial
│   │   │       │   ├── SPI
│   │   │       │   └── Wire
│   │   │       ├── platform.txt
│   │   │       ├── programmers.txt
│   │   │       └── variants
│   │   │           ├── eightanaloginputs
│   │   │           ├── ethernet
│   │   │           ├── gemma
│   │   │           ├── leonardo
│   │   │           ├── mega
│   │   │           │   └── pins_arduino.h
│   │   │           ├── micro
│   │   │           ├── robot_control
│   │   │           ├── robot_motor
│   │   │           ├── standard
│   │   │           └── yun1
│   │   └── tools
└── Test
    ├── bin
    │   └── Release
    │       ├── Test.elf.eep.hex
    │       └── Test.elf.hex
    ├── cores
    │   ├── CDC.cpp
    │   ├── HardwareSerial.cpp
    │   ├── HID.cpp
    │   ├── IPAddress.cpp
    │   ├── main.cpp
    │   ├── new.cpp
    │   ├── Print.cpp
    │   ├── Stream.cpp
    │   ├── Tone.cpp
    │   ├── USBCore.cpp
    │   ├── WInterrupts.c
    │   ├── wiring_analog.c
    │   ├── wiring.c
    │   ├── wiring_digital.c
    │   ├── wiring_pulse.c
    │   ├── wiring_shift.c
    │   ├── WMath.cpp
    │   └── WString.cpp
    ├── libraries
    │   └── libraries.cpp
    ├── obj
    │   ├── Debug
    │   │   ├── cores
    │   │   │   ├── CDC.o
    │   │   │   ├── HardwareSerial.o
    │   │   │   ├── HID.o
    │   │   │   ├── IPAddress.o
    │   │   │   ├── main.o
    │   │   │   ├── new.o
    │   │   │   ├── Print.o
    │   │   │   ├── Stream.o
    │   │   │   ├── Tone.o
    │   │   │   ├── USBCore.o
    │   │   │   ├── WInterrupts.o
    │   │   │   ├── wiring_analog.o
    │   │   │   └── wiring.o
    │   │   ├── libraries
    │   │   │   └── ArduinoHardware
    │   │   │       └── arduino
    │   │   │           └── avr
    │   │   │               └── cores
    │   │   │                   └── arduino
    │   │   │                       └── main.o
    │   │   ├── sketch.o
    │   │   └── Test
    │   │       └── sketch.o
    │   └── Release
    │       ├── cores
    │       │   ├── CDC.o
    │       │   ├── HardwareSerial.o
    │       │   ├── HID.o
    │       │   ├── IPAddress.o
    │       │   ├── main.o
    │       │   ├── new.o
    │       │   ├── Print.o
    │       │   ├── Stream.o
    │       │   ├── Tone.o
    │       │   ├── USBCore.o
    │       │   ├── WInterrupts.o
    │       │   ├── wiring_analog.o
    │       │   └── wiring.o
    │       ├── libraries
    │       │   └── ArduinoHardware
    │       │       └── arduino
    │       │           └── avr
    │       │               └── cores
    │       │                   └── arduino
    │       │                       └── main.o
    │       ├── sketch.o
    │       └── Test
    │           └── sketch.o
    ├── sketch.cpp
    ├── Test
    ├── Test.cbp
    ├── Test.cbpbackup
    ├── Test.depend
    └── Test.layout

I compared the compiler options and search for ages but can't find the cause of the error. The function main is defined in ./libraries/ArduinoHardware/arduino/avr/cores/arduino/main.cpp. I tried to include the file itselfe (which might not make sense at all...), I declared main in a main.h and included the headerfile to the search path but with no success. I tried to definde main in my sketch.cpp befor defining setup() and loop () which didn't help either. Any ideas? What am I missing?

Thanks in advance!
Windy

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
Re: Undefined reference to main - first arduino project in code::blocks
« Reply #1 on: July 27, 2015, 04:53:08 pm »
Later today I will try and see if I can duplicate the problem under Debian 8.1 "Jessie"

I have no arduino hardware; so, I hope that is NOT needed to dup the problem.
Likely be using CB Nightly on first dup attempt.
Failed: To even get as far as your error using  CB Nightly.

The error itself looks like a linking error to me.

Tim S.
« Last Edit: July 27, 2015, 07:05:05 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Windy

  • Single posting newcomer
  • *
  • Posts: 3
Re: Undefined reference to main - first arduino project in code::blocks
« Reply #2 on: July 27, 2015, 05:26:17 pm »
You don't need any arduino hardware...
Jap, that's a linking error. The "ld" after "error:" indicates that.

Windy

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
Re: Undefined reference to main - first arduino project in code::blocks
« Reply #3 on: July 27, 2015, 07:06:53 pm »
Could you attach the cbp file you are using for me to test that directly?
I believe you need to zip or 7zip it to attach it.

Tim S.


C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Windy

  • Single posting newcomer
  • *
  • Posts: 3
Re: Undefined reference to main - first arduino project in code::blocks
« Reply #4 on: July 27, 2015, 07:15:44 pm »
cbp-File is attached.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
Re: Undefined reference to main - first arduino project in code::blocks
« Reply #5 on: July 27, 2015, 09:32:27 pm »
Found this value in "build options" "linker settings" "other linker options" for target "Arduino Mega 2560/ADK".

Code
-mmcu=$(MCU)

I suggest adding it to "Simulator - Debug" to see if it causes the error to happen on building "Simulator - Debug" target.
Edit: Likely better to add it like below.
Code
-mmcu=atmega2560


But, the first thing to change is to make the value of "ARDUINO_DIR" custom variable to be the same for both targets one of the has double quotes around it.

Tim S.
« Last Edit: July 27, 2015, 09:41:34 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
Re: Undefined reference to main - first arduino project in code::blocks
« Reply #6 on: July 27, 2015, 09:40:28 pm »
Make the compiler setting the same to verify one of those are not causing the difference.

Optimize generated code (for size)
Strip all symbols from binary (minimizes size)
Produce debugging symbols

I would suggest removing -mmcu=$(MCU) from Compiler other options for target "Arduino Mega 2560/ADK"
And instead add -mmcu=atmega2560 in Compiler other options for both targets

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Undefined reference to main - first arduino project in code::blocks
« Reply #7 on: July 27, 2015, 09:50:17 pm »
As far as I know this is the most up to date arduino wizard: https://github.com/obfuscated/cb_arduino_template
But I don't know if the simulator targets work at all, I've never tried them.

To debug you problems you can run the arduino ide and see what commands and options are executed.
Then you can try to replicate them in C::B.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]