Thread: Problem linking to external library

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    9

    Problem linking to external library

    Hello all,

    I've been having issues trying to compile the basic example for the
    paho MQTT library. I've asked this question on stackoverflow before, where it hasn't gotten any traction, so I'm hoping someone here can help me.

    I downloaded the
    pre-built binaries for my system (Windows 10 64) from their projects page. I unpacked the zip file to a folder in the documents folder, where I also created a .c file with the example at the bottom of the Paho product page. My editor is atom and my compiler is gcc. When I tried to compile it in Atom, I got this error:
    Code:
    undefined reference to MQTTClient_create' 
    So I went searching and found plenty of topics, but I still couldn't figure out, how to resolve this issue. From this
    stackoverflow topic I gather that it's a linker problem and that I need to link the files during compile, so here's what I tried:
    Code:
    gcc MQTT.c -L "C:\Users\Pete\Documents\MQTT on C\Examples\Paho\lib"-l paho-mqtt3c

    Which still gives me the same undefined reference error. When I try to link to the
    dll of the same name, the compiler does not find the file.

    Can anyone point me in the right direction, please?
    Any help is appreciated!

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    From what you are saying, it's finding the header and the library, finding the prototype of MQTTClient_create in the header, but somehow not finding MQTTClient_create in the library. That's strange.

    Try exactly this (copy/paste). Then copy/paste/post everything including the compile line and all that the compile process emits.
    Code:
    gcc MQTT.c -L"C:/Users/Pete/Documents/MQTT on C/Examples/Paho/lib" -lpaho-mqtt3c
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Jan 2019
    Posts
    9
    Here goes:
    Code:
    C:\Users\Pete\Documents\MQTT on C\Examples>gcc MQTT.c -L"C:/Users/Pete/Documents/MQTT on C/Examples/Paho/lib" -lpaho-mqtt3c
    c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Pete\AppData\Local\Temp\ccFGYzxk.o:MQTT.c:(.text+0xc1): undefined reference to `MQTTClient_create'
    c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Pete\AppData\Local\Temp\ccFGYzxk.o:MQTT.c:(.text+0xe8): undefined reference to `MQTTClient_connect'
    c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Pete\AppData\Local\Temp\ccFGYzxk.o:MQTT.c:(.text+0x163): undefined reference to `MQTTClient_publishMessage'
    c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Pete\AppData\Local\Temp\ccFGYzxk.o:MQTT.c:(.text+0x1ae): undefined reference to `MQTTClient_waitForCompletion'
    c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Pete\AppData\Local\Temp\ccFGYzxk.o:MQTT.c:(.text+0x1e0): undefined reference to `MQTTClient_disconnect'
    c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Pete\AppData\Local\Temp\ccFGYzxk.o:MQTT.c:(.text+0x1ef): undefined reference to `MQTTClient_destroy'
    collect2.exe: error: ld returned 1 exit status

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    So it's more than just MQTTClient_create that it's not finding. But it seems to be finding the header, the prototypes (no misspellings), the library, but not the function definitions inside the library. All I can think of is to try -lpaho-mqtt3cs (or possibly -lpaho-mqtt3a or anything else you can see in the lib directory).

    Note that forward slashes worked in the -L string, which is nice. However, it makes no difference. You might try removing the C: at the beginning (if you're compiling from C: anyway), but that's a long shot since it seems to be finding the library or else it should say "cannot find -lpaho-mqtt3c" (you could add an extra letter into the -L string somewhere, giving the wrong path, to ensure it says that). Unfortunately I'm on Linux so I can't really test it out properly.

    EDIT: You could also try using the full name of the library file after -l, including the .lib extension.
    Last edited by john.c; 01-08-2019 at 06:16 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Jan 2019
    Posts
    9
    Yeah, basically it complains about all the functions. I've tried linking the different files before, with no change in result. As far as I understand it the ...a libs are for the asynchronous functions, so I wouldn't expect these functions to be in there anyway.
    Is there anything else I can try?

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    All I can think of is to try what I said in the EDIT above, i.e., use the full library name, including the .lib extension.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Jan 2019
    Posts
    9
    If I add the .lib extension I get an error: cannot find -lpaho-mqtt3c.lib

    Thanks for all your input so far! it's been good getting some confirmation that I'm not completely wrong in what I tried.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by jak888 View Post
    If I add the .lib extension I get an error: cannot find -lpaho-mqtt3c.lib

    Thanks for all your input so far! it's been good getting some confirmation that I'm not completely wrong in what I tried.
    Your prior post said you were using GCC ".lib" is not right in most cases.
    I suggest trying ".a", ".dll", or ".dll.a" as being more likely.

    Tim S.
    "...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

  9. #9
    Registered User
    Join Date
    Jan 2019
    Posts
    9
    Hi Tim,

    thanks for the tip! I have tried that before and it's the same error: cannot find -lpaho-mqtt3c.dll

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by jak888 View Post
    Hi Tim,

    thanks for the tip! I have tried that before and it's the same error: cannot find -lpaho-mqtt3c.dll
    And, the other two give what?
    "...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

  11. #11
    Registered User
    Join Date
    Jan 2019
    Posts
    9
    Which other two are you referring to?

    -l paho-mqtt3c.dll --> cannot find
    -l paho-mqtt3c.lib --> cannot find
    -l paho-mqtt3c --> undefined reference MQTTClient_Create
    -l paho-mqtt3cs.dll --> cannot find
    -l paho-mqtt3a.dll --> cannot find

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by stahta01 View Post
    Your prior post said you were using GCC ".lib" is not right in most cases.
    I suggest trying ".a", ".dll", or ".dll.a" as being more likely.

    Tim S.
    Please read my prior post!
    "...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

  13. #13
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    stahta01 is thinking of -l paho-mqtt3c.dll.a

    @stahta01, The list of files in the Windows download for this is:
    Code:
    paho-mqtt3a.dll
    paho-mqtt3a.lib
    paho-mqtt3as.dll
    paho-mqtt3as.lib
    paho-mqtt3c.dll
    paho-mqtt3c.lib
    paho-mqtt3cs.dll
    paho-mqtt3cs.lib

    I was able to compile the same sample code on linux linking to paho-mqtt3c
    Last edited by john.c; 01-08-2019 at 08:10 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  14. #14
    Registered User
    Join Date
    Jan 2019
    Posts
    9
    Sorry about that!

    Both of these give me the cannot find error. Which makes sense as those files do not exist. Should I have them?

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by jak888 View Post
    Sorry about that!

    Both of these give me the cannot find error. Which makes sense as those files do not exist. Should I have them?
    No idea, depends where you got the library from?

    Tim S.
    "...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. Visual C++ Linking Problem: Unresolved External Symbol
    By neoanderson9318 in forum C++ Programming
    Replies: 5
    Last Post: 11-23-2010, 04:13 PM
  2. Linking library problem
    By seibold in forum C Programming
    Replies: 6
    Last Post: 06-28-2010, 10:48 AM
  3. Problem calling external method from within a shared library
    By Major Tom in forum C++ Programming
    Replies: 0
    Last Post: 04-21-2007, 09:14 AM
  4. Linking Problem in C++(Unresolved external symbol ....)
    By grscot in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2003, 08:08 PM
  5. Library Linking problem
    By Lynchie in forum C Programming
    Replies: 3
    Last Post: 07-23-2002, 08:49 AM

Tags for this Thread