Connecting Tech Pros Worldwide Forums | Help | Site Map

How to call a function from one program that's in another program

Member
 
Join Date: Sep 2006
Posts: 68
#1: Sep 2 '08
I'm having a little trouble finishing this program. I don't know how to call a function that's in another program. I need program1a.c to call a function in program1b.c and return control back to program1a.c after the function is done. I have #include "program1b.c" in program1a.c. Do I also need one in program1b.c to include program1a.c? Right now I am getting a segmentation fault when it reaches the point where program1a.c calls a function in program1b.c. Can anyone give me a little help?

Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,379
#2: Sep 2 '08

re: How to call a function from one program that's in another program


First, you do not include source files. That is, do not include .c files in other .c files.

Second, write your function in a .c file:
Expand|Select|Wrap|Line Numbers
  1. void MyFunc(int arg)
  2. {
  3.  
  4. }
  5.  
Then in the file with main(), which is another .c file:
Expand|Select|Wrap|Line Numbers
  1. void MyFunc(int arg);
  2. int main()
  3. {
  4.      MyFunc(10);
  5.      return 0;
  6. }
  7.  
That is, the files that do not contain the actual function must contain the function prototype. This is the first line of the function followed by a semi-colon.

You can gussy this up by putting the function prototype in a header file and then do a #include of the header file as needed in other .c files.

Good luck.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#3: Sep 5 '08

re: How to call a function from one program that's in another program


First of all, don't call program1a.c or program1b.c "programs". They are commonly called 'source files". The C Standard calls them "compilation units"; although I've never heard anybody actually use that term.

The "compiler" converts a source file into an object file; that is, for each foo.c it creates a foo.o.

The "linker" (or "link editor") converts a set of object files into an executable image file. The extension in the output filename depends on your target environment.

The "compiler driver" is a tool that automates the invocations of the compiler and linker for you. For example, for Gnu C you might type this command:
Expand|Select|Wrap|Line Numbers
  1. gcc -o foo.exe program1a.c program1b.c
The gcc compiler driver performs these steps:
  • Compile program1a.c into program1a.o.
  • Compile program1b.c into program1b.o.
  • Link program1a.o and program1b.o into foo.exe.
  • Delete program1a.o and program1b.o.

I'm actually hiding some complexity from you. "Compile foo.c into foo.o" is shorthand for the following steps:
  • Preprocess foo.c into foo.i
  • Compile foo.i into foo.s
  • Assemble foo.s into foo.o
  • Delete foo.i and foo.s

Cheers,
donbock
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,379
#4: Sep 5 '08

re: How to call a function from one program that's in another program


Quote:

Originally Posted by donbock

The C Standard calls them "compilation units"; although I've never heard anybody actually use that term.

Yes, we use the term compilation unit. That is the expanded source file fed to the compiler after the preprocessor has processed all of the preprocessor directives in the source code. A compilation unit is also known as a translation unit.
Reply