Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#5101 closed feature request (fixed)

GHC and GHCi use different syntax for dynamic libraries

Reported by: mcandre Owned by:
Priority: normal Milestone: 7.2.1
Component: Compiler Version: 7.0.2
Keywords: ghc ghci dylib load compile c2hs c Cc:
Operating System: MacOS X Architecture: Unknown/Multiple
Type of failure: Incorrect warning at compile-time Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

Loading libcharm.dylib into GHCi works as follows:

$ ghci -lcharm
GHCi, version 7.0.2: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Loading object (dynamic) charm ... done
final link ... done
Prelude>

-lcharm works for GCC, too.

$ gcc -o hellocharm.c -lcharm

But not GHC.

$ ghc --make -fforce-recomp -o hellocharm hellocharm.hs charm.hs -lcharm
[1 of 2] Compiling Charm            ( Charm.hs, Charm.o )
[2 of 2] Compiling Main             ( hellocharm.hs, hellocharm.o )
Linking hellocharm ...
ld: library not found for -lcharm
collect2: ld returned 1 exit status
make: *** [hellocharm] Error 1

Inexplicably, a different flag must be used: -dylib-install-name flag must be used instead.

$ ghc --make -fforce-recomp -o hellocharm hellocharm.hs charm.hs charm.c -I/usr/include -dylib-install-name /usr/lib/libcharm.dynlib
[1 of 2] Compiling Charm            ( charm.hs, charm.o )
[2 of 2] Compiling Main             ( hellocharm.hs, hellocharm.o )
Linking hellocharm ...

Why the weird syntax? It seems that in this regard, GHCi is actually closer to GCC syntax than GHC.

Change History (7)

comment:1 Changed 9 years ago by igloo

Milestone: 7.2.1
Status: newinfoneeded

I don't understand what's going on here.

Does the program you get when using the -dylib-install-name flag actually work?

Does the gcc-compiled program actually work?

Can you attach the output of

ghc -v --make -fforce-recomp -o hellocharm hellocharm.hs charm.hs -lcharm

please?

comment:2 Changed 9 years ago by mcandre

Let me explain it all over again.

-lSOMELIBRARY syntax is commonly used to import libraries, either .SOs (Linux), .DLLs (Windows), or .DYLIBs (Mac).

-lSOMELIBRARY verifiably works for dylibs in GHCi.

$ ghci -lcharm
GHCi, version 7.0.2: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Loading object (dynamic) charm ... done
final link ... done
Prelude>

But not GHC. The pertinent line in the verbose output is ld: library not found for -lcharm .

I am asking for GHC to accept -lSOMELIBRARY instead of the weird -dylib-install-name syntax. It's inconsistent for GHCi to use one syntax and GHC to use another. My primary preference is that they both use -lSOMELIBRARY , but it would also be acceptable if they both used -dylib-install-name .

The C code to create the dylib verifiably works; there's an example at GitHub. https://github.com/mcandre/charm#readme

Incidentally, when the dylib is successfully imported using -dylib-install-name , there are other problems to consider.

These are reported at Haskell Cafe. http://groups.google.com/group/haskell-cafe/browse_thread/thread/9896a511c67e7532

$ ghc -v --make -fforce-recomp -o hellocharm hellocharm.hs charm.hs -lcharm
Glasgow Haskell Compiler, Version 7.0.2, for Haskell 98, stage 2 booted by GHC version 6.12.3
Using binary package database: /Library/Frameworks/GHC.framework/Versions/7.0.2-i386/usr/lib/ghc-7.0.2/package.conf.d/package.cache
Using binary package database: /Users/andrew/.ghc/i386-darwin-7.0.2/package.conf.d/package.cache
package happstack-6.0.0-c49252982a038b5a87025323c5423364 is unusable due to missing or recursive dependencies:
  happstack-ixset-6.0.0-5b9328e35d9b851c97fe9d19f3b3fd74 happstack-server-6.0.3-f3c609a5cec76128ef323e0d66ec1b4d happstack-state-6.0.0-231e40c865d495396126572dc46833e4 happstack-util-6.0.0-0b470220d46f915fbc69aeb03aa5ebcd
