library binding / MQTT

312 views
Skip to first unread message

Marc Quinton

unread,
May 11, 2016, 4:24:13 AM5/11/16
to Crystal
hello,

I am trying to write a dynamic binding for MQTT library (mosquitto). I have an error when building application :

quinton:~/Bureau/perso/dev/crystal/mqtt$ crystal build --verbose src/mqtt-test.cr
cc -o "/home/quinton/Bureau/perso/dev/crystal/mqtt/mqtt-test" "${@}"  -rdynamic  /opt/crystal/src/ext/libcrystal.a -levent -lrt -lpcre -lgc -lpthread -lmosquitto -ldl _main.o Array40Pointer40Void4141.o Pointer40Pointer40Void4141.o Fiber.o Pointer40Void41.o Deque40Fiber41.o Pointer40Fiber41.o Event5858Base.o Array40Int3241.o String5858Builder.o String.o ArgumentError.o CallStack.o Pointer40LibUnwind5858Exception41.o UInt64.o Exception.o GC.o Slice40T41.o Slice40UInt841.o IO5858Encoder.o StaticArray40UInt84432102441.o Iconv.o Pointer40UInt841.o Errno.o Int32.o IndexError.o Math.o Char.o StaticArray40UInt84432441.o StaticArray40UInt844326541.o DivisionByZero.o Pointer40Int3241.o IO5858FileDescriptor.o Scheduler.o LibEvent25858EventFlags.o Event5858Event.o Int64.o Exception43.o Array40String41.o Pointer40String41.o 40Fiber3245623240Int643212432Void4141.o IO5858Timeout.o IO5858Error.o Range40B4432E41.o Range40Int324432Int3241.o Signal.o 40Int3232456232Void41.o Event5858SignalHandler.o Hash40Signal443240Signal32456232Void4141.o UInt32.o Pointer40Hash5858-53873ca918e49a5155e4e00c1c71dd1e.o Hash5858Entry40Signal443240Signal32456232Void4141.o IO.o StaticArray40Int324432241.o IO5858FileDescriptor43.o IO5858ByteFormat5858LittleEndian.o IO5858EOFError.o Nil.o Event5858SignalChildHandler.o Hash40Int324432Process5858Status41.o Pointer40Hash5858-5be03dfbff7189de5bb0166c94b5af88.o Hash40Int324432Ch-58936133be82407b4de468047ed7e7dc.o Pointer40Hash5858-9a1110bf3fc45b999f79e9061a2da0d2.o Process5858Status.o Hash5858Entry40Int324432Process5858Status41.o AtExitHandlers.o Array4040Int3232456232Void4141.o Pointer4040Int3232456232Void4141.o LEBReader.o LibUnwind5858Action.o LibUnwind5858ReasonCode.o CallStack5858RepeatedFrame.o
/usr/bin/ld: /opt/crystal/embedded/lib/../lib/libgc.a(os_dep.o): référence au symbole non défini «_end»
//usr/lib/x86_64-linux-gnu/libcares.so.2: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Error: execution of command failed with code: 1: `cc -o "/home/quinton/Bureau/perso/dev/crystal/mqtt/mqtt-test" "${@}"  -rdynamic  /opt/crystal/src/ext/libcrystal.a -levent -lrt -lpcre -lgc -lpthread -lmosquitto -ldl`

I think -lmosquitto library should be just after -rdynamic ; I have done a try with sqlite library binding, this is exactly what is done.

