Thread: Undefined reference to a function, linker error

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    2

    Undefined reference to a function, linker error

    This is a rookie question and I might be doing something stupid, but I wrote a C program that includes two non default libraries: curl and libxml2. I am trying to go through the compilation process in detail to learn the process and statically link the libraries. These are the steps I took:
    Downloaded libxml2 and curl and compiled it with the included makefile and did the following:
    Code:
    ar rcs libcurl.a  /home/${USER}/Desktop/Curl/curl-7.73.0/lib/*.o 
    ar rcs libxml2.a /home/${USER}/Desktop/xml2/libxml2-2.7.2/*.o

    Even though I only use a few functions from those libraries I packed all together in case they needed each other, or at least I think that's how it works. I copied the generated arhive library to a separate location and lastly I did:
    Code:
    cp ~/Desktop/libxml2-2.7.2/libxml2.a ~/Desktop/libs/    
    cp ~/Desktop/Curl/curl-7.73.0/lib/libxml2.a ~/Desktop/libs/
    gcc WolframParser.c -I/home/${USER}/Desktop/Curl/curl-7.73.0/include/ -I/home/${USER}/Desktop/libxml2-2.7.2/include/ -L/home/${USER}/Desktop/libs/ -lcurl -lxml2 -lm
    The linker reports a bunch of errors of undefined references, the likes of:
    Code:
            /usr/bin/ld: /home/${USER}/Desktop/libs//libcurl.a(libcurl_la-multi.o): in function `multi_getsock':
        multi.c:(.text+0x18e): undefined reference to `Curl_ssl_getsock'
        /usr/bin/ld: /home/${USER}/Desktop/libs//libcurl.a(libcurl_la-easy.o): in function `curl_global_init':
        easy.c:(.text+0x172): undefined reference to `Curl_ssl_init'
        /usr/bin/ld: /home/${USER}/Desktop/libs//libcurl.a(libcurl_la-easy.o): in function `curl_global_init_mem':
        easy.c:(.text+0x22e): undefined reference to `Curl_ssl_init'
        /usr/bin/ld: /home/${USER}/Desktop/libs//libcurl.a(libcurl_la-easy.o): in function `curl_global_cleanup':
        easy.c:(.text+0x295): undefined reference to `Curl_ssl_cleanup'
        . . .
    Can someone please point out what I did wrong, and can this be done like this at all?




  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    curl-config --libs
    Try running above command and if it works it might show other libraries needed.

    Edit: I just reread your post and I saw you self-built curl; therefore the above likely will not work.

    Tim S.
    Last edited by stahta01; 12-02-2020 at 10:02 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2020
    Posts
    2
    Thanks for the reply!

    I could have installed curl with a package manager (in fact I did that a while ago, I didn't manually mess with object and archive files, I just called gcc on the source file, and it worked then) but I wanted to understand why my current understanding how linker works is wrong.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The libcurl likely needs another library you failed to link to.
    Edit: It also could need an object file you failed to copy.
    Edit2: The order of libraries in a link list matter to some compilers.
    It matters to MinGW GCC linking under Windows; not sure if it is the same under other operating systems.

    Tim S.
    Last edited by stahta01; 12-02-2020 at 10:52 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Linker error]Undefined reference for function
    By lisa92 in forum C Programming
    Replies: 6
    Last Post: 03-08-2012, 01:17 PM
  2. Linker Error - Undefined Reference
    By DaNxTh3xMaNx in forum C Programming
    Replies: 3
    Last Post: 09-07-2011, 05:49 PM
  3. Linker Error/Undefined Reference
    By liljp617 in forum C Programming
    Replies: 2
    Last Post: 09-24-2008, 09:02 PM
  4. linker error undefined reference to
    By BJtoVisualcC++ in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2007, 11:40 AM
  5. Dev C++ linker error: undefined reference
    By josh_d in forum Windows Programming
    Replies: 11
    Last Post: 03-11-2004, 11:58 AM

Tags for this Thread