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.

using command-line parameters argc and argv[]

Hi,

I have created a project to run on the simulator.  I want to load and run the project with command-line parameters.  How can I set this up so that when I load the file and run to main(), the argc and argv[] parameters are initialized values?

so if I declare main to be

int main (int argc, char *argv[])

argc and argv[] should have values I can preset.

 

thanks,

Mike

  • Mike,

    The TI rules for main are that it must be of the form

    void main (void)

    You cannot pass any command line parameters to main.  main isn't even the first thing that gets called - it gets called from c_int00 and that code (provided by TI) doesn't provide any parameters to main.  That being said, there is no reason why you can't do something like:

    #ifdef __TMS320CXX__

    void main (void) {

    int argc = 2;

    char arg[10] = "10.0";

    char * argv = &arg[0];

    #else

    int main (int argc, char *argv[]) {

    #endif

    ...  //code that uses argc and argv

     

    Is this what you are asking for?  Were you hoping that the simulator would provide some way to query you for the command line parameters?

  • Thanks Luke.

    Luke Postema said:

    Is this what you are asking for?

    yes, sort of...

    Luke Postema said:

    Were you hoping that the simulator would provide some way to query you for the command line parameters?

    yes, or at least enter them in some debug window that would load them upon start up.

    A couple comments.  In the linker user's guide, I think there is a section about the boot code passing argc and argv to main.  Why would this be mentioned if main can't have argc and argv?  There is even a linker parameter called --arg_size= N or something like that which allocates memory for command-line parameters.

    That being said, your suggestion is a good one and I can use that but I'm still wondering about the linker guide references so is there any insight you can give on that?

    My program is a standard C program that I want to compile with GNU GCC or C6000 compiler so I can do as you suggest and it should work.  Note that the TI program can compile with this

    int main (int argc, char *argv[]) {

    So I could probably do something like...

    int main (int argc, char *argv[]) {

    #ifdef __TMS320CXX__

    int argc = 2;

    char arg[10] = "10.0";

    char * argv = &arg[0];

    #endif

    I'll test this tomorrow and post what I find out...

  • The most recent compilers support passing argc and argv to main, but it must be done through a special section which only gets allocated if you use the "--arg_size" linker option.  Furthermore, you must have a host debugger attached, much the same way you must in order to use printf, etc.  Consider that there needs to be some program which copies the command-line arguments into target memory.  There should be documentation on this in the compiler or linker user's guide, for which I don't have a reference handy.

  • The documentation in the compiler and linker user's guide is inadequate to do anything with the arc and argv that are passed to main.  There is just no explanation of how to use the .args section and initialize it.  So I went back to the first suggestion.  Here is how it works.

     

    1. Create some globals to mimick the command-line arguments

    #if defined(_TMS320C6X)
    /*! defines number of command-line arguments to support for simulator */
    #define MAX_NUM_ARGS 6
    #endif
    /*! These variables are used by the TI simulator to mimick the command-line 
    parameters of the GCC program. */
    #if defined(_TMS320C6X)
    int argc;
    char *argv[MAX_NUM_ARGS];
    char arg0[] = "mod_simulation";
    char arg1[] = "-pi4_dqpsk";
    char arg2[] = "-num";
    char arg3[] = "2";
    char arg4[] = "-float";
    char arg5[] = "ccs_dqpsk_float.txt";
    #endif

    2. Create an init function that gets called before main. This is done in BIOS using the
     "call user_init function" in the global settings.
    #if defined(_TMS320C6X)
    /*!
    * \brief performs argc and argv initializations.
    *
    * This routine is called by DSP/BIOS before code execution reaches main.
    * It is used as a means to insert the command-line parameters argc and argv.
    * These settings must be done manually by editting the code and recompiling
    * each time an argument changes. It is meant as a work around for not being
    * able to invoke this program directly from a command-line or some CCS debug
    * utility tool.
    *
    * \author Mike Hansen
    */
    void gblfunc_system_init()
    {
    argc = MAX_NUM_ARGS;

    argv[0] = &arg0[0];
    argv[1] = &arg1[0];
    argv[2] = &arg2[0];
    argv[3] = &arg3[0];
    argv[4] = &arg4[0];
    argv[5] = &arg5[0];
    }
    #endif

    3. When execution reaches main, argc and argv are set. We need 2 main declarations:
    1 for GCC and 1 for TI CCS.
    #if defined(_TMS320C6X)
    int main()
    #else
    int main (int argc, char *argv[])
    #endif
    {

    
    

    
    
  • Mike and others,

    Additional ideas on this topic can be found at the forum thread below:

    http://e2e.ti.com/support/development_tools/f/81/p/3263/11445.aspx

    Hope this helps,

    Rafael