LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-03-2007, 10:36 AM   #1
ElDorado
LQ Newbie
 
Registered: Oct 2007
Distribution: Slackware
Posts: 3

Rep: Reputation: 0
Running GCC during from C program


I would like to compile a program from within a C program.

I imagine this can be done by calling fork on the running process and overlaying the child process with an execXX for the gcc process. However, I am wondering if anyone would have any advice on how this might be done with a library function call from some gcc header file? Any insights would be appreciated.

Thank you

E.D.

Last edited by ElDorado; 10-04-2007 at 05:49 AM.
 
Old 10-03-2007, 11:22 AM   #2
atulsvasu
Member
 
Registered: Apr 2006
Distribution: Gentoo
Posts: 49

Rep: Reputation: 15
Hi,
simplest way to execute any shell command from
a C program is using system() function call.
<code>
#include <stdlib.h>

int main() {
system("gcc cuberoot.cc -lm -o cuberoot);
return 0;
}</code>

shall work perfectly fine. check

$ man system

for more details,

-Atul
 
Old 10-03-2007, 11:30 AM   #3
ElDorado
LQ Newbie
 
Registered: Oct 2007
Distribution: Slackware
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by atulsvasu View Post
Hi,
simplest way to execute any shell command from
a C program is using system() function call.
<code>
#include <stdlib.h>

int main() {
system("gcc cuberoot.cc -lm -o cuberoot);
return 0;
}</code>

shall work perfectly fine. check

$ man system

for more details,

-Atul
Thank you for your reply. You are correct, however this approach incurs an even bigger overhead than forking a process. I am interested, in principle, in finding out if this can be done without neither invoking a new instance of the shell, nor creating a new process. Would anyone have any past experiences he or she would care to share?

Looking forward to some replies,

E.D.
 
Old 10-04-2007, 12:05 AM   #4
atulsvasu
Member
 
Registered: Apr 2006
Distribution: Gentoo
Posts: 49

Rep: Reputation: 15
There is no such support from gcc [pretty certain], you may better care about time taken to
run gcc, than overhead. Overhead will be much smaller compiling C-files.

o Get it nicely compiled, (with profiled bootstrap? and all optimizations).

o You can preload gcc and keep in memory, to avoid reloads every time you run it.

o Another way to optimize would be using pre-compiled headers.

o You can also use tmpfs (memory) to keep your source and output files so that gcc itself won't
spend much time reading it and compiling it.

o You can also run multiple(2 to 3) instances of gcc, if you have an HT processor. (Even other wise
it will fill some unnecessary idle time)
 
Old 10-04-2007, 04:42 AM   #5
ElDorado
LQ Newbie
 
Registered: Oct 2007
Distribution: Slackware
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by atulsvasu View Post
There is no such support from gcc [pretty certain]
That's too bad to hear. I appreciate your practical advice, atulsvasu - I didn't know all these points. Sadly, as efficient as the scheme to which you refer can become, it requires working with the Linux API (Nothing wrong with that per se).

I guess what I really should have asked to simplify the issue was, "Is there a gcc API in C?"

Unfortunately, if I haven't found a reference to such thing in the Internet, I'm ready to believe it's not a feature in existence.

E.D.

Last edited by ElDorado; 10-04-2007 at 05:50 AM.
 
Old 10-04-2007, 08:03 AM   #6
Dox Systems - Brian
Member
 
Registered: Nov 2006
Posts: 344

Rep: Reputation: 31
So, what you're trying to do is replace "make"? Even make calls gcc individually each time (AFAIK).
 
  


Reply

Tags
gcc


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC not able to run C program sco1984 Mandriva 5 08-12-2006 06:11 PM
Can't compile c++ program using gcc pengu Programming 9 02-22-2006 01:59 PM
running gcc or cc MI3GTO Linux - Newbie 3 02-28-2005 04:23 PM
running software built with gcc 3.0.2 on Redhat 8 - gcc 3.2 dst1989 Linux - Software 3 03-08-2004 10:23 PM
Problems running C with gcc!! jacksmash Programming 2 11-02-2003 07:23 PM


All times are GMT -5. The time now is 03:10 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration