动态库 Segmentation fault
本帖最后由 shihyu 于 2016-05-19 09:46 编辑int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
#include <stdio.h>
int a = 100;
int b = 200;
int main(int argc, char *argv[])
{
printf("add=%d\n",add(a,b));
return 0;
}
CC = gcc
SOFLAGS = -shared
LDFLAGS = -lm
CFLAGS = -Wall -pedantic -ggdb3 -O0 -std=c99 -lpthread -fPIC -shared
ADD_OBJS = add.o
SUB_OBJS = sub.o
all: add.so sub.so
${CC}-o mainmain.c -L -ladd -L -lsub ${CFLAGS} ${LDFLAGS}
add.so: ${ADD_OBJS}
$(CC) ${SOFLAGS} -o $@ $?
sub.so: ${SUB_OBJS}
$(CC) ${SOFLAGS} -o $@ $?
.PHONY: clean
clean:
rm -f main ${ADD_OBJS} ${SUB_OBJS}
./main
Segmentation fault (core dumped)
请问一下这Makefile 编译出来的执行文件为什么会 Segmentation fault?
这个问题。。。撸主,这个跟 makefile 有关系的可能不大。除非你引用外部库的版本变了。。。。当然,库比较傻逼的时候会有这种问题。
所以,上代码。
就这俩接口,谁要是能看出来断错误的原因,那就厉害了。 哎呀卧槽,发帖回复还能被删除? 把CFLAGS中的选项-fPIC -shared移到SOFLAGS中试试(删去原SOFLAGS中的-shared) 本帖最后由 shihyu 于 2016-05-19 09:50 编辑
爻易 发表于 2016-05-19 09:41 static/image/common/back.gif
把CFLAGS中的选项-fPIC -shared移到SOFLAGS中试试(删去原SOFLAGS中的-shared)
删去原SOFLAGS中的-sharedgcc -Wall -pedantic -ggdb3 -O0 -std=c99 -lpthread -fPIC -shared -c -o add.o add.c
gcc-o add.so add.o
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): 未定義參考到「main」
collect2: error: ld returned 1 exit status
make: *** Error 1 本帖最后由 VIP_fuck 于 2016-05-19 09:52 编辑
要不先不用 makefile,用一个 gcc 命令去做,然后再排查是 makefile 问题还是别的问题。这么排查东西多,比较费时吧。
恩,,没有看到底写的是啥,纯粹凭感觉。 靠,没注意你居然编译动态库用缺省规则:dizzy:
SOFLAGS再加上-c选项
删去ADD_OBJS = add.o 与SUB_OBJS = sub.o
add.so: ${ADD_OBJS}替换成 add.so: add.c
sub.so: ${SUB_OBJS}替换成 sub.so: sub.c 这个makefile是谁写(抄)的,直接拉出去打死:mrgreen: 爻易 发表于 2016-05-19 10:00 static/image/common/back.gif
靠,没注意你居然编译动态库用缺省规则
SOFLAGS再加上-c选项
-c不用加了,SOFLAGS最后就是-shared -fPIC shihyu 发表于 2016-05-19 09:49 static/image/common/back.gif
删去原SOFLAGS中的-shared
不光是删,还有移呢?
页:
[1]
2