gpt4 book ai didi

macos - OSX,ghci,dylib,正确的方法是什么?

转载 作者:行者123 更新时间:2023-12-03 11:43:26 25 4
gpt4 key购买 nike

我需要构建一些 C 代码,然后通过 FFI 引用该 C 代码。我想在 osx 的 ghci 内部使用我的绑定(bind)。我的一个限制是我不能只将 C 源代码交给 .cabal 文件中的 ghc。这是由于 ghc/cabal 的限制可能会在 ghc 的下一个版本中修复(但我希望我的代码现在可以在旧版本上工作)。看到这个bug for details .

该错误的要点是 C 代码需要使用一些 Objective-C 模块进行编译,而 ghc 将它们误解为链接器脚本。我已经尝试了很多事情,并且使用makefile自己构建文件是唯一有效的方法。真的,这不应该是一个问题,因为它应该与我决定使用我没有自己构建的外部 C 库一样。为了解决这个问题,让我们假设它是一个单独的 C 库,我可以使用不同的选项轻松地重建它。

如果我将 C 库构建为 .a,则 ghci 提示它无法打开 .dylib。我的第一个问题是:为什么 ghci 需要一个 .dylib 并且它真的使用它吗?

当我构建一个 dylib 时,我得到一个 segfault when loading the code into ghci .

请记住,这个绑定(bind)已经在其他平台上工作,包括 linux 和 windows,当我编译而不是使用 ghci 时,绑定(bind)在 osx 上工作正常。此问题特定于 osx/ghci 组合。

在上面的那个跟踪中,我正在使用 gdb,但无论我是否使用 gdb,它都会崩溃。我将其追踪到导致崩溃的行:

void _glfwClearWindowHints( void )
{
memset( &_glfwLibrary.hints, 0, sizeof( _glfwLibrary.hints ) );
}

麻烦制造者是 memset 行,实际上问题是在 ghci 中运行时写入 _glfwLibrary 的提示结构是内存访问冲突。提示结构只是一堆整数。它非常平坦和简单,因此我认为问题出在我如何链接事物或 ghci 加载代码的方式上。

以下是我用来构建 dylib 和 .a 的 makefile 的一些内容:
GCCFLAGS  := $(shell ghc --info | ghc -e "fmap read getContents >>=   \
putStrLn . unwords . read . Data.Maybe.fromJust . lookup \
\"Gcc Linker flags\"")
FRAMEWORK := -framework Cocoa -framework OpenGL
GLFW_FLAG := $(GCCFLAGS) -O2 -fno-common -Iglfw/include -Iglfw/lib \
-Iglfw/lib/cocoa $(CFLAGS)

all: $(BUILD_DIR)/static/libglfw.a $(BUILD_DIR)/dynamic/libglfw.dylib

$(BUILD_DIR)/dynamic/libglfw.dylib: $(OBJS)
$(CC) -dynamiclib -Wl,-single_module -compatibility_version 1 \
-current_version 1 \
$(GLFW_FLAG) -o $@ $(OBJS) $(GLFW_SRC) $(FRAMEWORK)

$(BUILD_DIR)/static/libglfw.a: $(OBJS)
ar -rcs $@ $(OBJS)

大多数标志直接取自 GLFW Makefile,所以我认为它们对于该库应该是正确的。

第一行看起来有点奇怪,但这是我用于此 problem 的解决方案.