package happstack-ixset-6.0.0-5b9328e35d9b851c97fe9d19f3b3fd74 is unusable due to missing or recursive dependencies:
  happstack-util-6.0.0-0b470220d46f915fbc69aeb03aa5ebcd
package happstack-server-6.0.3-f3c609a5cec76128ef323e0d66ec1b4d is unusable due to missing or recursive dependencies:
  happstack-util-6.0.0-0b470220d46f915fbc69aeb03aa5ebcd hslogger-1.1.4-86bda5851d1910081a340a12e35eb2a2
package happstack-state-6.0.0-231e40c865d495396126572dc46833e4 is unusable due to missing or recursive dependencies:
  happstack-util-6.0.0-0b470220d46f915fbc69aeb03aa5ebcd hslogger-1.1.4-86bda5851d1910081a340a12e35eb2a2
package happstack-util-6.0.0-0b470220d46f915fbc69aeb03aa5ebcd is unusable due to missing or recursive dependencies:
  hslogger-1.1.4-86bda5851d1910081a340a12e35eb2a2
hiding package parsec-2.1.0.1 to avoid conflict with later version parsec-3.1.1
hiding package network-2.2.1.10 to avoid conflict with later version network-2.3.0.2
wired-in package ghc-prim mapped to ghc-prim-0.2.0.0-6bf7b03ebc9c668817e4379b6796c0c2
wired-in package integer-gmp mapped to integer-gmp-0.2.0.3-91607778cf3ae8f3948a50062b4f8479
wired-in package base mapped to base-4.3.1.0-93953b308d1897309792406495cf356b
wired-in package rts mapped to builtin_rts
wired-in package template-haskell mapped to template-haskell-2.5.0.0-5134a59c0456de4c2a07cc56fa258728
wired-in package dph-seq not found.
wired-in package dph-par not found.
Hsc static flags: -static
*** Chasing dependencies:
Chasing modules from: *hellocharm.hs,*charm.hs
Stable obj: [Main, Charm]
Stable BCO: []
Ready for upsweep
  [NONREC
      ModSummary {
         ms_hs_date = Fri Apr  8 07:06:40 EDT 2011
         ms_mod = main:Charm,
         ms_imps = [import Prelude, import Foreign]
         ms_srcimps = []
      },
   NONREC
      ModSummary {
         ms_hs_date = Fri Apr  8 05:35:22 EDT 2011
         ms_mod = main:Main,
         ms_imps = [import Prelude, import Charm]
         ms_srcimps = []
      }]
compile: input file charm.hs
Created temporary directory: /var/folders/N-/N-IxaaKuFae5ik1WKxV6wE+++TI/-Tmp-/ghc38533_0
*** Checking old interface for main:Charm:
[1 of 2] Compiling Charm            ( charm.hs, charm.o )
*** Parser:
*** Renamer/typechecker:
*** Desugar:
    Result size = 16
*** Simplifier SimplMode {Phase = 0 [final],
                      inline,
                      no rules,
                      eta-expand,
                      case-of-case} max-iterations=4:
    Result size = 17
    Result size = 17
*** Tidy Core:
    Result size = 17
*** CorePrep:
    Result size = 21
*** Stg2Stg:
*** CodeGen:
*** CodeOutput:
*** Assembler:
/Developer/usr/bin/gcc -I. -c /var/folders/N-/N-IxaaKuFae5ik1WKxV6wE+++TI/-Tmp-/ghc38533_0/ghc38533_0.s -o charm.o -march=i686 -m32 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -fno-stack-protector -DDONT_WANT_WIN32_DLL_SUPPORT
*** Deleting temp files:
Deleting: /var/folders/N-/N-IxaaKuFae5ik1WKxV6wE+++TI/-Tmp-/ghc38533_0/ghc38533_0.s
compile: input file hellocharm.hs
*** Checking old interface for main:Main:
[2 of 2] Compiling Main             ( hellocharm.hs, hellocharm.o )
*** Parser:
*** Renamer/typechecker:
*** Desugar:
    Result size = 24
