Search code examples
cstatic-librariesldstatic-linking

Link Main error with library


Recently, I tried to create a library and to do that I created two files: file-parser.c and array.c with their corresponded .h file. So, in the file-parser I am using a function from the array that calls a length and when I compile (without linking it) parser-file.c the commands

gcc -c -o file-parser.o file-parser.c
gcc -c -o array.o array.c
ar r libutils.a array.o file-parser.o 

don't tell me an error.

The problem is that when I compile the main.c file

gcc -o prova main.c -L. -lutils

gcc returns:

array/libutils.a(file-parser.o): in function "AnalizeRow":
file-parser.c:(.text+0x109): undefined reference to "length"
array/libutils.a(file-parser.o): in the function "GetWord":
file-parser.c:(.text+0x16a): undefined reference to "length"
collect2: error: ld returned 1 exit status

I'm not English, so I think the error message took to me by ld. Can anyone explain where was my error? Thanks.


Solution

  • A similar error may take place in anywhere in a development process!!!

    Your question does not give a complete picture about current development process.

    I see, after read your question, you has two files with source code - array.c and file-parser.c. Also, you mentioned the static library libutils.a, but you did not indicate - how are you created it and what contains it for now.


    I try to recreate your current position:

    The file file-parser.c

    #include <stdlib.h>
    
    int
    AnalizeRow(char row[])
    {
        size_t len = length(row);
        return 0;
    }
    
    
    char *
    GetWord(char row[])
    {
        size_t len = length(row);
        return NULL;
    }
    

    The file file-parser.h

    int AnalizeRow(char row[]);
    char* GetWord(char row[]);
    

    The file array.c

    #include <string.h>
    
    size_t
    length(char row[])
    {
        return strlen(row);
    }
    

    The file array.h

    size_t length(char row[]);
    

    Create the empty static library libutils.a

    $ ar cr libutils.a
    $ ar t libutils.a (it empty)
    

    Your code

    $ gcc -c -o file-parser.o file-parser.c
    $ gcc -c -o array.o array.c
    $ ar r libutils.a array.o file-parser.o
    

    Content the libutils.a

    $ ar t libutils.a
    array.o
    file-parser.o
    

    Build and run

    $ gcc -o prova main.c -L. -lutils && ./prova
    

    I have not errors at all..


    Preliminary conclusions:

    1. I have wrong development process
    2. Your static library libutils.a is cause the problem
    3. Your source code in files is cause the problem

    Testing environment

    $ gcc --version
    gcc (Debian 4.9.2-10) 4.9.2
    $ lsb_release -a
    No LSB modules are available.
    Distributor ID: Debian
    Description:    Debian GNU/Linux 8.7 (jessie)
    Release:    8.7
    Codename:   jessie
    $ uname -a
    $ apt-cache policy binutils
    binutils:
      Installed: 2.25-5+deb8u1
      Candidate: 2.25-5+deb8u1
      Version table:
     *** 2.25-5+deb8u1 0
            500 http://ftp.ru.debian.org/debian/ jessie-proposed-updates/main amd64 Packages
            100 /var/lib/dpkg/status
         2.25-5 0
            500 http://ftp.ru.debian.org/debian/ jessie/main amd64 Packages
            500 http://httpredir.debian.org/debian/ jessie/main amd64 Packages