GCC,静态库,外部汇编函数是不确定的符号 [英] gcc, static library, external assembly function becomes undefined symbol
问题描述
我有g ++的建设可链接到一个静态库,后者应包含写在外部ASM-文件的一些全局函数,用YASM编译的应用程序有问题。因此,在图书馆,我有
I have a problem with g++ building an application which links to a static library, where the latter shall contain some global functions written in external asm-files, compiled with yasm. So in the library, I have
#ifdef __cplusplus
extern "C" {
#endif
extern void __attribute__((cdecl)) interp1( char *pSrc );
extern void __attribute__((cdecl)) interp2( char *pSrc );
#ifdef __cplusplus
}
#endif
这是我在其他地方引用的库中。然后,有在ASM文件的实施,是这样的:
which I reference elsewhere inside the library. Then, there is the implementation in an asm-file, like this:
section .data
; (some data)
section .text
; (some text)
global _interp1
_interp1:
; (code ...)
ret
global _interp2
_interp2:
; (code ...)
ret
编译和链接做工精细的图书馆,我
Compiling and Linking work fine for the library, I do
yasm -f elf32 -O2 -o interp.o interp.asm
然后
ar -rc libInterp.a objs1.o [...] objsN.o interp.o
ranlib libInterp.a
现在终于到库链接到主应用程序,我
Now finally, to link the library to the main application, I do
g++ -O4 -ffast-math -DNDEBUG -fomit-frame-pointer -DARCH_X86 -fPIC -o ../bin/interp this.o that.o -lboost_thread -lpthread ./libInterp.a
和我得到的错误
undefined reference to `interp1'
undefined reference to `interp2'
我在做什么错在这里?任何帮助是AP preciated。
What am I doing wrong here? any help is appreciated.
推荐答案
根据不同的目标类型,GCC不会prePEND领先下划线到外部符号。看来,这是您的方案的情况。
Depending on the target type, gcc will not prepend a leading underscore to external symbols. It appears that this is the case in your scenario.
最简单的解决方法是可能去除的名字下划线在汇编文件。
The simple fix is probably to remove the underscores from the names in your assembly file.
您可能consder一对夫妇的选择可能是使用像在汇编文件中的符号以下宏之一:
A couple alternatives you might consder might be to use something like one of the following macros for your symbols in the assembly file:
; sym()
; Return the proper symbol name for the target ABI.
;
; Certain ABIs, notably MS COFF and Darwin MACH-O, require that symbols
; with C linkage be prefixed with an underscore.
;
%ifidn __OUTPUT_FORMAT__,elf32
%define sym(x) x
%elifidn __OUTPUT_FORMAT__,elf64
%define sym(x) x
%elifidn __OUTPUT_FORMAT__,x64
%define sym(x) x
%else
%define sym(x) _ %+ x
%endif
http://www.dcs.warwick.ac .UK /〜彼得/ otherstuff.html
%macro public_c_symbol 1
GLOBAL %1,_%1
%1:
_%1:
%endmacro
public_c_symbol my_external_proc:
; ...
RET
这篇关于GCC,静态库,外部汇编函数是不确定的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!