LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 01-08-2010, 08:04 PM   #1
is noname
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Rep: Reputation: 0
call the C executable inside the C


hi guys
i have only basic knowledge of C so guys plz help me .....

is C language support call the C executable inside the C ??

example contect mainA.c have a many function define in the struct,when i compile mainA and make a executable the name is ( A ),can i use executable C inside the C <my C program call the executable ( A ) > .


Thankss all ..
 
Old 01-08-2010, 10:10 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
...
Code:
system("command/program-name here")
...
is that it?
 
Old 01-09-2010, 12:50 AM   #3
is noname
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
sory guys , i don't understand ..

the executable C have a many function , when i use the system(" ") then function name it actually call the exe file to run

what i want is ,
i wanna to call specific function from the executeable file, is there anyway i can do it ?
 
Old 01-09-2010, 01:11 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Please give an example of exactly what you want to do.
 
Old 01-09-2010, 01:20 AM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Not without fun time loading the program, loading a symbol table and calling it.
 
1 members found this post helpful.
Old 01-09-2010, 01:40 AM   #6
manavendra
LQ Newbie
 
Registered: May 2009
Location: Bangalore, India
Distribution: Kubuntu 2.6.31-14
Posts: 19

Rep: Reputation: 2
Why do you want to call the same entire executable for the C executable running.

If the executable is some other program other than what you are forking from, then you can use the system function call:

Code:
Function: int system (const char *command)
You can code to call the same executable by nesting or recursive use of main() function call. main() can call itself. Using main() to call main() is like running the same executable again !!!
 
Old 01-09-2010, 01:49 AM   #7
manavendra
LQ Newbie
 
Registered: May 2009
Location: Bangalore, India
Distribution: Kubuntu 2.6.31-14
Posts: 19

Rep: Reputation: 2
Quote:
Originally Posted by is noname View Post
i wanna to call specific function from the executeable file, is there anyway i can do it ?
I guess you want to use functions spread across several C source files. For this you need to link together all the object files and create a single executable using the make command. From this executable you can call all executable functions defined in your source codes.

You can read any standard tutorial about creating Makefile to solve your problem
 
Old 01-09-2010, 07:22 AM   #8
is noname
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
hai, guys...

the example content mainfile.c :

struct {
char namefunction[40] ;
int (*f)(int argc, char *argv[]) ;
}manyfunction[2] =
{
{"func1",func1}
{"func2",func2}
}

int main (int argc, char *argv[]) {
/* the code can access to struct match something function from request */

}

when mainfile.c compile to executable,
other source C can call its themself <function in define> ?

can i make a differences executable ?

Thankss...
 
Old 01-09-2010, 07:49 AM   #9
manavendra
LQ Newbie
 
Registered: May 2009
Location: Bangalore, India
Distribution: Kubuntu 2.6.31-14
Posts: 19

Rep: Reputation: 2
Quote:
Originally Posted by is noname View Post
when mainfile.c compile to executable,
other source C can call its themself <function in define> ?
for accessing the functions of mainfile.c from other executables you need to link its object file. For this you just need the object file of mainfile.c

Code:
gcc -c mainfile.c
Then link all the other object files of different source with mainfile.o using Makefile by providing suitable dependencies.
 
Old 01-09-2010, 09:28 AM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your code is trying to do the work that a linker normally does. The usual way to do what you are suggesting is to create separate source files, of which only one has a function 'main()'. The other source files have only public functions, and are compiled to object modules, and then linked with the object module containing 'main()'.
Code:
gcc -c module1.c
gcc -c module2.c
gcc -o moduleMain module1.o module2.o moduleMain.c
The source files:
Code:
/*   module1.c   */
#include <stdlib.h>
#include <stdio.h>

void    funct1(){

    printf( "Function # 1\n" );
    return;

}
/* ----------- */
Code:
/*   module2.c  */
#include <stdlib.h>
#include <stdio.h>

void    funct2(){

    printf( "Function # 2\n" );
    return;

}
/* ---------- */
Code:
/*   moduleMain.c  */
#include <stdlib.h>
#include <stdio.h>

extern void funct1(void);
extern void funct2(void);

int main( int argc, char * argv[] ){
    funct1();
    funct2();
    exit(0);
}
/* ---------- */
Code:
./moduleMain
Function # 1
Function # 2
It is possible to contrive a scheme where you could load and execute a fully linked standalone object module, and have it dispatch to various functions that are contained within it, but this would be unusual, and very convoluted. I feel certain that this is not what you intended.
--- rod.
 
  


Reply



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
problem of function call address in executable file valpa Programming 12 09-05-2008 08:36 AM
getting result from 'translate' call inside C cleopard Linux - General 4 08-08-2008 01:49 PM
how to call a function inside switch case sarathius Programming 4 01-28-2008 05:36 AM
problem on dynamic function call inside a class! antony_csf Programming 0 06-29-2004 10:15 PM
Unmounting a cdrom inside an executable jshort Programming 1 04-09-2003 08:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:31 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration