Code to compile a code

P

Profetas

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?
 
J

Jens.Toerring

Profetas said:
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?

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
 
E

Eric Sosman

Profetas said:
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?

You could try something like

#include <stdlib.h>
#include <stdio.h>
...
char *sourcefile = "blivet.c";
char *command = malloc(sizeof("cc ") + strlen(sourcefile));
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.
 
P

Profetas

Probably by cobbling together a string with the command to invoke the
compiler, using your variable, and than feeding it to the system()
function.

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

Default User

Profetas said:
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?

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
 
E

Emmanuel Delahaye

Profetas wrote on 27/07/04 :
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?

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

Emmanuel Delahaye

Profetas wrote on 27/07/04 :
function.

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top