quinton:~/Bureau/perso/dev/crystal/sqlite$ crystal build --verbose src/sqlite.cr
cc -o "/home/quinton/Bureau/perso/dev/crystal/sqlite/sqlite" "${@}"  -rdynamic  -lsqlite3  /opt/crystal/src/ext/libcrystal.a -levent -lrt -lpcre -lgc -lpthread -ldl _main.o Array40Pointer40Void4141.o Pointer40Pointer40Void4141.o Fiber.o Pointer40Void41.o Deque40Fiber41.o Pointer40Fiber41.o Event5858Base.o Array40Int3241.o String5858Builder.o String.o ArgumentError.o CallStack.o Pointer40LibUnwind5858Exception41.o UInt64.o Exception.o GC.o Slice40T41.o Slice40UInt841.o IO5858Encoder.o StaticArray40UInt84432102441.o Iconv.o Pointer40UInt841.o Errno.o Int32.o IndexError.o Math.o Char.o StaticArray40UInt84432441.o StaticArray40UInt844326541.o DivisionByZero.o Pointer40Int3241.o IO5858FileDescriptor.o Scheduler.o LibEvent25858EventFlags.o Event5858Event.o Int64.o Exception43.o Array40String41.o Pointer40String41.o 40Fiber3245623240Int643212432Void4141.o IO5858Timeout.o IO5858Error.o Range40B4432E41.o Range40Int324432Int3241.o Signal.o 40Int3232456232Void41.o Event5858SignalHandler.o Hash40Signal443240Signal32456232Void4141.o UInt32.o Pointer40Hash5858-53873ca918e49a5155e4e00c1c71dd1e.o Hash5858Entry40Signal443240Signal32456232Void4141.o IO.o StaticArray40Int324432241.o IO5858FileDescriptor43.o IO5858ByteFormat5858LittleEndian.o IO5858EOFError.o Nil.o Event5858SignalChildHandler.o Hash40Int324432Process5858Status41.o Pointer40Hash5858-5be03dfbff7189de5bb0166c94b5af88.o Hash40Int324432Ch-58936133be82407b4de468047ed7e7dc.o Pointer40Hash5858-9a1110bf3fc45b999f79e9061a2da0d2.o Process5858Status.o Hash5858Entry40Int324432Process5858Status41.o AtExitHandlers.o Array4040Int3232456232Void4141.o Pointer4040Int3232456232Void4141.o SQLite35858Database.o SQLite35858Flag.o SQLite35858Exception.o SQLite35858Statement.o SQLite35858ResultSet.o LibSQLite35858Code.o Array40Float64321-734008f5d81447e4a281ddcf5d37cced.o Pointer40Float643-9fe78160094db64857683e77d0b11e07.o SQLite35858Type.o Hash40123UInt644432Symbol1254432Bool41.o Pointer40Hash5858-87b25e50fb3d0d3b5c5052fe12251c97.o 123UInt644432Symbol125.o Hash5858Entry40123UInt644432Symbol1254432Bool41.o Float64.o StaticArray40UInt844322241.o UInt8.o Char5858Reader.o InvalidByteSequenceError.o LEBReader.o LibUnwind5858Action.o LibUnwind5858ReasonCode.o CallStack5858RepeatedFrame.o

can you help me please ?

Ary Borenszweig

unread,
May 13, 2016, 10:20:49 AM5/13/16
to crysta...@googlegroups.com
Hi,

If you can provide a link to the code (is it in github?) we could try to help you, otherwise there's not much we can do.

--
You received this message because you are subscribed to the Google Groups "Crystal" group.
To unsubscribe from this group and stop receiving emails from it, send an email to crystal-lang...@googlegroups.com.
To post to this group, send email to crysta...@googlegroups.com.
Visit this group at https://groups.google.com/group/crystal-lang.
To view this discussion on the web visit https://groups.google.com/d/msgid/crystal-lang/a5cbe867-3b3a-4be5-833b-d4184003636f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Ary Borenszweig         Manas Technology Solutions
[ar.phone]                      5258.5240       #ARY(279)
[us.phone]                      312.612.1050    #ARY(279)
[email]                         aboren...@manas.com.ar
[web]                           www.manas.com.ar

Marc Quinton

unread,
May 20, 2016, 11:51:05 AM5/20/16
to Crystal


Le vendredi 13 mai 2016 16:20:49 UTC+2, aborenszweig a écrit :
Hi,

If you can provide a link to the code (is it in github?) we could try to help you, otherwise there's not much we can do.


here is some code :

@[Link(ldflags: "-L/usr/lib -lmosquitto")]

lib Mosquitto
  fun lib_init = mosquitto_lib_init : LibC::Int
end

puts "running Mosquitto.lib_version ..."
status = Mosquitto.lib_init()
puts "done"

and build result :

