Thread: including header files and implementation

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    including header files and implementation

    Hello,

    Just experimenting with including user header file and implementation. And compiling using gcc.

    Just a simple program to demostrate. The three files are all seperate files and are in the same directory.

    Code:
    //currency.h 
    int exchange_rate(int amount);
    Code:
    //currency.c implementation 
    #include "currency.h"
    
    int exchange_rate(int amount)
    {
    	return amount * 71;
    }
    Code:
    #include <stdio.h>
    
    #include "currency.h"
    
    int main(int argc, char *argv[])
    {
    	printf("Welcome to currency exchange\n");
    	printf("Exchange rate is: %d\n", exchange_rate(10));
    
    	return 0;
    }
    [root@localhost currency]# gcc currency_app.c -o app
    /tmp/cciXMhc2.o: In function `main':
    currency_app.c.text+0x25): undefined reference to `exchange_rate'
    collect2: ld returned 1 exit status

    I thought as all the files are in the same directory. It should be able to find them.

    I have also compiled currency.c using:
    gcc -c currency.c
    to create the object file.

    Many thanks for any suggestions,

    Steve

    [/code]

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by steve1_rm View Post
    [root@localhost currency]# gcc currency_app.c -o app
    /tmp/cciXMhc2.o: In function `main':
    currency_app.c.text+0x25): undefined reference to `exchange_rate'
    collect2: ld returned 1 exit status

    I thought as all the files are in the same directory. It should be able to find them.

    I have also compiled currency.c using:
    gcc -c currency.c
    to create the object file.

    Many thanks for any suggestions,

    Steve
    1. Why are you compiling as root?
    2. How is the linker supposed to know that exchange_rate() is in currency.o?
    Tell it so, or better yet do it all in one step:
    Code:
    gcc -W currency_app.c currency.c -o app

  3. #3
    Banned
    Join Date
    Jan 2009
    Posts
    30
    If I am being a doofus here, just tell me if I am on the wrong track. But shouldn't it be something like this?

    gcc currency_app.c currency.c -o app

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It can be, but for larger projects you may want to compile each source file into an object file and then link. That way you don't have to compile things that haven't changed... make can do/does this anyway.

  5. #5
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Thanks,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM
  2. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM
  3. Replies: 2
    Last Post: 08-08-2003, 04:36 PM
  4. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM