问题描述
我正在为 x86 ubuntu 机器上的 raspberry pi ARM 目标编写一些代码.我正在使用 gcc-linaro-armhf 工具链.我能够在 pi 上交叉编译和运行一些独立的程序.现在,我想将我的代码与外部库(例如 ncurses)链接起来.我怎样才能做到这一点.
I am writing some code for raspberry pi ARM target on x86 ubuntu machine. I am using the gcc-linaro-armhf toolchain. I am able to cross compile and run some independent programs on pi. Now, I want to link my code with external library such as ncurses. How can I achieve this.
我是否应该将我的程序与主机上现有的 ncurses 库链接,然后在 ARM 上运行?(我认为这行不通)我是否需要为 arm 获取 lib 的源代码或预构建版本,将其放入我的 lib 路径中,然后进行编译?
Should I just link my program with the existing ncurses lib on host machine and then run on ARM? (I don't think this will work)Do I need to get source or prebuilt version of lib for arm, put it in my lib path and then compile?
这种情况下的最佳做法是什么?
What is the best practice in this kind of situation?
我也想知道它对 c stdlib 是如何工作的.在我的程序中,我使用了 stdio 函数,它在交叉编译后工作,没有做任何特殊的事情.我只是在 makefile 中为我的 arm gcc 提供了路径.所以,我想知道,它是如何获得正确的 std 标头和库的?
I also want to know how it works for the c stdlib. In my program I used the stdio functions and it worked after cross compiling without doing anything special. I just provided path for my arm gcc in makefile. So, I want to know, how it got correct std headers and libs?
推荐答案
关于您的一般问题:
C 库的工作原理:
C 库是您的交叉工具链的一部分.这就是为什么找到标题并且程序正确链接和运行的原因.对于其他一些非常基本的系统库,如 libm 和 libstdc++(并非在所有情况下,取决于工具链配置)也是如此.
The C library is part of your cross toolchain. That's why the headers are found and the program correctly links and runs. This is also true for some other very basic system libraries like libm and libstdc++ (not in every case, depends on the toolchain configuration).
一般来说,在处理交叉开发时,您需要某种方式来交叉编译所需的库.在这种情况下使用二进制文件是非常罕见的.也就是说,尤其是对于 ARM 硬件,因为有很多不同的配置,而且通常所有东西都以不同的方式进行了大量精简.这就是为什么二进制文件在不同设备和 Linux 配置之间不太兼容的原因.
In general when dealing with cross-development you need some way to get your desired libraries cross-compiled. Using binaries in this case is very rare. That is, especially with ARM hardware, because there are so many different configurations and often everything is stripped down much in different ways. That's why binaries are not very much binary compatible between different devices and Linux configurations.
如果您在 Raspberry Pi 上运行 Ubuntu,那么您可能会在 Internet 上甚至某些 Ubuntu apt 存储库中找到合适的 ncurses 库.然而,典型的方法是使用您拥有的特定工具链交叉编译库.
If you're running Ubuntu on the Raspberry Pi then there is a chance that you may find a suitable ncurses library on the internet or even in some Ubuntu apt repository. The typical way, however, will be to cross compile the library with the specific toolchain you have got.
在需要交叉编译大量复杂库的情况下,有一些解决方案可以让生活变得更轻松,例如 buildroot 或 ptxdist.这些程序为嵌入式设备构建完整的 Linux 内核和根文件系统.
In cases when a lot and complex libraries need to be cross-compiled there are solutions that make life a bit easier like buildroot or ptxdist. These programs build complete Linux kernels and root file systems for embedded devices.
但是,就您而言,只要您只需要 ncurses,您就可以自己编译源代码.您只需要下载源代码,运行 configure
,同时使用 --host
选项指定您的工具链.--prefix
选项将选择安装目录.运行 make
和 make install
后,考虑到一切正常,您将获得一组头文件和 ARM 编译的库供您的应用程序链接.
In your case, however, as long as you only want ncurses you can compile the source code yourself. You just need to download the sources, run configure
while specifying your toolchain using the --host
option. The --prefix
option will choose the installation directory. After running make
and make install
, considering everything went fine, you will have got a set of headers and the ARM-compiled library for your application to link against.
关于交叉编译,您肯定会在互联网上找到大量信息,也许 ncurses 在其附带的文档中也有一些指示.
Regarding cross compilation you will surely find loads of information on the internet and maybe ncurses has got some pointers in its shipped documentation, too.
这篇关于交叉编译时如何使用外部库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!