GCC无法识别Rust的.o文件格式 [英] Rust's .o file format not recognized by GCC

查看:391
本文介绍了GCC无法识别Rust的.o文件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MinGW以及GCC 4.9.3和Rust 1.9.0稳定版在C中构建一个简单的应用程序,该应用程序在Windows中调用Rust函数.这是源代码:

I'm trying to build a simple application in C that calls Rust functions, in Windows, using MinGW with GCC 4.9.3 and Rust 1.9.0 stable. Here's the source code:

test.rs

#![crate_type = "staticlib"]

#[no_mangle]
pub extern "C" fn get_number() -> isize {
    42 as isize
}

main.c

#include <stdio.h>

int get_number();

int main()
{
    printf("Hello, world!\n");
    printf("Number is %d.\n", get_number());
    return 0;
}

现在,我知道我应该在Rust和其他方面使用C兼容类型.但是在进入程序正确性之前,存在一个问题,即Rust似乎正在生成GCC无法理解的目标文件.这是我正在尝试的:

Now, I know I should use C-compatible types in Rust and whatnot. But before going into program correctness, there's a problem in that Rust seems to be generating object files that GCC doesn't understand. Here's what I'm trying:

rustc --emit obj test.rs
gcc -c main.c
gcc -static-libgcc test.o main.o -lmingw32 -o test.exe

但是链接器命令以以下结尾:

But the linker command terminates with:

test.o: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status

我已经搜索了会改变输出格式的Rust编译器指令,但是找不到任何指令.我已经阅读了Rust的FFI文档,但是没有提到这样的东西.通常,信息是关于从Rust调用C的信息.显然,由于语法不兼容,要求Rust生成ASM文件并与GCC的as组装是行不通的.

I've searched for Rust compiler directives that would change output format, but I couldn't find any. I've read Rust's FFI docs, but it doesn't mention anything like this. Usually, information is about calling C from Rust. Asking Rust to generate ASM files and assembling with GCC's as doesn't work, due to incompatible syntax, apparently.

这是Windows版Rust/GCC的兼容性问题吗?我可以做些什么来从Rust生成兼容的目标文件吗?或者更确切地说,我应该要求Rust生成什么样的输出才能使之成为可能?我也有兴趣在各种游戏机SDK上从C调用Rust代码.我需要什么样的设置才能最大程度地提高与其他链接程序的兼容性?

Is this a compatibility issue with the Windows version of Rust/GCC? Is there anything I can do to generate compatible object files from Rust? Or rather, what kind of output should I ask Rust to generate to make this possible? I'm also interested in calling Rust code from C on a variety of gaming console SDKs. What kind of setup would I need to maximize compatibility with other linkers?

推荐答案

对我来说很好:

$ rustc test.rs --emit=obj
$ gcc -c main.c
$ file test.o
test.o: 80386 COFF executable not stripped - version 30821
$ file main.o
main.o: 80386 COFF executable not stripped - version 30821
$ gcc test.o main.o -o awesome
$ file awesome.exe
awesome.exe: PE32 executable (console) Intel 80386, for MS Windows

$ ./awesome.exe
Hello, world!
Number is 42.

$ rustc --version --verbose
rustc 1.9.0 (e4e8b6668 2016-05-18)
binary: rustc
commit-hash: e4e8b666850a763fdf1c3c2c142856ab51e32779
commit-date: 2016-05-18
host: i686-pc-windows-gnu
release: 1.9.0
$ gcc --version
gcc (GCC) 5.3.0

这篇关于GCC无法识别Rust的.o文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