如何检测GHC是否设置为默认生成32位或64位代码?

Jas*_*git 7 haskell makefile ghc 32bit-64bit

我的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 $@
Run Code Online (Sandbox Code Playgroud)

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

:我怎样才能问GHC它将构建什么类型的代码(32位对64位)?

PS:我的cabal文件在构建期间调用此makefile以解决cabal中的变通方法限制.我希望我可以在我的cabal文件中列出这些作为c-sources.

Don*_*art 6

我看到的通常技巧是要求一个Int或多个字节或位的大小Word,因为根据机器的字大小,GHC会有所不同,

Prelude> :m + Foreign
Prelude Foreign> sizeOf (undefined :: Int)
8
Prelude Foreign> bitSize (undefined :: Int)
64
Run Code Online (Sandbox Code Playgroud)

或者使用系统工具:

$ 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
Run Code Online (Sandbox Code Playgroud)


Jas*_*git 2

感谢艾德卡,我现在知道了正确的答案。

Makefile 现在有这样的规则:

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

它有点长,但它所做的只是从 ghc 的输出中提取“Gcc 链接器标志”。 注意:ghc --info这是and的输出ghc +RTS --info

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

谢谢大家!