This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C++ Standard Library - string class

Other Parts Discussed in Thread: CODECOMPOSER

Hi everyone,

Has anyone experienced problems with CodeComposer Studio's support for the C++ Standard Library. Specifically with the "string" class. I have a project based on C++ and until now have used my home-grown solution for the string class (I had my reasons). Now that I'm trying to be a good citizen by using standard software, I seem to be having problems. I boiled it down to a simple example shown below:

 

//*****************************************************************************

//

//!     @file test.cpp

//!     Main entry point

//!

//!     @date Created: 8/18/2011

// 

//*****************************************************************************


#include <string>


//*****************************************************************************

//

//!     @brief Simple program utilizing string class in the C++ Standard Library

//

//*****************************************************************************

int main(void)

{

    string strTest("Test");


    //

    // Loop forever

    //

    for (;;) {

        

    }

}

_____________________________________________________________________
The compiler responds with:
**** Build of configuration Debug for project Test ****
C:\Program Files\Texas Instruments\ccsv4\utils\gmake\gmake -k all 
'Building file: ../startup_ccs.c'
'Invoking: Compiler'
"C:/Program Files/Texas Instruments/ccsv4/tools/compiler/TMS470 Code Generation Tools 4.6.6/bin/cl470" -mv7M3 -g -O2 --gcc --define="ccs" --define="PART_LM3S9B92" --define="TARGET_IS_TEMPEST_RB1" --include_path="C:/Projects/AX8410Stellaris/Test" --include_path="C:/Program Files/StellarisWare" --include_path="C:/Program Files/Texas Instruments/ccsv4/tools/compiler/TMS470 Code Generation Tools 4.6.6/include" --include_path="C:/Projects/AX8410Stellaris" --diag_warning="225" -me --gen_func_subsections --abi=eabi --code_state=16 --ual --preproc_with_compile --preproc_dependency="startup_ccs.pp"  "../startup_ccs.c"
'Finished building: ../startup_ccs.c'
' '
'Building file: ../test.cpp'
'Invoking: Compiler'
"C:/Program Files/Texas Instruments/ccsv4/tools/compiler/TMS470 Code Generation Tools 4.6.6/bin/cl470" -mv7M3 -g -O2 --gcc --define=ccs --define=PART_LM3S9B92 --define=TARGET_IS_TEMPEST_RC1 --include_path="C:/Projects/AX8410Stellaris/Test" --include_path="C:/Program Files/StellarisWare" --include_path="C:/Program Files/Texas Instruments/ccsv4/tools/compiler/TMS470 Code Generation Tools 4.6.6/include" --include_path="C:/Projects/AX8410Stellaris" --diag_warning=225 -me --gen_func_subsections --abi=eabi --code_state=16 --ual --preproc_with_compile --preproc_dependency="test.pp"  "../test.cpp"
"../test.cpp", line 24: error: identifier "string" is undefined
1 error detected in the compilation of "../test.cpp".
>> Compilation failure
gmake: *** [test.obj] Error 1
gmake: Target `all' not remade because of errors.
Build complete for project Test
_____________________________________________________________________
I highlighted the problem above in red. I have the "string" include file in the include path. So, what's up? Why doesn't CCS even recognize "string" as a C++ standard library class? Can anyone help? Is this the case with other classes within the C++ standard library?
Thanks,
Johnas

  • All the files in the C++ standard library declare all of its entities within the std namespace.  So you need to add 'using namespace std;' to your code:

      using namespace std;
      int main(void)
      {
          string strTest("Test");

          for (;;) { }
      }


    Regards,

    David

  • David points out one solution.  But there is more to think about.  Please see http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5 .  That's why, instead of a using declaration, I prefer to explicitly write "std::string" as needed.

    Thanks and regards,

    -George

  • Thanks guys. I've got it all working. I did not use the "using namespace std;". Instead, I'm explicitly writing, "std::string" as George had suggested. I had another problem before I could get it to work. As I mentioned, I had written my own string class. When I substituted the C++ Standard Library's string class, my system would crash. I quickly realized that the standard string class used up more memory than my own string class. My string class was only as subset of the standard string class. So, this was to be expected. What I didn't understand was when I first allocated more memory, I allocated more heap space because my string class used the heap. However, I discovered much later that the standard string class actually needed more stack space not heap. My theory is that a form of "smart pointer" is used which overrides the "new" operator or at least some form of memory manager is used that does not allocate heap memory. I could be completely wrong, but I remember reading about something to that effect in "More Effective C++" by Scott Meyers.

    Just curious if anyone understands this well or has experienced the same problem.

    Thanks again everyone.

    Johnas

  • I don't know whether the TI string implementation uses any smart pointers, or how it allocates memory.  But I do know that template code usually involves many layers of function calls.  And this often means lots of stack is required.  Try compiling for optimization with the options --opt_level=2 or --opt_level=3.  At those higher levels of optimization, the compiler performs function inlining.  That certainly improves the performance of the code, and may decrease stack requirements as well.

    Thanks and regards,

    -George