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.

TMS320F28379D: sprintf works in a C++ function but not in a C function

Part Number: TMS320F28379D


I have a while (1) loop in my main.cpp file

while (1)

{

    if (++i >= 2000000uL)
    {
          i = 0;
          sprintf(debugStr, "Coffee from C++ function\n");
          g_sci.Tx(debugStr, strnlen(debugStr));
          printDummy(debugStr);
          g_sci.Tx(debugStr, strnlen(debugStr));
    }
}
 

my printDummy function is a C function in a separate C file. I used the extern "C" keywork in my main.cpp file.

extern "C" {

#include "dummy.h"
}

void printDummy(char * dummy)
{
    sprintf(dummy, "Coffee from C function\n");
}

The printf from the C++ code works fine. However, the printf from the C code writes some random values in the string. 
I have open source C code that I need to debug so that's why I used the sprintf function from both C and C++ code.

I'm puzzled. This should work. The code compiles and links just fine.

I'm using the TI v22.6.0.LTS compiler in the legacy COFF mode.

My stack size is 0x2000 and heap size is 0x2000. Printf is enabled with the "nofloat" switch.

Regards

  • I found the bug. I forgot to include <stdio.h>. However, I don't understand why the compiler and linker never complained.... They should have generated an error..



    #include <stdio.h>
    
    
    void printDummy(char * dummy)
    {
        sprintf(dummy, "Coffee from C function\n");
    }
    

  • Benjamin,

    I have looped in compiler expert. Please expect response by tomorrow.

    Thanks, Santosh

  • I don't understand why the compiler and linker never complained.... They should have generated an error..

    A warning is generated by default.

    C:\examples>type file.c
    void printDummy(char *dummy)
    {
        sprintf(dummy, "Coffee from C function\n");
    }
    
    C:\examples>cl2000 file.c
    "file.c", line 3: warning: function "sprintf" declared implicitly

    You can discover the ID number associated with that diagnostic ...

    C:\examples>cl2000 --display_error_number file.c
    "file.c", line 3: warning #225-D: function "sprintf" declared implicitly

    Then use that ID number to elevate the handling of this diagnostic to an error, and thus cause the build to fail.

    C:\examples>cl2000 --display_error_number --diag_error=225 file.c
    "file.c", line 3: error #225-D: function "sprintf" declared implicitly
    1 error detected in the compilation of "file.c".
    
    >> Compilation failure

    For more background on these compiler options, please search the C28x compiler manual for the sub-chapter titled Understanding Diagnostic Messages.

    Thanks and regards,

    -George