【问题标题】:How can I detect if GHC is set to generate 32bit or 64bit code by default?如何检测 GHC 默认设置为生成 32 位还是 64 位代码?
【发布时间】:2011-06-02 16:35:09
【问题描述】:

我的makefile 中有以下内容:

GLFW_FLAG := -m32 -O2 -Iglfw/include -Iglfw/lib -Iglfw/lib/cocoa $(CFLAGS)
...
$(BUILD_DIR)/%.o : %.c
    $(CC) -c $(GLFW_FLAG) $< -o $@
$(BUILD_DIR)/%.o : %.m
    $(CC) -c $(GLFW_FLAG) $< -o $@

-m32 指示 GCC 生成 32 位代码。它存在是因为在某些配置中,GHC 设置为构建 32 位代码,但 GCC 的默认值有时是 64 位。我想概括一下,以便它自动检测 GHC 是在构建 32 位还是 64 位代码,然后将正确的标志传递给 GCC。

问题:我如何询问 GHC 它将构建什么类型的代码(32 位与 64 位)?

PS:我的 cabal 文件在构建期间调用此 makefile 以解决 cabal 中的限制。我真希望我可以在我的 cabal 文件中将这些作为 c 源列出。

【问题讨论】:

  • 我可以使用它从 GHC 获取字长,但我不知道它是否是正确的值:WORDSIZE := $(shell ghc +RTS --info | ghc -e "do c
  • 编译一个测试程序并用 Elf 库读取结果是否合理(关于 hackage)?
  • 您也许可以使用ghc -e 'print (maxBound :: Int)' 的结果,这取决于您使用的是 32 位还是 64 位 GHC...
  • ghc --info 提供平台信息(如("Gcc Linker flags","[\"-m64\"]"))
  • 你可以只调用 ghc 而不是 gcc 吗?然后它会自动添加正确的标志。

标签: haskell makefile 32bit-64bit ghc


【解决方案1】:

我看到的常用技巧是询问Int 或Word 的字节或位大小,因为这在 GHC 中会根据机器的字大小而有所不同,

Prelude> :m + Foreign
Prelude Foreign> sizeOf (undefined :: Int)
8
Prelude Foreign> bitSize (undefined :: Int)
64

或者使用系统工具:

$ cat A.hs
main = print ()

$ ghc --make A.hs
[1 of 1] Compiling Main             ( A.hs, A.o )
Linking A ...

$ file A
A: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), 
     dynamically linked (uses shared libs), for GNU/Linux 2.6.27, not stripped

【讨论】:

    【解决方案2】:

    感谢 Ed'ka,我现在知道正确答案了。

    Makefile 现在有这样的规则:

    GCCFLAGS  := $(shell ghc --info | ghc -e "fmap read getContents >>= putStrLn . unwords . read . Data.Maybe.fromJust . lookup \"Gcc Linker flags\"")
    

    它有点长,但它所做的只是从 ghc 的输出中提取“Gcc Linker flags”。 注意:这是ghc --info 的输出,而不是ghc +RTS --info。

    这比其他建议的方式更好,因为它为我提供了 all 需要指定的标志,而不仅仅是 -m 标志。当不需要标志时它也是空的。

    谢谢大家!

    【讨论】:

    • 看来你需要像getContents &gt;&gt;= putStrLn . Data.Maybe.fromJust . lookup "C compiler link flags" . read这样的东西
    【解决方案3】:

    根据我的评论,应该可以编译测试程序并读取生成的二进制文件。

    $ cabal install elf
    

    代码就是

    readElf testFile'sPath >>= \e -> if elfClass e == ELFCLASS64 then print 64 else print 32
    

    或者在 GHCi 中我们看到:

    > e <- readElf "/bin/ls"
    > elfClass e
    ELFCLASS64
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-03
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多