Author Topic: HardwareSerial exists but Serial not defined...  (Read 11627 times)

Offline mogplus88

  • Multiple posting newcomer
  • *
  • Posts: 11
HardwareSerial exists but Serial not defined...
« on: November 17, 2017, 01:31:19 pm »
Hi, I am using CodeBlocks with the AVR template. I want to add Serial.print to my program, so I include HardwareSerial.h. CB finds the include library, but then flags an error on the Serial.begin(9600); statement saying "Undefined reference to Serial". It then flags another error saying "undefined reference to `HardwareSerial::begin(unsigned long, unsigned char)'". So it finds the #include library (and I can display it by right-clicking and selecting "open include library"). So why is it telling me Serial.begin is not valid? Apologies if this is a really dumb question, but I'm really stuck on this.
 Here's the code.
Code
#include </usr/lib/avr/include/inttypes.h> // otherwise wrong inttypes.h included
#include <HardwareSerial.h>
#include <Arduino.h>
#include <avr/io.h>
#include <stdio.h>

int main(void) {
Serial.begin(9600) ;
return 0;
}
I'm using Linux Mint 18.2 and CB version 16.01.
All advice gratefully accepted.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7679
    • My Best Post
Re: HardwareSerial exists but Serial not defined...
« Reply #1 on: November 17, 2017, 03:45:21 pm »
http://wiki.codeblocks.org/index.php/FAQ-Compiling_(errors)#Q:_How_do_I_troubleshoot_a_compiler_problem.3F

Post the full re-build log.

Edit: Your words imply that you have failed to link to the library that supports hardware serial.
But, till you post a build log the problem could be something else.

NOTE: A header file is not a library!

Tim S.
« Last Edit: November 18, 2017, 02:18:03 am 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 oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: HardwareSerial exists but Serial not defined...
« Reply #2 on: November 18, 2017, 08:17:31 pm »
stahta01: Doing generic posts is good, but it is not optimal, because you could sometimes confuse people. As is the case here.

mogplus88: The problem here is that you have to add all source files from the arduino distribution you're using. They've changed between 1.0.x and 1.5.x. And I think the template works only for 1.0.x arduino releases. You have to adjust the paths and files which are included. If you look around the generated project you'll see that there are a bunch file which include a file with the same name. The project also points to the directories inside the arduino distribution. So you need to find the files you need and you need to include them. You need to include source files, not headers. And this is a bit more complicated.

You could also check my improved arduino template at https://github.com/obfuscated/cb_arduino_template I've updated it to work with arduino 1.5 for both uno and micro arduinos. I've not tested the other models, because I don't own them.
(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!]

Offline mogplus88

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: HardwareSerial exists but Serial not defined...
« Reply #3 on: November 19, 2017, 06:42:02 am »
Thank you for the feedback gentlemen, I can see I have a lot more work to do!

In the meantime, here is the build log.
Quote
-------------- Build: Debug in AVRtest (compiler: GNU GCC Compiler for AVR)---------------

avr-gcc -Wall -mmcu=atmega328 -DF_CPU=16000000UL -g --verbose -I/usr/share/arduino-1.8.3/hardware/arduino/avr/cores/arduino -I/usr/share/arduino-1.8.3/hardware/arduino/avr/variants/standard -I/usr/lib/avr/include/avr -I/usr/include -c main.cpp -o obj/Debug/main.o
Using built-in specs.
Reading specs from /usr/lib/gcc/avr/4.9.2/device-specs/specs-atmega328
COLLECT_GCC=avr-gcc
Target: avr
Configured with: ../src/configure -v --enable-languages=c,c++ --prefix=/usr/lib --infodir=/usr/share/info --mandir=/usr/share/man --bindir=/usr/bin --libexecdir=/usr/lib --libdir=/usr/lib --enable-shared --with-system-zlib --enable-long-long --enable-nls --without-included-gettext --disable-libssp --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=avr CFLAGS='-g -O2 -fstack-protector-strong -Wformat ' CPPFLAGS=-D_FORTIFY_SOURCE=2 CXXFLAGS='-g -O2 -fstack-protector-strong -Wformat ' FCFLAGS='-g -O2 -fstack-protector-strong' FFLAGS='-g -O2 -fstack-protector-strong' GCJFLAGS='-g -O2 -fstack-protector-strong' LDFLAGS='-Wl,-Bsymbolic-functions -Wl,-z,relro' OBJCFLAGS='-g -O2 -fstack-protector-strong -Wformat ' OBJCXXFLAGS='-g -O2 -fstack-protector-strong -Wformat '
Thread model: single
gcc version 4.9.2 (GCC)
COLLECT_GCC_OPTIONS='-Wall'  '-D' 'F_CPU=16000000UL' '-g' '-v' '-I' '/usr/share/arduino-1.8.3/hardware/arduino/avr/cores/arduino' '-I' '/usr/share/arduino-1.8.3/hardware/arduino/avr/variants/standard' '-I' '/usr/lib/avr/include/avr' '-I' '/usr/include' '-c' '-o' 'obj/Debug/main.o' '-specs=device-specs/specs-atmega328' '-mmcu=avr5'
 /usr/lib/gcc/avr/4.9.2/cc1plus -quiet -v -I /usr/share/arduino-1.8.3/hardware/arduino/avr/cores/arduino -I /usr/share/arduino-1.8.3/hardware/arduino/avr/variants/standard -I /usr/lib/avr/include/avr -I /usr/include -imultilib avr5 -D__AVR_ATmega328__ -D__AVR_DEVICE_NAME__=atmega328 -D F_CPU=16000000UL main.cpp -mn-flash=1 -mno-skip-bug -quiet -dumpbase main.cpp -mmcu=avr5 -auxbase-strip obj/Debug/main.o -g -Wall -version -mn-flash=1 -mno-skip-bug -fno-rtti -fno-enforce-eh-specs -fno-exceptions -o /tmp/ccbbyh7q.s
