简体   繁体   English

链接库与 arm gcc

[英]Linking library with arm gcc

I'm getting an issue with my embedded c project.我的嵌入式 c 项目出现问题。

I'm trying to link libcrc to my project, but I'm getting an undefined reference error.我正在尝试将 libcrc 链接到我的项目,但出现未定义的引用错误。

I tried boiling the problem down to its simplest form and this is what I have:我尝试将问题简化为最简单的形式,这就是我所拥有的:

#include "checksum.h"
int main(void)
{
  char data[8];
  short a = crc_modbus(data, 8);
  while (1);
}

Then I run this command:然后我运行这个命令:

arm-none-eabi-gcc -mcpu=arm7tdmi --specs=nosys.specs -I../../../libraries/libcrc/include -o main ../src/main.c -L../../../libraries/libcrc/lib -lcrc

and I get the following error:我收到以下错误:

/var/folders/_8/_cw6l3nd335f_j6cn6z79m4m0000gn/T//ccL0dSgt.o: In function `main':
main.c:(.text+0x18): undefined reference to `crc_modbus'
collect2: error: ld returned 1 exit status

I read about how the flags' order matters, and I've played around with the order to no avail.我读到了旗帜的顺序是如何重要的,但我一直在玩这个顺序,但无济于事。

I've also tried running the following:我也尝试过运行以下命令:

gcc -I../../../libraries/libcrc/include ../src/main.c -lcrc -o main -L../../../libraries/libcrc/lib

And I got this as the output:我得到的是 output:

../src/main.c:5:24: warning: passing 'char [8]' to parameter of type 'const unsigned char *' converts between pointers to integer types with different sign [-Wpointer-sign]
  short a = crc_modbus(data, 8);
                       ^~~~
../../../libraries/libcrc/include/checksum.h:93:52: note: passing argument to parameter 'input_str' here
uint16_t                crc_modbus(         const unsigned char *input_str, size_t num_bytes       );
                                                                 ^
1 warning generated.

So it seems when I pass essentially the same command to gcc as to arm-gcc it works.因此,当我将与 arm-gcc 基本相同的命令传递给 gcc 时,它似乎可以工作。 What am I missing here?我在这里想念什么? What needs to be added arm gcc需要补充什么 arm gcc

From the comment that it works for gcc but it's not working for arm-gcc , it looks like you are using a cross-compiler and you are picking up the libraries for the host architecture instead of the libraries for the target architecture.从它适用于gcc但不适用于arm-gcc的评论来看,看起来您正在使用交叉编译器,并且您正在选择主机架构的库而不是目标架构的库。

When you are using a crosscospiler, you need to make sure that your toolchain environment is properly set, otherwise you will have lot's of problems with the build.当您使用交叉编译器时,您需要确保您的工具链环境设置正确,否则您将在构建时遇到很多问题。 Also you need to assure that the libraries which are available your build environment are build for the target architecture.您还需要确保您的构建环境可用的库是为目标架构构建的。 If not, the linker will find the library specified with -L , but inside the library there will be no symbols for your target architecture, so you will get error that the symbols you are using form that library are missing.如果没有,linker 将找到用-L指定的库,但在库内部将没有您的目标架构的符号,因此您将收到错误,即您使用的符号来自该库丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM