简体   繁体   English

C文件中装配元素的未定义引用

[英]undefined reference of assembly element in c file

I have linkers errors(undefined reference) that I cannot solve by myself. 我有自己无法解决的链接器错误(未定义参考)。 I use GCC Sourcery G++ Lite 4.5.2 for ARM. 我将GCC Sourcery G ++ Lite 4.5.2用于ARM。

I have a lot of undefined reference and they almost all refer to assembly file. 我有很多未定义的引用,它们几乎都引用了程序集文件。 Here one example : 这里是一个例子:

I have "_CPU_MmuConfigGet" defined in an assembly file named cpu.sx : 我在名为cpu.sx的程序集文件中定义了“ _CPU_MmuConfigGet”:

.global _CPU_MmuConfigGet
CPU_MmuConfigGet: 
mrc p15,0,r0,c1,c0,0        
mov pc,lr

In ac file named mmu.c , CPU_MmuConfigGet is called : 在名为mmu.c的ac文件中,CPU_MmuConfigGet称为:

#include "cpu.h"
U32 MMU_ConfigGet(void) {
    return CPU_MmuConfigGet();
}

Finally, in the header file cpu.h, CPU_MmuConfigGet is declared : 最后,在头文件cpu.h中,声明CPU_MmuConfigGet:

extern U32 CPU_MmuConfigGet(void);

Theses tree files are located in the following folders : 论文树文件位于以下文件夹中:

Base/Common/src/mmu.c     
Base/Common/inc/cpu.h     
Base/Common/src/cpu.sx    

From different post I red on the net, I found that I should add an underline to CPU_MmuConfigGet (tried, but didn't solve my problem). 从网上我发红的其他帖子中,我发现我应该在CPU_MmuConfigGet上添加下划线(尝试过,但没有解决我的问题)。 I also red in some forum that , when linking, object file order are important, but in others forums, object order are not important (I'm confused here...). 在某些论坛中,我也提到,在链接时,目标文件的顺序很重要,但是在其他论坛中,对象的顺序并不重要(我在这里很困惑...)。 I tried the -S parameter to see the assembly version of mmu.c, but that didn't help me finding a clue about my error... 我尝试使用-S参数来查看mmu.c的汇编版本,但这并没有帮助我找到有关错误的线索...

Here's is the compiler command used (I have added -H in case this help finding what is wrong...) : 这是使用的编译器命令(如果有帮助,我添加了-H,以防出现问题……):

arm-none-eabi-gcc -c -g3 -gdwarf-2  -H -o"mmu.o" -Wall -Wa,-adhlns="mmu.o.lst" 
-fmessage-length=0 -MMD -MP -MF"mmu.d" -MT"mmu.d" -fpic  -march=armv4t -mcpu=arm7tdmi -mlittle-endian 
-I"../../OS/ngos/hw/cdb89712" -I"../../OS/ngos" -I"../../OS/ngos/include" -I"../../OS/ngos/rtos/ucosii"  
-I"C:/Program Files/CodeSourcery/Sourcery G++ Lite/lib/gcc/arm-none-eabi/4.5.2/include" -I"./"  
-I"src/" -I"../../Common/inc" -I"../../OS/uCOS-II/SOURCE" -I"../../OS/ngos/drivers/arm" 
-I"../../OS/ngos/include/ngos" -I"../../OS/ngip/include" -I"../../OS/ngip/include/ngip" 
-I"../../Dvcscomponent/Inc" -I"../../Inc"  "../../Common/src/mmu.c"
. ../../Common/inc/base.h
. ../../Common/inc/hw7312.h
. ../../Common/inc/serial.h
.. ../../Common/inc/base.h
.. ../../Common/inc/hw7312.h
. ../../Common/inc/base.h
. ../../Common/inc/cpu.h
.. ../../Common/inc/base.h
.. ../../Common/inc/hw7312.h
. ../../Common/inc/mmu.h

Now the assembly command : 现在,汇编命令:

arm-none-eabi-gcc -g3 -gdwarf-2  -x assembler-with-cpp -Wa,-adhlns="cpu.o.lst" -Wall -c 
-fmessage-length=0 -MMD -MP -MF"cpu.d" -MT"cpu.d" -fpic -o"cpu.o" -march=armv4t  -mcpu=arm7tdmi -mlittle-endian 
-I"../../OS/ngos/hw/cdb89712" -I"../../Common" -I"../../OS/ngos/drivers/arm" 
"../../Common/src/cpu.sx"

Finally the linking command with the error : 最后链接命令出现错误:

arm-none-eabi-gcc -fpic -mcpu=arm7tdmi -T".\linker.ld" -Wl,-Map,BootLoad.map -g3 -gdwarf-2  -o "BootLoad.elf"  
InitMain.o tsk_main.o ecp.o memalloc.o tsk_ecp.o firmdesc.o crc.o flash.o eth.o firmflash.o 
firmdest.o bcfg.o bootdownload.o cinit.o serial.o cpu.o mmu.o  ngucos.o cdbini.o cs712sio.o  
cs712eth.o  ../../OS/ngos/lib/rtstub/arm/gcc/libngosd4m32l.a ../../OS/ngip/lib/rtstub/arm/gcc/libngipd4m32l.a 
mmu.o: In function `MMU_ConfigGet':
C:\Working\SF2100-0074-BootLoaderMezz\Base\NexGen\BootLoader/../../Common/src/mmu.c:28: undefined reference to `CPU_MmuConfigGet'

I someone have any suggestions, I would be pleased to hear them! 我有人有任何建议,我很高兴听到他们的建议!

Thanks in advance! 提前致谢!

Finally, after consulting the listing of cpu.sx, I was surprised that a part of the file was "missing". 最后,在查询了cpu.sx的列表之后,我惊讶于文件的一部分“丢失”。 I realized that the file had an include of another assembly file. 我意识到该文件包含另一个程序集文件。 This included assembly file was ended... by a 该包含的程序集文件以...结尾

.end

directive. 指示。

I removed this directive and the project compiled. 我删除了该指令,并编译了项目。

The problem appears to be that your source has a leading underscore in some places, but not in others. 问题似乎是您的来源在某些地方带有下划线,而在其他地方则没有。 You don't need them with that toolchain, although you can, if you like, but it has to be consistent. 不需要使用该工具链,尽管可以(如果愿意),但是必须保持一致。

Even better, perhaps you should ditch the little assembler files altogether, and use assembler inserts in C files? 更好的是,也许您应该完全放弃小汇编程序文件,并在C文件中使用汇编程序插入?

Eg 例如

int CPU_MmuConfigGet ()
{
  int config;
  asm ("mrc p15,0,%0,c1,c0,0" : "=r" (config));
  return config;
}

This little function hides all that linker nonsense, cleans up your project tree, and your makefiles, and the compiler is even free to inline it into other functions if it can. 这个小功能隐藏了所有不必要的链接器,清理了项目树和makefile,并且编译器甚至可以随意将其内联到其他函数中。

The compiler manual has full details of now to specify inputs and outputs to assember inserts. 编译器手册现在提供了完整的详细信息,以指定对assmber插入的输入和输出。 They're a little bit confusing at first, but worth it in the long run, I find. 我发现它们一开始有点令人困惑,但从长远来看还是值得的。

Of course, there some places where an assembler file is still necessary - crt0.s, for example. 当然,在某些地方仍然需要汇编程序文件,例如crt0.s。

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

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