Thread: Static Library Linker Error {Dev C++ / GCC}

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Dongargarh, India
    Posts
    16

    Question Static Library Linker Error {Dev C++ / GCC}

    Hey everyone.
    I'm trying to create & use a static library but kind of stuck at something I can't quite grasp...

    I have created library file containing just 2 functions dealing with temperature conversion. I typed the source(.c) file and the headre(.h) file & compiled them to get the library(.a) file.

    Now when i use it in a new program I get a LINKER ERROR at the call to the function defined in the library.
    [Linker error] undefined reference to `toFahrenheit'
    ld returned 1 exit status


    I'm not sure where I made the mistake... while writing the source or I'm missing something about linking?

    I'm posting my codes.

    Source File fore creating the library "temperature.c"
    Code:
    /*
    temperature library file
    */
    
    double c,f,k;
    
    double toCelsius(double x)
    {
           c = (x-32)*5/9;
           return c;
    }
    
    double toFahrenheit(double x)
    {
           f = x*5/9+32;
           return f;
    }
    Header file "temperature.h"
    Code:
    /*
    header file : temperature
    */
    extern double toCelsius(double);
    extern double toFahrenheit(double);
    New Program "Test.c"
    Code:
    //testing temperature library
    #include<stdio.h>
    #include "temperature.h"
    
    int main()
    {
        double c,f;
        printf("enter Celsius\t");
        scanf("%f",&c);
        f = toFahrenheit(c);
        printf("Fahrenheit = %d",f);
        
        int ch;
        while((ch = getchar()) != '\n' && ch != EOF);
        return 0;
    }


    P.S.
    I'm using Dev C++ v4.9.9.2
    with GCC v3.4.2 on Windows 7

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    This indicates that when you compiled the user binary (that uses your library) you did not include the library object. Not sure how you would do this in Dev-C++ but with GCC on the command line it would be like this:

    gcc -o program object.a userfile.c

    instead of
    gcc -o program userfile.c

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    In something like project->settings->source files, you need to make sure "temperature.c" is also listed as a source file, along with Test.c

    You should be able to see the project "tree" on the left of the IDE.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    Hey, Tanu.

    I would make sure that you have added all source files to your current project if you are using Dev. I'm not sure what you mean by "with GCC", I didn't know you could get GCC functionality in Dev.
    Anyway if you make sure you have all source files in a project together, that should fix the problem.

    I'd also try using #include "temperature.h" in at the top of your file temperature.c . Can anyone else verify this is necessary? I always use this in all non .h files that are coordinated by one .h, and it's always worked. I haven't tried leaving it out.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by Derek Lake View Post
    Hey, Tanu.

    I would make sure that you have added all source files to your current project if you are using Dev. I'm not sure what you mean by "with GCC", I didn't know you could get GCC functionality in Dev.
    Anyway if you make sure you have all source files in a project together, that should fix the problem.

    I'd also try using #include "temperature.h" in at the top of your file temperature.c . Can anyone else verify this is necessary? I always use this in all non .h files that are coordinated by one .h, and it's always worked. I haven't tried leaving it out.
    1. He is using #include "temperature.h", if he wasn't the error would be different (something like implicit declaration of function X)
    2. Undefined reference is at the LINKER stage, not the compilation stage. This has nothing to do with includes, it means the linker literally can't find the library that the referenced function belongs to.
    3. Yes its necessary if you are calling functions that are prototyped/declared in the header file you are including.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static function pointer linker error
    By guitarist809 in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2011, 03:24 AM
  2. linker error with static member...
    By m37h0d in forum C++ Programming
    Replies: 10
    Last Post: 05-25-2011, 02:27 PM
  3. static declaration creates linker error
    By black_spot1984 in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2008, 10:58 AM
  4. linker error, using static member
    By Chiel in forum C++ Programming
    Replies: 6
    Last Post: 06-28-2003, 08:40 PM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM