Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: std::string causes link error

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    15

    std::string causes link error

    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\\teen sy3/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\\a rduino_cache_395686\\core\\core_teensy_avr_teensy3 1_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...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 by jwhoag; 10-04-2021 at 09:00 AM. Reason: Added info about std::string::assign

  2. #2
    Member
    Join Date
    Aug 2018
    Location
    Brisbane, Australia
    Posts
    85

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Hi jwhoag,

    here is a solution to the above code...

    Click image for larger version. 

Name:	Arduino std string compiled ok.jpg 
Views:	23 
Size:	60.5 KB 
ID:	26070

    go to the tools menu and select optimise "smallest code" and compile, as you can see from the image it compiles ok

    best regards

    Simon.M

  4. #4
    Junior Member
    Join Date
    Jun 2014
    Posts
    15
    Ah interesting. The "smallest code" option causes it to work for me too.

    I'd make a wild guess that maybe there's some debug code somewhere in the libraries that relies on _write_r (like printing some intermediate state), and it's not completely removed when the rest of the debug code is optimized out.

    Thank you for the quick and accurate response Simon!

  5. #5
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    hi jwhoag,

    your very welcome, it filled some time in my day and stopped me getting bored!

    I did some digging around and looked at the build commands for each of the options, on the build commands used it's the -O option that seems to be the problem. I believe the -O is for optimisations. The smallest code uses -Os and the other options use -O1, -O2, -O3 and -Og for debug, all these fail on a teensy 3.x or teensy LC.

    I see the "smallest code" as a set and forget option, the added bonus is it's the smallest code and you can squeeze more code in.

    best regards

    Simon.M

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •