Code to compile a code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Profetas

    #1

    Code to compile a code

    In a c program I have a var with the name of a file that I want to compile,
    how would I compile it using c code?


  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: Code to compile a code

    Profetas <xuxu_18@yahoo. com> wrote:[color=blue]
    > In a c program I have a var with the name of a file that I want to compile,
    > how would I compile it using c code?[/color]

    Probably by cobbling together a string with the command to invoke the
    compiler, using your variable, and than feeding it to the system()
    function.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • Eric Sosman

      #3
      Re: Code to compile a code

      Profetas wrote:[color=blue]
      > In a c program I have a var with the name of a file that I want to compile,
      > how would I compile it using c code?[/color]

      You could try something like

      #include <stdlib.h>
      #include <stdio.h>
      ...
      char *sourcefile = "blivet.c";
      char *command = malloc(sizeof(" cc ") + strlen(sourcefi le));
      if (command != NULL) {
      sprintf (command, "cc %s", sourcefile);
      system (command);
      }

      Of course, the actual invocation of your system's C compiler
      might be more complicated than a mere "cc blivet.c". You may
      need something like "cc -c blivet.c" or "cc -O3 -c -Iinc blivet.c"
      or "cc /i:inc /optimize /noload blivet.c" -- it depends on the
      platform you're using.

      Even when you've constructed the desired command string,
      your troubles are not over. The C Standard does not actually
      promise that the system() function will do anything useful --
      it usually does, but there's no guarantee. Also, system()
      gives you no sure-fire way to check whether the compilation
      succeeded or failed, or to examine any diagnostic messages
      the compiler may have produced. In short, while the above
      could be the skeleton of a solution, it isn't a very good
      one. To get something more useful you'll probably need to
      use platform-specific facilities.

      --
      Eric.Sosman@sun .com

      Comment

      • Profetas

        #4
        Re: Code to compile a code

        >Probably by cobbling together a string with the command to invoke the[color=blue]
        >compiler, using your variable, and than feeding it to the system()[/color]
        function.

        That worked Thanks I didn't know that the system accepted vars

        Comment

        • Default User

          #5
          Re: Code to compile a code

          Profetas wrote:[color=blue]
          >
          > In a c program I have a var with the name of a file that I want to compile,
          > how would I compile it using c code?[/color]

          The only possible way would be to invoke a separate compiler using the
          system() function. The form of the command passed to the system is
          completely platform dependent, if it can be done at all. It usually is
          as if you were doing it yourself from the commandline.




          Brian Rodenborn

          Comment

          • Emmanuel Delahaye

            #6
            Re: Code to compile a code

            Profetas wrote on 27/07/04 :[color=blue]
            > In a c program I have a var with the name of a file that I want to compile,
            > how would I compile it using c code?[/color]

            You can call an external compiler via system(). s[n]printf() could help
            to build the string to pass to system().

            --
            Emmanuel
            The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

            "C is a sharp tool"

            Comment

            • Emmanuel Delahaye

              #7
              Re: Code to compile a code

              Profetas wrote on 27/07/04 :[color=blue][color=green]
              >> Probably by cobbling together a string with the command to invoke the
              >> compiler, using your variable, and than feeding it to the system()[/color]
              > function.
              >
              > That worked Thanks I didn't know that the system accepted vars[/color]

              The parameter of system() has the type 'char const *'. Hence, it
              accepts any address pointing to a string. As far as the string is
              valid, the behaviour is system dependent.

              --
              Emmanuel
              The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

              "C is a sharp tool"

              Comment

              • Christopher Benson-Manica

                #8
                Re: Code to compile a code

                Eric Sosman <Eric.Sosman@su n.com> spoke thus:
                [color=blue]
                > char *sourcefile = "blivet.c";[/color]

                ITYM

                const char *sourcefile = "blivet.c";

                although it's an admitted nitpick.

                --
                Christopher Benson-Manica | I *should* know what I'm talking about - if I
                ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                Comment

                Working...