Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

May be a bug on linking static library

Old Baby
2012-04-13
2013-06-06
  • Old Baby
    Old Baby
    2012-04-13

    This is my code, d.c uses function hello defined at callee.c. 
    d.c
    #include <stdio.h>
    #include "callee.h"

    int main()

        hello(); 
        return 0;
    }

    callee.c
    #include <stdio.h>

    void hello()
    {
        printf("hello\r\n");
    }

    Function hello is provide as static library, libcallee.a.
    orange@orange-Rev-1-0:~/d$ ar -r libcallee.a callee.o
    orange@orange-Rev-1-0:~/d$

    When linking libcallee.a, excute command like this, gcc failes.
    orange@orange-Rev-1-0:~/d$ gcc -o d -lcallee -L. d.o
    d.o: In function `main':
    d.c:(.text+0xa): undefined reference to `hello'
    collect2: ld returns 1
    orange@orange-Rev-1-0:~/d$

    However, if you put the argument '-lcallee' at  the end of the command, gcc succeeds.
    orange@orange-Rev-1-0:~/d$ gcc -o d  -L. d.o -lcallee
    orange@orange-Rev-1-0:~/d$

    I dont't know why, I think this may be a bug.

     
  • Old Baby
    Old Baby
    2012-04-13

    Version of gcc I use is 4.6.2

     
  • Jonathan Yong
    Jonathan Yong
    2012-04-13

    No, not a bug, its meant to work that way, please consult the binutils linker manual.