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

Thread: string help

  1. #1

    string help

    I have a simple test program that uses two strings. But when I include the second string (world), the program fails to link. Can someone help me understand the issue? Thanks. Teensyduino, Version 1.39, Arduino 1.8.4.

    Code:
    #include <string>
    
    using namespace std;
    
    void setup() {
      // put your setup code here, to run once:
      Serial1.begin(115200);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      
      string hello("hello");
      Serial1.println(hello.c_str());
    
      string world("world");
      Serial1.println(world.c_str());
    
      delay(1000);
    }
    /Applications/Arduino.app/Contents/Java/arduino-builder -dump-prefs -logger=machine -hardware /Applications/Arduino.app/Contents/Java/hardware -tools /Applications/Arduino.app/Contents/Java/tools-builder -tools /Applications/Arduino.app/Contents/Java/hardware/tools/avr -built-in-libraries /Applications/Arduino.app/Contents/Java/libraries -libraries /Users/jli1/Documents/Arduino/libraries -fqbn=teensy:avr:teensy36:usb=serial,speed=180,opt= o2std,keys=en-us -ide-version=10804 -build-path /var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_build_273581 -warnings=all -build-cache /var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_cache_873730 -verbose /Users/jli1/Documents/Arduino/sketch_oct09a/sketch_oct09a.ino
    /Applications/Arduino.app/Contents/Java/arduino-builder -compile -logger=machine -hardware /Applications/Arduino.app/Contents/Java/hardware -tools /Applications/Arduino.app/Contents/Java/tools-builder -tools /Applications/Arduino.app/Contents/Java/hardware/tools/avr -built-in-libraries /Applications/Arduino.app/Contents/Java/libraries -libraries /Users/jli1/Documents/Arduino/libraries -fqbn=teensy:avr:teensy36:usb=serial,speed=180,opt= o2std,keys=en-us -ide-version=10804 -build-path /var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_build_273581 -warnings=all -build-cache /var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_cache_873730 -verbose /Users/jli1/Documents/Arduino/sketch_oct09a/sketch_oct09a.ino
    Using board 'teensy36' from platform in folder: /Applications/Arduino.app/Contents/Java/hardware/teensy/avr
    Using core 'teensy3' from platform in folder: /Applications/Arduino.app/Contents/Java/hardware/teensy/avr
    Detecting libraries used...
    "/Applications/Arduino.app/Contents/Java/hardware/teensy/../tools/arm/bin/arm-none-eabi-g++" -E -CC -x c++ -w -g -Wall -ffunction-sections -fdata-sections -nostdlib -fno-exceptions -felide-constructors -std=gnu++14 -fno-rtti -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsingle-precision-constant -D__MK66FX1M0__ -DTEENSYDUINO=139 -DARDUINO=10804 -DF_CPU=180000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH "-I/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3" "/var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_build_273581/sketch/sketch_oct09a.ino.cpp" -o "/dev/null"
    Generating function prototypes...
    "/Applications/Arduino.app/Contents/Java/hardware/teensy/../tools/arm/bin/arm-none-eabi-g++" -E -CC -x c++ -w -g -Wall -ffunction-sections -fdata-sections -nostdlib -fno-exceptions -felide-constructors -std=gnu++14 -fno-rtti -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsingle-precision-constant -D__MK66FX1M0__ -DTEENSYDUINO=139 -DARDUINO=10804 -DF_CPU=180000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH "-I/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3" "/var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_build_273581/sketch/sketch_oct09a.ino.cpp" -o "/var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_build_273581/preproc/ctags_target_for_gcc_minus_e.cpp"
    "/Applications/Arduino.app/Contents/Java/tools-builder/ctags/5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "/var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_build_273581/preproc/ctags_target_for_gcc_minus_e.cpp"
    Compiling sketch...
    "/Applications/Arduino.app/Contents/Java/hardware/teensy/../tools/arm/bin/arm-none-eabi-g++" -c -O2 -g -Wall -ffunction-sections -fdata-sections -nostdlib -MMD -fno-exceptions -felide-constructors -std=gnu++14 -fno-rtti -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsingle-precision-constant -D__MK66FX1M0__ -DTEENSYDUINO=139 -DARDUINO=10804 -DF_CPU=180000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH "-I/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3" "/var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_build_273581/sketch/sketch_oct09a.ino.cpp" -o "/var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_build_273581/sketch/sketch_oct09a.ino.cpp.o"
    Compiling libraries...
    Compiling core...
    Using precompiled core
    Linking everything together...
    "/Applications/Arduino.app/Contents/Java/hardware/teensy/../tools/arm/bin/arm-none-eabi-gcc" -O2 -Wl,--gc-sections,--relax,--defsym=__rtc_localtime=1507558569 "-T/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/mk66fx1m0.ld" -lstdc++ -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsingle-precision-constant -o "/var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_build_273581/sketch_oct09a.ino.elf" "/var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_build_273581/sketch/sketch_oct09a.ino.cpp.o" "/var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_build_273581/../arduino_cache_873730/core/core_teensy_avr_teensy36_usb_serial,speed_180,opt_ o2std,keys_en-us_cee3a1d70ca5f7f18ab44a012a95ee10.a" "-L/var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_build_273581" -larm_cortexM4lf_math -lm
    /var/folders/q4/djp0qrzs55x04tt5rt_qhpsw0000gp/T/arduino_build_273581/sketch/sketch_oct09a.ino.cpp.o: In function `void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char const*>(char const*, char const*, std::forward_iterator_tag)':
    /Applications/Arduino.app/Contents/Java/hardware/tools/arm/arm-none-eabi/include/c++/5.4.1/bits/basic_string.tcc:219: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_create(unsigned int&, unsigned int)'
    /Applications/Arduino.app/Contents/Java/hardware/tools/arm/arm-none-eabi/include/c++/5.4.1/bits/basic_string.tcc:212: undefined reference to `std::__throw_logic_error(char const*)'

    collect2: error: ld returned 1 exit status
    Error compiling for board Teensy 3.6.

  2. #2
    Junior Member
    Join Date
    Oct 2017
    Location
    Nantes (FRANCE)
    Posts
    5
    Use String, not string.

  3. #3
    Can you explain? Am I not able to use std::string?

  4. #4
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,317
    Quote Originally Posted by Joe31093 View Post
    Can you explain? Am I not able to use std::string?
    have you tried "std::String"?

  5. #5
    "String" is not part of the namespace std. I was able to use std::string in 1.35, just trying to understand what I need to do with the latest update.

  6. #6
    Junior Member
    Join Date
    Oct 2017
    Location
    Nantes (FRANCE)
    Posts
    5
    Teensyduino, as Arduino, uses String class for representing string objects:
    https://www.arduino.cc/en/Reference/StringObject

    You can see the String class header in the String.h. I am working on Mac, this file is:
    /Applications/Arduino.app/Contents/Java/hardware/teensy/avr/cores/teensy/WString.h

    I think that stdlib C++ string is not supported any more.


    Try this:
    Code:
    void setup() {
      // put your setup code here, to run once:
      Serial1.begin(115200);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      
      String hello("hello");
      Serial1.println(hello);
    
      String world("world");
      Serial1.println(world);
    
      delay(1000);
    }

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,077
    As noted string doesn't exist from the code above - but String does as this example works without the commented items.

    Not sure what the heap effect is of the loop() local String's? Or what features might be excluded with the default String that is included?

    Code:
    //#include <string>
    //using namespace std;
    
    void setup() {
      Serial.begin(115200);
    }
    
    void loop() {
      String hello("hello ");
      Serial.print(hello.c_str());
      String world("world @");
      world += millis();
      Serial.println(world.c_str());
      delay(1000);
    }
    sample output:
    ...
    hello world @261408
    hello world @262409
    ...

  8. #8
    Junior Member
    Join Date
    Oct 2017
    Location
    Nantes (FRANCE)
    Posts
    5
    String allocates memory on the heap, and implicitly called destructor releases memory.

    You can get free memory size with this function:
    Code:
    // https://forum.pjrc.com/threads/23256-Get-Free-Memory-for-Teensy-3-0
    
    extern "C" char* sbrk(int incr);
    
    static size_t freeRAM (void) {
      int top; // For getting stack address
      return ((size_t) &top) - ((size_t) sbrk(0)) ;
    }
    And display the returned value with:
    Code:
    void loop() {
      ...
      Serial.print("Free Ram: ");
      Serial.println(freeRAM());
      ...
    }

  9. #9
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,077
    That was a somewhat rhetorical question - just wondering if those local's would behave well on the heap - and noting that heap usage can do odd things when it fragments.

    It seems they do behave okay and don't - as used - cause any fragmentation with loop() local 'repeat' creation.

    I added the FreeRam() code and making them global (on T_3.2) does result in them having more Free Ram to a small degree where Free is 57308 when global and 57276 when as shown below versus above as local vars.

    Code:
    String hello("hello ");
    String world("world @");
    
    void loop() {
      Serial.print(hello.c_str());
      world = "world ";
      world += millis();
      // ...

  10. #10
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,049
    Quote Originally Posted by Joe31093 View Post
    Can you explain? Am I not able to use std::string?
    To more directly answer your question, Teensy generally follows the Arduino conventions and aims for Arduino compatibility.

    Arduino has very limited support for the C++ standard library functions. You can expect most C++ std namespace stuff to not work.

  11. #11
    Thanks for the help everyone. Was there something specific that was changed to make std::string not work from 1.35 to 1.39?

Posting Permissions

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