GNU C++ (GCC) version 4.9.2 (avr)
   compiled by GNU C version 5.3.1 20151206, GMP version 6.1.0, MPFR version 3.1.3, MPC version 1.0.3
warning: MPFR header version 3.1.3 differs from library version 3.1.4.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/lib/gcc/avr/4.9.2/../../../avr/include/c++/4.9.2"
ignoring nonexistent directory "/usr/lib/gcc/avr/4.9.2/../../../avr/include/c++/4.9.2/avr/avr5"
ignoring nonexistent directory "/usr/lib/gcc/avr/4.9.2/../../../avr/include/c++/4.9.2/backward"
ignoring nonexistent directory "/usr/lib/gcc/avr/4.9.2/../../../avr/sys-include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/share/arduino-1.8.3/hardware/arduino/avr/cores/arduino
 /usr/share/arduino-1.8.3/hardware/arduino/avr/variants/standard
 /usr/lib/avr/include/avr
 /usr/include
 /usr/lib/gcc/avr/4.9.2/include
 /usr/lib/gcc/avr/4.9.2/include-fixed
 /usr/lib/gcc/avr/4.9.2/../../../avr/include
End of search list.
GNU C++ (GCC) version 4.9.2 (avr)
   compiled by GNU C version 5.3.1 20151206, GMP version 6.1.0, MPFR version 3.1.3, MPC version 1.0.3
warning: MPFR header version 3.1.3 differs from library version 3.1.4.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 102377f9992a8cb1e0090a3a50e1004a
COLLECT_GCC_OPTIONS='-Wall'  '-D' 'F_CPU=16000000UL' '-g' '-v' '-I' '/usr/share/arduino-1.8.3/hardware/arduino/avr/cores/arduino' '-I' '/usr/share/arduino-1.8.3/hardware/arduino/avr/variants/standard' '-I' '/usr/lib/avr/include/avr' '-I' '/usr/include' '-c' '-o' 'obj/Debug/main.o' '-specs=device-specs/specs-atmega328' '-mmcu=avr5'
 /usr/lib/gcc/avr/4.9.2/../../../avr/bin/as -mmcu=avr5 -mno-skip-bug -o obj/Debug/main.o /tmp/ccbbyh7q.s
COMPILER_PATH=/usr/lib/gcc/avr/4.9.2/:/usr/lib/gcc/avr/4.9.2/:/usr/lib/gcc/avr/:/usr/lib/gcc/avr/4.9.2/:/usr/lib/gcc/avr/:/usr/lib/gcc/avr/4.9.2/../../../avr/bin/
LIBRARY_PATH=/usr/lib/gcc/avr/4.9.2/avr5/:/usr/lib/gcc/avr/4.9.2/../../../avr/lib/avr5/:/usr/lib/gcc/avr/4.9.2/:/usr/lib/gcc/avr/4.9.2/../../../avr/lib/
COLLECT_GCC_OPTIONS='-Wall'  '-D' 'F_CPU=16000000UL' '-g' '-v' '-I' '/usr/share/arduino-1.8.3/hardware/arduino/avr/cores/arduino' '-I' '/usr/share/arduino-1.8.3/hardware/arduino/avr/variants/standard' '-I' '/usr/lib/avr/include/avr' '-I' '/usr/include' '-c' '-o' 'obj/Debug/main.o' '-specs=device-specs/specs-atmega328' '-mmcu=avr5'
avr-g++ -L/usr/lib -o bin/Debug/AVRtest.elf obj/Debug/fuse.o obj/Debug/main.o  -mmcu=atmega328 -Wl,-Map=bin/Debug/AVRtest.map,--cref 
obj/Debug/main.o: In function `main':
/home/ian/CodeBlocks/AVRtest/main.cpp:11: undefined reference to `Serial'
/home/ian/CodeBlocks/AVRtest/main.cpp:11: undefined reference to `Serial'
/home/ian/CodeBlocks/AVRtest/main.cpp:15: undefined reference to `HardwareSerial::begin(unsigned long, unsigned char)'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
4 error(s), 2 warning(s) (0 minute(s), 0 second(s))
 

Offline mogplus88

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: HardwareSerial exists but Serial not defined...
« Reply #4 on: November 30, 2017, 09:01:38 am »
Well, i've gotten around the problem, but I'm not entirely sure how. All I did was rename my main program file from main.c to main.cpp. That seemed to do it. Apparently the C compiler (?) wasn't  happy about including a .cpp file into a .c program. Kinda makes sense I suppose. But now I have a raft of other problems trying to understand how #include works...