*** Simplifier SimplMode {Phase = 0 [final],
                      inline,
                      no rules,
                      eta-expand,
                      case-of-case} max-iterations=4:
    Result size = 24
*** Tidy Core:
    Result size = 24
*** CorePrep:
    Result size = 32
*** Stg2Stg:
*** CodeGen:
*** CodeOutput:
*** Assembler:
/Developer/usr/bin/gcc -I. -c /var/folders/N-/N-IxaaKuFae5ik1WKxV6wE+++TI/-Tmp-/ghc38533_0/ghc38533_0.s -o hellocharm.o -march=i686 -m32 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -fno-stack-protector -DDONT_WANT_WIN32_DLL_SUPPORT
*** Deleting temp files:
Deleting: /var/folders/N-/N-IxaaKuFae5ik1WKxV6wE+++TI/-Tmp-/ghc38533_0/ghc38533_0.s
Upsweep completely successful.
*** Deleting temp files:
Deleting: 
link: linkables are ...
LinkableM (Tue Apr 12 13:18:02 EDT 2011) main:Main
   [DotO hellocharm.o]
LinkableM (Tue Apr 12 13:18:02 EDT 2011) main:Charm
   [DotO charm.o]
Linking hellocharm ...
*** Linker:
/Developer/usr/bin/gcc -v -o hellocharm -march=i686 -m32 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -fno-stack-protector -DDONT_WANT_WIN32_DLL_SUPPORT hellocharm.o charm.o -lcharm -L/Library/Frameworks/GHC.framework/Versions/7.0.2-i386/usr/lib/ghc-7.0.2/base-4.3.1.0 -L/Library/Frameworks/GHC.framework/Versions/7.0.2-i386/usr/lib/ghc-7.0.2/integer-gmp-0.2.0.3 -L/Library/Frameworks/GHC.framework/Versions/7.0.2-i386/usr/lib/ghc-7.0.2/ghc-prim-0.2.0.0 -L/Library/Frameworks/GHC.framework/Versions/7.0.2-i386/usr/lib/ghc-7.0.2 -lHSrtsmain -lHSbase-4.3.1.0 -liconv -lHSinteger-gmp-0.2.0.3 -lHSghc-prim-0.2.0.0 -lHSrts -lm -ldl -u _ghczmprim_GHCziTypes_Izh_static_info -u _ghczmprim_GHCziTypes_Czh_static_info -u _ghczmprim_GHCziTypes_Fzh_static_info -u _ghczmprim_GHCziTypes_Dzh_static_info -u _base_GHCziPtr_Ptr_static_info -u _base_GHCziWord_Wzh_static_info -u _base_GHCziInt_I8zh_static_info -u _base_GHCziInt_I16zh_static_info -u _base_GHCziInt_I32zh_static_info -u _base_GHCziInt_I64zh_static_info -u _base_GHCziWord_W8zh_static_info -u _base_GHCziWord_W16zh_static_info -u _base_GHCziWord_W32zh_static_info -u _base_GHCziWord_W64zh_static_info -u _base_GHCziStable_StablePtr_static_info -u _ghczmprim_GHCziTypes_Izh_con_info -u _ghczmprim_GHCziTypes_Czh_con_info -u _ghczmprim_GHCziTypes_Fzh_con_info -u _ghczmprim_GHCziTypes_Dzh_con_info -u _base_GHCziPtr_Ptr_con_info -u _base_GHCziPtr_FunPtr_con_info -u _base_GHCziStable_StablePtr_con_info -u _ghczmprim_GHCziBool_False_closure -u _ghczmprim_GHCziBool_True_closure -u _base_GHCziPack_unpackCString_closure -u _base_GHCziIOziException_stackOverflow_closure -u _base_GHCziIOziException_heapOverflow_closure -u _base_ControlziExceptionziBase_nonTermination_closure -u _base_GHCziIOziException_blockedIndefinitelyOnMVar_closure -u _base_GHCziIOziException_blockedIndefinitelyOnSTM_closure -u _base_ControlziExceptionziBase_nestedAtomically_closure -u _base_GHCziWeak_runFinalizzerBatch_closure -u _base_GHCziTopHandler_runIO_closure -u _base_GHCziTopHandler_runNonIO_closure -u _base_GHCziConcziIO_ensureIOManagerIsRunning_closure -u _base_GHCziConcziSync_runSparks_closure -u _base_GHCziConcziSignal_runHandlers_closure -Wl,-search_paths_first -read_only_relocs warning -lHSffi
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5664~89/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5664)
 /Developer/usr/bin/../libexec/gcc/i686-apple-darwin10/4.2.1/collect2 -dynamic -arch i386 -macosx_version_min 10.5 -read_only_relocs warning -syslibroot /Developer/SDKs/MacOSX10.5.sdk -weak_reference_mismatches non-weak -u _ghczmprim_GHCziTypes_Izh_static_info -u _ghczmprim_GHCziTypes_Czh_static_info -u _ghczmprim_GHCziTypes_Fzh_static_info -u _ghczmprim_GHCziTypes_Dzh_static_info -u _base_GHCziPtr_Ptr_static_info -u _base_GHCziWord_Wzh_static_info -u _base_GHCziInt_I8zh_static_info -u _base_GHCziInt_I16zh_static_info -u _base_GHCziInt_I32zh_static_info -u _base_GHCziInt_I64zh_static_info -u _base_GHCziWord_W8zh_static_info -u _base_GHCziWord_W16zh_static_info -u _base_GHCziWord_W32zh_static_info -u _base_GHCziWord_W64zh_static_info -u _base_GHCziStable_StablePtr_static_info -u _ghczmprim_GHCziTypes_Izh_con_info -u _ghczmprim_GHCziTypes_Czh_con_info -u _ghczmprim_GHCziTypes_Fzh_con_info -u _ghczmprim_GHCziTypes_Dzh_con_info -u _base_GHCziPtr_Ptr_con_info -u _base_GHCziPtr_FunPtr_con_info -u _base_GHCziStable_StablePtr_con_info -u _ghczmprim_GHCziBool_False_closure -u _ghczmprim_GHCziBool_True_closure -u _base_GHCziPack_unpackCString_closure -u _base_GHCziIOziException_stackOverflow_closure -u _base_GHCziIOziException_heapOverflow_closure -u _base_ControlziExceptionziBase_nonTermination_closure -u _base_GHCziIOziException_blockedIndefinitelyOnMVar_closure -u _base_GHCziIOziException_blockedIndefinitelyOnSTM_closure -u _base_ControlziExceptionziBase_nestedAtomically_closure -u _base_GHCziWeak_runFinalizzerBatch_closure -u _base_GHCziTopHandler_runIO_closure -u _base_GHCziTopHandler_runNonIO_closure -u _base_GHCziConcziIO_ensureIOManagerIsRunning_closure -u _base_GHCziConcziSync_runSparks_closure -u _base_GHCziConcziSignal_runHandlers_closure -o hellocharm -lcrt1.10.5.o -L/Library/Frameworks/GHC.framework/Versions/7.0.2-i386/usr/lib/ghc-7.0.2/base-4.3.1.0 -L/Library/Frameworks/GHC.framework/Versions/7.0.2-i386/usr/lib/ghc-7.0.2/integer-gmp-0.2.0.3 -L/Library/Frameworks/GHC.framework/Versions/7.0.2-i386/usr/lib/ghc-7.0.2/ghc-prim-0.2.0.0 -L/Library/Frameworks/GHC.framework/Versions/7.0.2-i386/usr/lib/ghc-7.0.2 -L/Developer/SDKs/MacOSX10.5.sdk/usr/lib/i686-apple-darwin10/4.2.1 -L/Developer/SDKs/MacOSX10.5.sdk/usr/lib -L/Developer/usr/bin/../lib/gcc/i686-apple-darwin10/4.2.1 -L/Developer/usr/bin/../lib/gcc -L/usr/lib/gcc/i686-apple-darwin10/4.2.1 -L/usr/lib/gcc/i686-apple-darwin10/4.2.1 -L/Developer/usr/bin/../lib/gcc/i686-apple-darwin10/4.2.1/../../../i686-apple-darwin10/4.2.1 -L/Developer/usr/bin/../lib/gcc/i686-apple-darwin10/4.2.1/../../.. -L/Developer/SDKs/MacOSX10.5.sdk/usr/lib/gcc/i686-apple-darwin10/4.2.1/../../../i686-apple-darwin10/4.2.1 -L/Developer/SDKs/MacOSX10.5.sdk/usr/lib/gcc/i686-apple-darwin10/4.2.1/../../.. hellocharm.o charm.o -lcharm -lHSrtsmain -lHSbase-4.3.1.0 -liconv -lHSinteger-gmp-0.2.0.3 -lHSghc-prim-0.2.0.0 -lHSrts -lm -ldl -search_paths_first -lHSffi -lgcc_s.10.5 -lgcc -lSystem