quinton:~/dev/crystal$ crystal build --verbose libmosquitto.cr
cc -o "/home/quinton/Bureau/perso/dev/crystal/libmosquitto" "${@}"  -rdynamic  -L/usr/lib -lmosquitto /opt/crystal/src/ext/libcrystal.a -levent -lrt -lpcre -lgc -lpthread -ldl _main.o Array40Int3241.o String5858Builder.o String.o ArgumentError.o CallStack.o Array40Pointer40Void4141.o Pointer40Pointer40Void4141.o Pointer40LibUnwind5858Exception41.o UInt64.o Exception.o GC.o Slice40T41.o Slice40UInt841.o IO5858Encoder.o StaticArray40UInt84432102441.o Iconv.o Pointer40UInt841.o Errno.o Int32.o IndexError.o Math.o Char.o StaticArray40UInt84432441.o StaticArray40UInt844326541.o DivisionByZero.o Pointer40Int3241.o Pointer40Void41.o Fiber.o Deque40Fiber41.o Pointer40Fiber41.o Event5858Base.o IO5858FileDescriptor.o Scheduler.o LibEvent25858EventFlags.o Event5858Event.o Int64.o Exception43.o Array40String41.o Pointer40String41.o 40Fiber3245623240Int643212432Void4141.o IO5858Timeout.o IO5858Error.o Range40B4432E41.o Range40Int324432Int3241.o Signal.o 40Int3232456232Void41.o Event5858SignalHandler.o Hash40Signal443240Signal32456232Void4141.o UInt32.o Pointer40Hash5858-53873ca918e49a5155e4e00c1c71dd1e.o Hash5858Entry40Signal443240Signal32456232Void4141.o IO.o StaticArray40Int324432241.o IO5858FileDescriptor43.o IO5858ByteFormat5858LittleEndian.o IO5858EOFError.o Nil.o Event5858SignalChildHandler.o Hash40Int324432Process5858Status41.o Pointer40Hash5858-5be03dfbff7189de5bb0166c94b5af88.o Hash40Int324432Ch-58936133be82407b4de468047ed7e7dc.o Pointer40Hash5858-9a1110bf3fc45b999f79e9061a2da0d2.o Process5858Status.o Hash5858Entry40Int324432Process5858Status41.o AtExitHandlers.o Array4040Int3232456232Void4141.o Pointer4040Int3232456232Void4141.o LEBReader.o LibUnwind5858Action.o LibUnwind5858ReasonCode.o CallStack5858RepeatedFrame.o
/usr/bin/ld: /opt/crystal/embedded/lib/../lib/libgc.a(os_dep.o): undefined reference to symbol '_end'

//usr/lib/x86_64-linux-gnu/libcares.so.2: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Error: execution of command failed with code: 1: `cc -o "/home/quinton//ev/crystal/libmosquitto" "${@}"  -rdynamic  -L/usr/lib -lmosquitto /opt/crystal/src/ext/libcrystal.a -levent -lrt -lpcre -lgc -lpthread -ldl`


I have been able to build one simple binding for sqlite :

@[Link(ldflags: "-L/usr/lib -lsqlite3")]

lib Sqlite

   # SQLITE_API const char *sqlite3_libversion(void);
   fun libversion = sqlite3_libversion: Char*

   # SQLITE_API int sqlite3_libversion_number(void);
   fun libversion_number = sqlite3_libversion_number: LibC::Int
 
end

version = Sqlite.libversion
puts version

puts Sqlite.libversion_number



there may be a problem with mosquitto library.

Ary Borenszweig

unread,
May 22, 2016, 10:10:24 AM5/22/16
to crysta...@googlegroups.com
Hi,

In Mac, I did `brew install mosquitto`, then ran this script:

~~~
@[Link("mosquitto")]
lib LibMosquitto
  alias Int = LibC::Int

  fun init = mosquitto_lib_init : Int
  fun version = mosquitto_lib_version(major : Int*, minor : Int*, revision : Int*) : Int*
end

init = LibMosquitto.init
pp init

LibMosquitto.version(out major, out minor, out revision)
pp major, minor, revision
~~~

And got as output:

~~~
init = 0
major = 1
minor = 4
revision = 8
~~~

On what platform are you? You have to make sure libmosquitto in the system's library path.

--
You received this message because you are subscribed to the Google Groups "Crystal" group.
To unsubscribe from this group and stop receiving emails from it, send an email to crystal-lang...@googlegroups.com.
To post to this group, send email to crysta...@googlegroups.com.
Visit this group at https://groups.google.com/group/crystal-lang.

For more options, visit https://groups.google.com/d/optout.

Marc Quinton

unread,
May 23, 2016, 3:31:43 AM5/23/16
to Crystal


Le dimanche 22 mai 2016 16:10:24 UTC+2, aborenszweig a écrit :
Hi,

In Mac, I did `brew install mosquitto`, then ran this script:

~~~
@[Link("mosquitto")]
lib LibMosquitto
  alias Int = LibC::Int

  fun init = mosquitto_lib_init : Int
  fun version = mosquitto_lib_version(major : Int*, minor : Int*, revision : Int*) : Int*
end

init = LibMosquitto.init
pp init

LibMosquitto.version(out major, out minor, out revision)
pp major, minor, revision
~~~

And got as output:

~~~
init = 0
major = 1
minor = 4
revision = 8
~~~

On what platform are you? You have to make sure libmosquitto in the system's library path.

hello Ary,

I am on Debian jessie. and yes, libmosquitto is installed.

