Thread: C program calling Function in another program

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    1

    C program calling Function in another program

    looking for an example on how to call a function in one program from another program and using makefile to compile them. i just need a simple example.

    thank you

    banging my head against the computer

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by tiger5678 View Post
    looking for an example on how to call a function in one program from another program and using makefile to compile them. i just need a simple example.
    Well:
    Quote Originally Posted by tiger5678 View Post
    banging my head against the computer
    That isn't the quickest solution, though you may eventually solve it that way, if you direct your head banging to the keyboard: https://en.wikipedia.org/wiki/Infinite_monkey_theorem.

    In all seriousness though, you have to be a little more clear. Do you mean calling a function in another source file? If so, that's not too bad. Posting the code and the Makefile for us could help.

    If you are actually trying to invoke a function in another program, then you have a much bigger challenge. Two processes (instances of running programs) can't communicate without some OS-specific stuff. The general term for this is IPC or Inter-Process Communication, some examples being pipes, message queues, FIFOs.

    EDIT:
    If you have some functions that need to be used by two different programs, and you have full control over the source for both programs, you can build a library, and link each program against the one library. That allows two programs to more or less share the same code.

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Here's a sample Makefile that I use e'eryday!

    Code:
    MKFILE    = Makefile
    
    
    GCC       = gcc -g -O3 -W -Wall -Wextra -std=c11
    
    
    CSOURCE   = main.c tetra.c
    CHEADER   = structures.h
    OBJECTS   = ${CSOURCE:.c=.o}
    EXECBIN   = tetra
    
    
    all : ${EXECBIN}
    
    
    ${EXECBIN} : ${OBJECTS}
    	${GCC} -o $@ ${OBJECTS} -lm
    
    
     
    %.o : %.c
    
    
    	${GCC} -c $<
    
    
    
    
    clean :
    	- rm ${OBJECTS} ${EXECBIN}
    
    
    
    
    again :
    	${MAKE} clean all

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by tiger5678 View Post
    ...how to call a function in one program from another program and using makefile to compile them.
    And by 'program' do you mean another separate application program, or do you mean from one .c file to another .c or .h file?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-11-2011, 04:47 AM
  2. having some trouble calling a function in the main program
    By CMakesMeSad :( in forum C Programming
    Replies: 19
    Last Post: 06-26-2009, 09:33 PM
  3. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  4. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  5. calling one program from another program
    By arian in forum C# Programming
    Replies: 2
    Last Post: 09-19-2008, 08:25 AM