ld: library not found for -lcharm
collect2: ld returned 1 exit status
*** Deleting temp files:
Deleting: 
*** Deleting temp dirs:
Deleting: /var/folders/N-/N-IxaaKuFae5ik1WKxV6wE+++TI/-Tmp-/ghc38533_0

comment:3 Changed 9 years ago by batterseapower

The -lfoo syntax works fine for me with GHC, and I have never used -dylib-install-name (I'm on OS X). That said, I have not tried your particular example because you do not provide the code for your various hs/c files.

The problem you have in that Haskell Cafe thread is just because you have both charm.hs and charm.c, both of which will produce a file called charm.o. It looks like the charm.o from charm.hs has overwritten the one from charm.c, which explains why the duplicate symbol error (from linking charm.o twice) mentions _Charm_getWidth_info.

comment:4 Changed 9 years ago by igloo

Can you test which of these work and which don't, please?

gcc -march=i686 -m32 -lcharm hellocharm.c
gcc -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -lcharm hellocharm.c
gcc -lcharm -Wl,-search_paths_first hellocharm.c
gcc -march=i686 -m32 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -lcharm -Wl,-search_paths_first hellocharm.c

comment:5 Changed 9 years ago by mcandre

Architecture: x86Unknown/Multiple
Keywords: ghc ghci dylib load compile c2hs c added
$ gcc -march=i686 -m32 -lcharm hellocharm.c 
Undefined symbols:
  "_start_charm", referenced from:
      _main in ccfP9YO9.o
  "_end_charm", referenced from:
      _main in ccfP9YO9.o
  "_vcenter_string", referenced from:
      _main in ccfP9YO9.o
  "_get_key", referenced from:
      _main in ccfP9YO9.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
$ gcc -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -lcharm hellocharm.c 
ld: library not found for -lcharm
collect2: ld returned 1 exit status

The -search_paths_first one seems to work, producing an a.out that works just like https://github.com/mcandre/charm#readme

$ gcc -lcharm -Wl,-search_paths_first hellocharm.c 
wonko:charm andrew$ mate .
wonko:charm andrew$ ./a.out
$ gcc -march=i686 -m32 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -lcharm -Wl,-search_paths_first hellocharm.c 
ld: library not found for -lcharm
collect2: ld returned 1 exit status

The makefile that works for me:

https://github.com/mcandre/charm/blob/master/Makefile

gcc -c charm.c charm.h
gcc -arch i386 -arch x86_64 -dynamiclib -o libcharm.dylib -flat_namespace charm.o
ld: warning: in charm.o, file was built for unsupported file format which is not the architecture being linked (i386)
gcc -o hellocharm hellocharm.c -Wall -I/usr/include -L. -lcharm

Apple does a poor job explaining how to create dylibs, so I don't expect my makefile to be the best.

comment:6 Changed 9 years ago by igloo

Resolution: fixed
Status: infoneededclosed
Version: 7.0.37.0.2

OK, from 7.0.3 we no longer use the -isysroot and -mmacosx-version-min flags, so this should now work fine.

comment:7 Changed 9 years ago by mcandre

Cool!

Do you know when 7.0.3 will make its way to Haskell Platform?

Note: See TracTickets for help on using tickets.