I'm attempting to use the std::string class, but I'm getting a linker error (writer.c
.text._write_r+0x12): undefined reference to `_write'). I'm pretty sure this exact example worked in some previous Arduino/Teensyduino environment, but it's been quite a long time since I worked on my Teensy project and I've updated my installations since then. I'm using Ardiuno 1.8.16 and and Teensyduino 1.55. Teensy 3.1 is being targeted, although for kicks I also tried building for a 3.0 and 3.6 and got the same result. I also uninstalled Arduino/Teensy and re-installed. No luck.
----------------------------------------------------------------
Here's the full minimized source code sketch that exhibits the issue:
Filename: Mini.ino
----------------------------------------------------------------
#include <arduino.h>
#include <string>
void test(std::string& stdstr, const char* charstr)
{
stdstr = charstr;
}
void setup()
{
std::string output;
test(output, "hello");
}
void loop()
{
}
----------------------------------------------------------------
Here's the error that is reported:
----------------------------------------------------------------
Linking everything together...
"C:\\Program Files (x86)\\Arduino\\hardware\\teensy/../tools/arm/bin/arm-none-eabi-gcc" -O2 -Wl,--gc-sections,--relax,--defsym=__rtc_localtime=1633307146 "-TC:\\Program Files (x86)\\Arduino\\hardware\\teensy\\avr\\cores\\teensy3/mk20dx256.ld" -mthumb -mcpu=cortex-m4 -fsingle-precision-constant -o "c:\\proj\\Teensy31\\Builds/Mini.ino.elf" "c:\\proj\\Teensy31\\Builds\\sketch\\Mini.ino.cpp.o" "c:\\proj\\Teensy31\\Builds/..\\..\\..\\Users\\jwhoag\\AppData\\Local\\Temp\\arduino_cache_395686\\core\\core_teensy_avr_teensy31_usb_serial,speed_96,opt_o2std,keys_en-us_4939f5412a75a8560acb0f9e98268f02.a" "-Lc:\\proj\\Teensy31\\Builds" -larm_cortexM4l_math -lm -lstdc++
c:/program files (x86)/arduino/hardware/tools/arm/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/lib/armv7e-m\libc.a(lib_a-writer.o): In function `_write_r':
writer.c
.text._write_r+0x12): undefined reference to `_write'
collect2.exe: error: ld returned 1 exit status
Error compiling for board Teensy 3.2 / 3.1.
----------------------------------------------------------------
-Hardware: Teensy 3.1
-Wiring: not applicable to this question
-Software: nothing nonstandard. I only include <arduino.h> and <string>
----------------------------------------------------------------
I searched the entire arduino directory structure and there's no file called writer.c, or any function called _write_r anywhere. I suspect this issue may be related to another post: https://forum.pjrc.com/threads/68342-std-deque-Won-t-Compile-for-Teensy-3-x.
What's also weird is that the following lines of code (which should essentially do the same thing) both link okay:
std::string stdstring("hello);
std::string stdstring = "hello";
I also changed the function test to use a pointer instead of a reference, and that still produces the same problem
i.e. --> void test(std::string* stdstr, const char* charstr)
I also changed the function test to use std::string::assign, and that still produces the same problem
i.e. --> stdstr.assign(charstr); instead of stdstr = charstr;
Any ideas? Thanks,
--Jeff
----------------------------------------------------------------
Here's the full minimized source code sketch that exhibits the issue:
Filename: Mini.ino
----------------------------------------------------------------
#include <arduino.h>
#include <string>
void test(std::string& stdstr, const char* charstr)
{
stdstr = charstr;
}
void setup()
{
std::string output;
test(output, "hello");
}
void loop()
{
}
----------------------------------------------------------------
Here's the error that is reported:
----------------------------------------------------------------
Linking everything together...
"C:\\Program Files (x86)\\Arduino\\hardware\\teensy/../tools/arm/bin/arm-none-eabi-gcc" -O2 -Wl,--gc-sections,--relax,--defsym=__rtc_localtime=1633307146 "-TC:\\Program Files (x86)\\Arduino\\hardware\\teensy\\avr\\cores\\teensy3/mk20dx256.ld" -mthumb -mcpu=cortex-m4 -fsingle-precision-constant -o "c:\\proj\\Teensy31\\Builds/Mini.ino.elf" "c:\\proj\\Teensy31\\Builds\\sketch\\Mini.ino.cpp.o" "c:\\proj\\Teensy31\\Builds/..\\..\\..\\Users\\jwhoag\\AppData\\Local\\Temp\\arduino_cache_395686\\core\\core_teensy_avr_teensy31_usb_serial,speed_96,opt_o2std,keys_en-us_4939f5412a75a8560acb0f9e98268f02.a" "-Lc:\\proj\\Teensy31\\Builds" -larm_cortexM4l_math -lm -lstdc++
c:/program files (x86)/arduino/hardware/tools/arm/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/lib/armv7e-m\libc.a(lib_a-writer.o): In function `_write_r':
writer.c
collect2.exe: error: ld returned 1 exit status
Error compiling for board Teensy 3.2 / 3.1.
----------------------------------------------------------------
-Hardware: Teensy 3.1
-Wiring: not applicable to this question
-Software: nothing nonstandard. I only include <arduino.h> and <string>
----------------------------------------------------------------
I searched the entire arduino directory structure and there's no file called writer.c, or any function called _write_r anywhere. I suspect this issue may be related to another post: https://forum.pjrc.com/threads/68342-std-deque-Won-t-Compile-for-Teensy-3-x.
What's also weird is that the following lines of code (which should essentially do the same thing) both link okay:
std::string stdstring("hello);
std::string stdstring = "hello";
I also changed the function test to use a pointer instead of a reference, and that still produces the same problem
i.e. --> void test(std::string* stdstr, const char* charstr)
I also changed the function test to use std::string::assign, and that still produces the same problem
i.e. --> stdstr.assign(charstr); instead of stdstr = charstr;
Any ideas? Thanks,
--Jeff
Last edited: