datanoise / mongo.cr

Crystal binding for MongoDB C Driver
MIT License
97 stars 35 forks source link

Unable to build on Debian stretch #22

Closed Val closed 7 years ago

Val commented 7 years ago

Environment: a Debian stretch up to date, with libmongoc-dev package

> crystal --version
Crystal 0.22.0 [3c71228] (2017-04-20) LLVM 3.5.0
> llvm-config-3.9 --version
3.9.1
> cat /etc/{issue,debian_version}
Debian GNU/Linux 9 \n \l

9.0
> dpkg -l libmongoc-dev
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                            Version                      Architecture                 Description
+++-===============================================-============================-============================-===================================================================================================
ii  libmongoc-dev                                   1.4.2-1+b1                   amd64                        MongoDB C client library - dev files

Code: an attempt to connect to a local Mongo database.

> crystal init app try_mongo
      create  try_mongo/.gitignore
      create  try_mongo/LICENSE
      create  try_mongo/README.md
      create  try_mongo/.travis.yml
      create  try_mongo/shard.yml
      create  try_mongo/src/try_mongo.cr
      create  try_mongo/src/try_mongo/version.cr
      create  try_mongo/spec/spec_helper.cr
      create  try_mongo/spec/try_mongo_spec.cr
Initialized empty Git repository in /path/to/try_mongo/.git/

Adding mongo.cr

> cd try_mongo
try_mongo> cat >> shard.yml 
dependencies:
  mongo:
    github: datanoise/mongo.cr
    branch: master
^D
try_mongo> shards install
Updating https://github.com/datanoise/mongo.cr.git
Installing mongo (master)

Add basic code to try db connect

try_mongo> cat > src/try_mongo.cr 
require "mongo"

client = Mongo::Client.new "mongodb://localhost"
db = client["test"]
collection = db["mongo_try"]

puts collection.count
^D

Attempt to build...

> crystal deps build
Dependencies are satisfied
Building: try_mongo
Error target try_mongo failed to compile:
/usr/bin/ld: /opt/crystal/embedded/lib/../lib/libgc.a(os_dep.o): undefined reference to symbol '_end'
//usr/lib/x86_64-linux-gnu/libyajl.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 "try_mongo/bin/try_mongo" "${@}"  -rdynamic  -lmongoc-1.0 -lbson-1.0 -lpcre -lgc -lpthread /opt/crystal/src/ext/libcrystal.a -levent -lrt -ldl -L/usr/lib -L/usr/local/lib`
Val commented 7 years ago

This is more a Linux distribution libyajl override issue, see https://github.com/crystal-lang/crystal/issues/1269#issuecomment-306478314 & https://github.com/crystal-lang/crystal/issues/1269#issuecomment-306490121 .

ysbaddaden commented 7 years ago

@Val what happens if you specify -lyajl? Searching for information about the error, then reading the error message made me realize the DSO missing from command line part in the error message, related to libyajl. It looks like it needs symbols from libyajl but it won't because it's not supposed to, since -lyajl isn't specified in the cc command.

Val commented 7 years ago

src/try_mongo.cr with -lyajl:

require "mongo"

@[Link(ldflags: "-lyajl")]

client = Mongo::Client.new "mongodb://localhost"
db = client["test"]
collection = db["mongo_try"]

puts collection.count

Build attempt:

try_mongo> crystal deps build
Dependencies are satisfied
Building: try_mongo
Error target try_mongo failed to compile:
/usr/bin/ld: /opt/crystal/embedded/lib/../lib/libgc.a(os_dep.o): undefined reference to symbol '_end'
//usr/lib/x86_64-linux-gnu/libyajl.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 "try_mongo/bin/try_mongo" "${@}"  -rdynamic  -lmongoc-1.0 -lbson-1.0 -lpcre -lgc -lpthread /opt/crystal/src/ext/libcrystal.a -levent -lrt -ldl -L/usr/lib -L/usr/local/lib`

@ysbaddaden : -lyajl should have been added in the cc command line ?

Val commented 7 years ago

@ysbaddaden if you want to reproduce a Debian environment with a vagrant up stretch64: https://github.com/Val/crystal/compare/master...Val:add_debian_distribs_to_vagrantfile plus a sudo apt-get -y install libmongoc-1.0 libbson-1.0

Val commented 7 years ago

@ysbaddaden sorry if I bug you with this but you didn't respond to https://github.com/datanoise/mongo.cr/issues/22#issuecomment-306793932 .

Do you think -lyajl missing from the cc command line is normal when @[Link(ldflags: "-lyajl")] is added ?

Val commented 7 years ago

It seems to be a compiler bug, see https://github.com/crystal-lang/crystal/issues/4825 so closing.