【问题标题】:Nim cross compilation to CNim 交叉编译为 C
【发布时间】:2015-04-29 06:04:48
【问题描述】:

我写了一个 Nim 程序,

echo("Hello.")

然后我尝试为 Linux 机器进行交叉编译,

nim c --cpu:i386 --os:linux -c hello.nim

这产生了以下输出:

config/nim.cfg(45, 2) Hint: added path: '/Users/connor/.babel/pkgs/' [Path]
config/nim.cfg(46, 2) Hint: added path: '/Users/connor/.nimble/pkgs/' [Path]
Hint: used config file '/usr/local/lib/nim-0.10.2/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: hello [Processing]
Hint: operation successful (8753 lines compiled; 0.140 sec total; 14.148MB)[SuccessX]

此时我切换到nimcache/目录并尝试执行:

gcc hello.c -o hello.o

但这给了我一个错误:

hello.c:5:10: fatal error: 'nimbase.h' file not found
#include "nimbase.h"
         ^
1 error generated.

我想,“没什么大不了的,我会找到 nimbase.h 并将其放到那里的 nimcache 目录中”,但之后我又遇到了一个新错误,

In file included from hello.c:5:
./nimbase.h:385:28: error: 'assert_numbits' declared as an array with a
      negative size
  ...sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8 ? 1 : -1];
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

我不确定我应该怎么做。我曾尝试使用--genScript 选项,但这导致了类似的错误。我正在运行 OS X Yosemite。

谢谢!

更新:

我不确定--cpu: 选项支持多少架构,但我在What makes Nim practical 博客文章中找到了(部分?)列表。我终于打电话了,

nim c --cpu:amd64 --os:linux -c hello.nim

这防止了我在我的 Linux 机器上编译时看到的错误。如果您使用的是 Linux 或 OS X,不确定您可以调用什么 CPU 架构,

less /proc/cpuinfo

【问题讨论】:

  • 文档说“生成的 C 代码与平台无关。例如,为 Linux 生成的 C 代码不能在 Windows 上编译。”和“将 C 代码和编译脚本 compile_myproject.sh 移动到你的 Linux i386 机器”。所以也许你不能在 OS X 上完成这个。
  • 哦,是的,我记得读过那个。我将nimcache(与nimbase.h)scp'd 到我的linux 机器上,并再次收到最后一个错误。我还缺少什么吗?

标签: c compilation nim-lang


【解决方案1】:

最后一个问题是因为您正在为 x86_64 架构运行 gcc,而源代码是为 i386 架构生成的。

【讨论】:

  • 是的。它静态地断言您在 32 位平台上。
  • 谢谢大家,我仍然无法编译,但这回答了我一直看到的错误的问题。使用nim c --cpu:amd64 --os:linux -c hello.nim 成功了。
【解决方案2】:

我在让nim 从 GNU/Linux 机器编译 Windows 可执行文件时遇到了同样的问题,所以我制作了一个 bash 脚本。它采用包含*.nim 源文件的目录的路径和要输出的可执行文件的名称。

我相信您可以换掉 GCC 编译器(在本例中为 MinGW)并根据需要更改 --os: 开关:

#!/usr/bin/env bash
# Nim must generate C sources only, to be fed to MingW
nim c --cpu:amd64 --os:windows --opt:speed --embedsrc --threads:on --checks:on -c -d:release $1/*.nim
# Copy nimbase.h so MingW32 can find it during compilation and linking
cp /opt/Nim/lib/nimbase.h $1/nimcache/nimbase.h
mkdir -p $1/bin
cd $1/nimcache && x86_64-w64-mingw32-gcc -save-temps $1/nimcache/*.c -o $1/bin/$2.exe
rm $1/nimcache/*.{i,s} # only care about *.o objects
ls -lAhF $1/nimcache
ls -lAhF $1/bin

【讨论】:

    猜你喜欢
    • 2012-12-28
    • 1970-01-01
    • 2021-09-14
    • 2016-01-15
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    相关资源
    最近更新 更多