【发布时间】:2017-08-16 14:30:09
【问题描述】:
我有一个问题:我有 2 个库(一个用 NASM 编译的 ASM 中的静态库和一个用 GCC 编译的 C 中的动态库)。
我首先在 ASM 中使用以下 Makefile 编译一个(我删除了部分以使其更具可读性):
ASM = nasm
NAME = libasmlib.a
SRC = [...all .asm files...]
OBJ = $(SRC:.asm=.o)
FLAGS = -f elf64 -g
all : $(NAME)
$(NAME) : $(OBJ)
ar rc $(NAME) $(OBJ)
ranlib $(NAME)
%.o : %.asm
$(ASM) $(FLAGS) -o $@ $<
然后我编译动态库,以便它使用静态的功能:
CC = gcc
NAMEDYN = libclib.so
SRC = [...all .c files...]
OBJ = $(SRC:%.c=%.o)
CFLAGS = -W -Wall -Werror -pedantic -fPIC
LDFLAGS = -L./libs/ASM -lasmlib
$(NAME) : $(OBJ)
$(CC) $(LDFLAGS) -shared -o $(NAMEDYN) $(OBJ)
all : $(NAME)
我没问题,一切都完美编译,但是当我使用以下 .c 测试代码时(使用 gcc maindyn.c -ldl):
#include <stdio.h>
#include <dlfcn.h>
int main(int ac, char **av)
{
int res;
void *handle;
int (*c_function)(char *str);
if (!(handle = dlopen("./libclib.so", RTLD_LAZY | RTLD_GLOBAL | RTLD_NOW)))
return 1;
c_function = dlsym(handle, "c_function");
res = c_function("Hi!");
printf("%d\n", res);
[...]
}
我收到此错误:
./a.out:符号查找错误:./libclib.so:未定义符号:asm_function
动态库上的纳米:
U asm_function
0000000000202078 B __bss_start
0000000000202078 b completed.7558
<snip>
0000000000000ee0 T c_function
0000000000000e20 t register_tm_clones
U __stack_chk_fail@@GLIBC_2.4
0000000000202078 d __TMC_END__
静态库上的纳米:
asm_function.o:
0000000000000000 T asm_function
000000000000001c t _end
0000000000000021 t _finded
0000000000000008 t _loop
【问题讨论】:
-
您能否在您的
libclib.so文件上调用nm并在此处发布结果。 -
U asm_function:asm_function被引用但未定义。 -
我的
asm_function在静态库中。 -
你能
nm静态库和edit你的问题吗? -
改变了。
标签: c gcc assembly makefile shared-libraries