平台详情:
  • OSX 10.6.6
  • x86_64
  • 4核
  • 通过 Haskell 平台安装程序安装的 GHC 版本 7.0.3
  • 源代码:https://github.com/dagit/GLFW-b

  • 编辑:这是我的问题:
  • 这应该与 ghci 一起使用吗?
  • 如果是这样,我做错了什么或如何修复崩溃?
  • 我可以通过 ghci 使用静态 .a 版本的库吗?
  • 最佳答案

    最初的问题

    Should this work with ghci? If so, what am I doing wrong or how can I fix the crash?



    在 OSX 10.6.7(使用 Haskell Platform/w GHC 7.0.2)上,我可以将您构建的共享库加载到 ghci 中,如下所示:
    ➜  GLFW-b git:(master) ✗ ghci dist/build/Graphics/UI/GLFW.hs -Lbuild/dynam
    ic -lglfw
    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) glfw ... done
    final link ... done
    [1 of 1] Compiling Graphics.UI.GLFW ( dist/build/Graphics/UI/GLFW.hs, inte
    rpreted )
    Ok, modules loaded: Graphics.UI.GLFW.
    *Graphics.UI.GLFW> initialize
    True

    注意:我构建了 glfw使用您提供的库 Makefile ,并另外使用了您的 .cabal要处理的文件 src/Graphics/UI/GLFW.hsc并构建 dist/build/Graphics/UI/GLFW.hs (即我之前运行过 cabal configure/build )。

    Can I just get by with the static .a version of the library with ghci?



    是的,GHC 7.0.2 ( GHC Manual) 中包含对加载静态库的支持。 compiler/ghci/Linker.lhs是一本很好的读物,它将让您对 ghci 如何决定如何处理传递给它的命令行参数有一个高级的了解。此外,在浏览各种平台支持问题时,我发现 this documentation非常有用。

    将静态文件与 ghci 链接。

    在撰写本文时,行 1113compiler/ghci/Linker.hs证明 ghci当前要求由软件包系统构建静态文件(即命名为 HSlibname.a )
    locateOneObj :: [FilePath] -> String -> IO LibrarySpec                        
    locateOneObj dirs lib
    | not ("HS" `isPrefixOf` lib)
    -- For non-Haskell libraries (e.g. gmp, iconv) we assume dynamic library
    = assumeDll
    | not isDynamicGhcLib
    -- When the GHC package was not compiled as dynamic library
    -- (=DYNAMIC not set), we search for .o libraries or, if they
    -- don't exist, .a libraries.
    = findObject `orElse` findArchive `orElse` assumeDll

    对 cmd 行参数解析的进一步调查表明,指定的库在 402 行收集。在 reallyInitDynLinker功能:
    ; classified_ld_inputs <- mapM classifyLdInput cmdline_ld_inputs

    在哪里 classifyLdInput被定义在
    classifyLdInput :: FilePath -> IO (Maybe LibrarySpec)
    classifyLdInput f
    | isObjectFilename f = return (Just (Object f))
    | isDynLibFilename f = return (Just (DLLPath f))
    | otherwise = do
    hPutStrLn stderr ("Warning: ignoring unrecognised input `" ++ f ++ "'")
    return Nothing

    这意味着在包规范之外,当前有 无法直接在 ghci 中链接存档文件(或者说不同,目前没有 cmd-line 参数可以这样做)。

    修复你的 cabal 包

    在您的 .cabal包规范,您正在尝试构建 两个 冲突库:
  • 一个 : 预建库中的链接(根据您在 Setup.hsMakefile 中的规范构建,并根据 extra-librariesextra-lib-dirs 指令链接)
  • : 构建一个新的内联库(c-sourcesframeworks 指令)。

  • 解决上述错误的一个简单方法是删除所有启用 的指令。乙为 Mac OSX 构建时,如下所示:
       include-dirs:
    glfw/include
    glfw/lib
    - c-sources:
    - glfw/lib/enable.c
    - glfw/lib/fullscreen.c
    - glfw/lib/glext.c
    - glfw/lib/image.c
    - glfw/lib/init.c
    - glfw/lib/input.c
    - glfw/lib/joystick.c
    - glfw/lib/stream.c
    - glfw/lib/tga.c
    - glfw/lib/thread.c
    - glfw/lib/time.c
    - glfw/lib/window.c
    +
    + if !os(darwin)
    + c-sources:
    + glfw/lib/enable.c
    + glfw/lib/fullscreen.c
    + glfw/lib/glext.c
    + glfw/lib/image.c
    + glfw/lib/init.c
    + glfw/lib/input.c
    + glfw/lib/joystick.c
    + glfw/lib/stream.c
    + glfw/lib/tga.c
    + glfw/lib/thread.c
    + glfw/lib/time.c
    + glfw/lib/window.c


         if os(darwin)
    - include-dirs:
    - glfw/lib/cocoa
    - frameworks:
    - AGL
    - Cocoa
    - OpenGL
    extra-libraries: glfw
    - extra-lib-dirs: build/static build/dynamic
    + extra-lib-dirs: build/dynamic

    除了验证以下内容现在是否正常工作之外,我没有测试任何东西:
    ➜  GLFW-b git:(master) ✗ ghci                                      
    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.
    Prelude> :m + Graphics.UI.GLFW
    Prelude Graphics.UI.GLFW> initialize
    Loading package GLFW-b-0.0.2.6 ... linking ... done.
    True
    Prelude Graphics.UI.GLFW>

    关于macos - OSX,ghci,dylib,正确的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6323755/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com