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.

printf works, but std::cout does not work?

I have a TMS320C6678LE EVM board and am trying to get it up and operational. I have successfully run through the getting started guides and loaded the HUA demo (it didn't work because I am not using ethernet so the IP aquisition failed). However, I have written the basic hello world program and loaded it onto the board. I was using std::cout to out put text to the console but nothing would come through, so I tested printf and the console displayed my text.

My stack and heap are set to 128Kb in the CORE0_L2_SRAM using the default C6678.cmd file to map the memory.

#include <stdio.h>
#include <iostream>
int main()
{
   FILE *fid;
   fid = fopen("myfile","w");
   fprintf(fid,"Hello, world\n");
   fclose(fid);
   printf("Hello again, world\n");
   std::cout << "Hello World C++ Style\n";
   return 0;
}

What am I missing here? The file IO works fine and the printf works fine  but the std::cout fails to display anything to the console.
Using CCSv5.3, Win7, XDS560v2 via USB

Thanks,

Aaron

++++ EDIT/UPDATE++++++

I found this post after doing a google search http://e2e.ti.com/support/development_tools/code_composer_studio/f/81/t/208929.aspx

It seems that CCS requires a buffer flush before it updates the console output when using c++ std::cout? Can I get a verification on this for CCSv5.3 because doing a buffer flush after each cout statement is a very slow task. If this is true then I will just watch my console outputs more carefully. Thanks.

+++++++++++++++++++++

  • Aaron,

    I am not sure why you would like to mix between cout and printf() in the same code, but nonetheless please check the sections about buffering and flushing of the page below:

    http://processors.wiki.ti.com/index.php/Tips_for_using_printf#Flushing

    You can experiment with the buffering modes mentioned there, and I am almost sure they are also applicable to cout.

    Cheers,

    Rafael

     

  • Rafael,

    Thank you for the information, that is a good explanation of what is probably going on here. For the record I am not wanting to mix printf() and cout in the same code. I only provided the code as an example because I was using   cout << "text.text.text.\n";   And none of my text was placed in the console. Then I tried    printf("text.text.text.\n");    and the text was placed in the console. So I was confused as to why this was the case as I have never used a compiler that behave this way. In fact under MSVC++ using buffer flushing std::endl; statements is much slower than a simple "\n" and text is placed into the console immediately following the execution of the line.

    Again, thank you for the information and I will play with the buffer settings as the wiki article suggest.

    -Aaron

  • After further digging I have found this reference: http://en.cppreference.com/w/cpp/io/manip/unitbuf

    What I was looking for was this command:   std::cout << std::unitbuf;

    I guess this is not on by default under CCS. Placing this at the top of the main() function now creates a console experience akin to what I am use too.

    For those interested I did try this: 
       setvbuf(stdout, NULL, _IOLBF, 0); but it had no affect on the operation of cout.

    So I then tried this:
       setvbuf(stdout, NULL, _IONBF, 0);  and this caused the printf() and cout statements to run slow because it forced immediate console output after every character (i.e. no buffer)

    It was not until I set    std::cout << std::unitbuf;   did the cout statements write to console after each cout statements regardless of a terminating "\n"

  • Aaron,

    Beautiful reference and explanation. Thank you!

    Cheers,

    Rafael