Results 1 to 3 of 3

Thread: About Code Reuse?

  1. #1
    Join Date
    Jan 2009
    Beans
    328

    About Code Reuse?

    hello,
    I wonder how to reuse another *.c file's function. To have a try and demo, I have written following codes. Would you help me to have a look, and could you give me more suggestion about code reuse in c.
    \\192.168.0.7\myproject\c-c++\linux\caller\caller.c
    Code:
    #include "../becalled/becalled.h"
    
    int main(void)
    {
    	int result= myAdd(1, 2);
    	printf("result=%d",result);
    	return   0;
    }
    \\192.168.0.7\myproject\c-c++\linux\caller\makefile
    Code:
    #include ../becalled/makefile #this want to be realized later.
    run : caller.o
    	gcc -o run caller.o
    caller.o : caller.c ../becalled/becalled ../becalled/becalled.h
    	gcc -c caller.c ../becalled/becalled.h ../becalled/becalled
    clean :
    	rm run caller.o
    \\192.168.0.7\myproject\c-c++\linux\becalled\makefile
    Code:
    becalled: becalled.o
    	gcc -o becalled becalled.o
    becalled.o: becalled.c becalled.h
    	gcc -c becalled.c
    \\192.168.0.7\myproject\c-c++\linux\becalled\becalled.c
    Code:
    int myAdd(int a, int b)
    {
    	int result=0;
    	result=a+b;
    	return result; 
    }
    
    int main(void)
    {
    	return   0;
    }
    \\192.168.0.7\myproject\c-c++\linux\becalled\becalled.h
    Code:
    int myAdd(int a, int b);
    make command output at:
    Code:
    root@linux-programming:~/myproject/c-c++/linux/caller# make
    gcc -o run caller.o
    caller.o: In function `main':
    caller.c:(.text+0x13): undefined reference to `myAdd'
    collect2: ld returned 1 exit status
    make: *** [run] Error 1

  2. #2
    Join Date
    Feb 2008
    Location
    Denmark
    Beans
    91

    Re: About Code Reuse?

    Just compile the other file, too, i.e if you have a file main.c and it's calling a function inside another file call.c:

    Code:
    gcc main.c test.c
    If you're including a header, you still have to add the file(s) being called from the header:

    main.c:
    Code:
    #include "header.h"
    
    int main(void)
    {
            add(2, 3);
    
            return 0;
    }
    header.h:
    Code:
    int add(int, int);
    add.c:
    Code:
    int add(int a, int b)
    {
            return a + b;
    }
    Would be compiled with:
    Code:
    gcc main.c add.c
    You want to read up on header files and include guards.

  3. #3
    Join Date
    Jan 2009
    Beans
    328

    Re: About Code Reuse?

    Quote Originally Posted by Nemooo View Post
    Just compile the other file, too, i.e if you have a file main.c and it's calling a function inside another file call.c:

    Code:
    gcc main.c test.c
    If you're including a header, you still have to add the file(s) being called from the header:

    main.c:
    Code:
    #include "header.h"
    
    int main(void)
    {
            add(2, 3);
    
            return 0;
    }
    header.h:
    Code:
    int add(int, int);
    add.c:
    Code:
    int add(int a, int b)
    {
            return a + b;
    }
    Would be compiled with:
    Code:
    gcc main.c add.c
    You want to read up on header files and include guards.
    Thanks for your guidance. While, I really don't like to have the add re-compiled again, so I may seek to use the dll instead.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •