riscv/gcc/ld-“未定义对printf的引用"使用自己的脚本进行链接 [英] riscv/gcc/ld - "Undefined reference to printf" using own script to link

查看:944
本文介绍了riscv/gcc/ld-“未定义对printf的引用"使用自己的脚本进行链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在学习RISC-V,使用RISC-V工具链,并为我的嵌入式系统编辑新的ld脚本.我写了一个例子,并编译以观看操作码.

Currently, I'm learning RISC-V, use the RISC-V toolchain, and edit a new ld script for my embedded. I write a example, and compile to watch the opcode.

示例:

#include <stdio.h> //float.c

int main()
{
float a=1.04;
printf("a=%f\n",a);
return 0;
}

我的步骤是:

1. riscv64-unknown-elf-gcc -S float.c  *//generate assembly code*

2. riscv64-unknown-elf-as float.s -o float.o  *//generate obj file*

3. riscv64-unknown-elf-ld -T elf64lriscv1.x float.o *//use own script to link, -T is using other script*

然后将显示"float.c :(.text + 0x50):对`printf'的未定义引用

我正在尝试

添加-lc参数,但不起作用,它将显示更多未定义的消息.

Add -lc parameter, but doesn't working, it will show more undefined message.

我的ld脚本

OUTPUT_FORMAT("elf64-littleriscv", "elf64-littleriscv","elf64-littleriscv")
OUTPUT_ARCH(riscv)
ENTRY(_start)
SEARCH_DIR("/path/to/install/riscv/toolchain/riscv64-unknow-elf/lib");
/*MEMORY{  //for my embedded allocation
  flash : org = 0x0, l = 0x10000
  ram : org= 0x10000, l = 512
}*/
SECTIONS{
_start =0x10000;
.text :
{
  *(.text)
}
.bss :
{
  *(.bss)
  *(.sbss)
}}

此外,我正在尝试使用默认脚本,例如此命令:

Also, i'm trying to use the default script, like this command:

$ riscv-unknown-elf-ld float.o -o float

但这是相同的结果. 请帮助我!

but it is same result.... please help me!

致谢!

推荐答案

printf提供C标准库(并且很难实现).您需要链接它(也许通过在riscv-unknown-elf-ld命令中添加-lc或提供该库的完整路径)

printf is provided by the C standard library (and is very difficult to implement). You need to link it (perhaps by adding -lc to your riscv-unknown-elf-ld command, or by giving the complete path of that library)

您可以将其他选项传递给您的可能是-M-L search-dir --trace--verbose等.还请仔细阅读有关

You might pass additional options to your riscv-unknown-elf-ld perhaps -M, -L search-dir, --trace, --verbose, etc. Read also more carefully the chapter about ld scripts.

由于要交叉编译,因此可能需要从源代码中交叉编译某些libc.

Since you are cross-compiling, you might need to cross-compile some libc from source code.

您需要花费更多的时间来了解链接器 ELF 对象文件和可执行文件格式(另请参见>链接器和加载器

You need to spend more time understanding the behavior of linkers and ELF object files and executable format (see also elf(5)). Consider reading Linkers and Loaders

您可以使用其他交叉 binutils 程序(例如交叉-objdumpnmreadelf等...)以浏览相关的目标文件.

You could use other cross- binutils programs (like cross- objdump, nm, readelf etc...) to explore the relevant object files.

如果您要为裸机系统进行编码,则可能需要以独立模式进行编译(免费软件 C库可能会激发您的灵感(但是您需要找到一个或工作数月才能获得开发一些等效的东西.

If you are coding for a bare metal system, you might want to compile in freestanding mode (passing -ffreestanding to your GCC) and provide so implement your own printf (or other output) function. Existing free software C libraries could inspire you (but you need to find one or work many months to develop some equivalent).

我还建议您阅读有关操作系统的信息,例如 操作系统:三个简单的部分 (自操作系统概念与您相关,因为裸机上的嵌入式系统与操作系统共享功能. OSDEV Wiki 可能也有帮助(但与RISC-V无关).

I recommend also reading about OSes, e.g. Operating Systems: Three Easy Pieces (since the OS concepts are relevant to you, because an embedded system on the bare metal share features with OSes). OSDEV wiki might also be helpful (but is not about RISC-V).

您可能需要几个月的工作(甚至是几年),因此要适当地预算一下.

You could need several months of work (or even years), so budget it appropriately.

顺便说一句,令我惊讶的是,您在示例中使用了float.浮点数很难.参见 floating-point-gui.de ;第一次尝试时,我会考虑仅使用整数.

BTW, I am surprized that you use a float in your example. Floating point is difficult. See floating-point-gui.de ; For a first try, I would consider using integer only.

这篇关于riscv/gcc/ld-“未定义对printf的引用"使用自己的脚本进行链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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