473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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?
Sep 2 '08 #1
3 7271
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 2 '08 #2
donbock
2,426 Expert 2GB
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
Sep 5 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 5 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Alberto Vera | last post by:
Hello: Could you tell me How I Can call a python program from another one? Regards
4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
4
by: Dave | last post by:
I have a program that I've written a class for. I need to call the function in the program from the class. When I try to call the function I receive the error, the name xxx does not exist in the...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
7
by: Steve_Black | last post by:
Hello, I'm toying with the idea of loading a MenuStrip (VB.Net 2005) dynamically based on who is logged into my system. Every user has different security settings and I want to customize the...
11
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
3
by: balakrishnan.dinesh | last post by:
hi frndz, I need to call a C program function from php code, Is that possible, If it is ,then tell me how ? i.e,main() { int callfunction(); int i=2; callfunction(i); } callfucntion(int i)
17
by: Sara | last post by:
Hi, I'm having a problem with my program and I think it stems from me not understand how to call a function and return a int value to main. What I have to do is create a program that runs...
1
by: syok90 | last post by:
i'm doing a lot of AJAX PROGRAM in my website 1. madads.myadsworld.com 2. eshop.myadsworld.com 3. syokchat.myadsworld.com i just want to ask: can i call my php program from another...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.