quinton:~/dev/crystal$ dpkg -l | grep mosqui
ii  libmosquitto-dev                                            1.3.4-2                              all          MQTT version 3.1 client library, development files
ii  libmosquitto1                                               1.3.4-2                              amd64        MQTT version 3.1 client library
ii  libmosquittopp-dev                                          1.3.4-2                              all          MQTT version 3.1 client C++ library, development files
ii  libmosquittopp1                                             1.3.4-2                              amd64        MQTT version 3.1 client C++ library
ii  mosquitto                                                   1.3.4-2                              amd64        MQTT version 3.1/3.1.1 compatible message broker
ii  mosquitto-clients                                           1.3.4-2                              amd64        Mosquitto command line MQTT clients
 
quinton:~/dev/crystal$ ls -al /usr/lib/libmosquitto*
 0 lrwxrwxrwx 1 root root    17 Aug 17  2014 /usr/lib/libmosquitto.so -> libmosquitto.so.1
60 -rw-r--r-- 1 root root 60328 Aug 17  2014 /usr/lib/libmosquitto.so.1
 0 lrwxrwxrwx 1 root root    19 Aug 17  2014 /usr/lib/libmosquittopp.so -> libmosquittopp.so.1
24 -rw-r--r-- 1 root root 22936 Aug 17  2014 /usr/lib/libmosquittopp.so.1


adding /usr/lib to LD_LIBRARY_PATH does not correct my problem.


Marc Quinton

unread,
May 23, 2016, 3:39:34 AM5/23/16
to Crystal

same problem on Ubuntu 16.04 :

marc@ubuntu-xenial:~/crystal$ ~/crystal-0.16.0-1/bin/crystal build --verbose ./test.cr
cc -o "/home/marc/crystal/test" "${@}"  -rdynamic  -lmosquitto /home/marc/crystal-0.16.0-1/src/ext/libcrystal.a -levent -lrt -lpcre -lgc -lpthread -ldl _main.o Array40Pointer40Void4141.o Pointer40Pointer40Void4141.o Fiber.o Pointer40Void41.o Deque40Fiber41.o Pointer40Fiber41.o Event5858Base.o Array40Int3241.o String5858Builder.o String.o ArgumentError.o CallStack.o Pointer40LibUnwind5858Exception41.o UInt64.o Exception.o GC.o Slice40T41.o Slice40UInt841.o IO5858Encoder.o StaticArray40UInt84432102441.o Iconv.o Pointer40UInt841.o Errno.o Int32.o IndexError.o Math.o Char.o StaticArray40UInt84432441.o StaticArray40UInt844326541.o DivisionByZero.o Pointer40Int3241.o IO5858FileDescriptor.o Scheduler.o LibEvent25858EventFlags.o Event5858Event.o Int64.o Exception43.o Array40String41.o Pointer40String41.o 40Fiber3245623240Int643212432Void4141.o IO5858Timeout.o IO5858Error.o Range40B4432E41.o Range40Int324432Int3241.o Signal.o 40Int3232456232Void41.o Event5858SignalHandler.o Hash40Signal443240Signal32456232Void4141.o UInt32.o Pointer40Hash5858-53873ca918e49a5155e4e00c1c71dd1e.o Hash5858Entry40Signal443240Signal32456232Void4141.o IO.o StaticArray40Int324432241.o IO5858FileDescriptor43.o IO5858ByteFormat5858LittleEndian.o IO5858EOFError.o Nil.o Event5858SignalChildHandler.o Hash40Int324432Process5858Status41.o Pointer40Hash5858-5be03dfbff7189de5bb0166c94b5af88.o Hash40Int324432Ch-58936133be82407b4de468047ed7e7dc.o Pointer40Hash5858-9a1110bf3fc45b999f79e9061a2da0d2.o Process5858Status.o Hash5858Entry40Int324432Process5858Status41.o AtExitHandlers.o Array4040Int3232456232Void4141.o Pointer4040Int3232456232Void4141.o LEBReader.o LibUnwind5858Action.o LibUnwind5858ReasonCode.o CallStack5858RepeatedFrame.o
/usr/bin/ld: /home/marc/crystal-0.16.0-1/embedded/lib/../lib/libgc.a(os_dep.o): référence au symbole non défini «_end»

//usr/lib/x86_64-linux-gnu/libcares.so.2: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Error: execution of command failed with code: 1: `cc -o "/home/marc/crystal/test" "${@}"  -rdynamic  -lmosquitto /home/marc/crystal-0.16.0-1/src/ext/libcrystal.a -levent -lrt -lpcre -lgc -lpthread -ldl`
marc@ubuntu-xenial:~/crystal$
marc@ubuntu-xenial:~/crystal$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04 LTS"


Marc Quinton

unread,
May 23, 2016, 5:51:10 AM5/23/16
to Crystal
Reply all
Reply to author
Forward
0 new messages