Chinaunix

标题: 编译源码执行没问题,源码编译成动态库加载执行内存出错 [打印本页]

作者: 沉水之鱼    时间: 2019-05-15 11:29
标题: 编译源码执行没问题,源码编译成动态库加载执行内存出错
本帖最后由 沉水之鱼 于 2019-05-15 11:39 编辑

   RT, 目前对外交付一个项目,因为一些核心源码不能对外开放,将对应的源码编译成.so进行链接,传进去的多级指针出现了如下问题:
1. 用源码编译执行的时候,传入这段函数之前和传入之后的多级指针打印地址相同,没有问题, 程序能正确执行。
2. 用源码编译的.so链接后,传入这段函数之前和传入之后的多级指针打印地址不一样,程序不能正确执行。
相关makefile如下

  1. copybase:= cp $(CURDIR)/libbase/libbase.so $(bin_dir)/libs/

  2. define compile_libbase
  3.         $(CR_CC) -c $(CURDIR)/libbase/lib_base.c -o $(bin_dir)/$(EXE)/lib$@/lib_base.o $(LOCAL_CFLAGS)
  4.         $(CR_CC) -shared -o $(bin_dir)/$(EXE)/libbase.so $(bin_dir)/$(EXE)/lib$@/*.o
  5. endef

  6. $(EXE): clean checkdir base
  7.         $(Q) echo "DONE"

  8. base:
  9.         mkdir -p $(bin_dir)/$(EXE)/lib$@
  10. ifeq ($(CURDIR)/libbase/lib_base.c, $(wildcard $(CURDIR)/libbase/lib_base.c))               
  11.         $(call compile_libbase)
  12. else
  13.         $(copybase)
  14. endif        
  15.         echo "DONE LIBBASE"        
复制代码


有没有大神遇到这种情况?


作者: mymbsd    时间: 2019-11-04 09:28
如果使用GCC, 在定义全局变量的时候如果不小心,使用动态链接库的时候确实会出现奇怪的现象
  1. $ cat main.c
  2. #include <stdio.h>

  3. void foo();
  4. void bar();

  5. int
  6. main(int argc, char *argv[])
  7. {
  8.         foo();
  9.         bar();

  10.         return 0;
  11. }
  12. $ cat foo.c
  13. #include <stdio.h>

  14. int x;
  15. int y = 2;

  16. void
  17. foo(void)
  18. {
  19.          printf("x=%d,y=%d\n", x, y);
  20. }
  21. $ cat bar.c
  22. #include <stdio.h>

  23. int x = 1;
  24. int y;

  25. void
  26. bar(void)
  27. {
  28.          printf("x=%d,y=%d\n", x, y);
  29. }
  30. $ make
  31. cc    -c -o main.o main.c
  32. cc    -c -o foo.o foo.c
  33. cc    -c -o bar.o bar.c
  34. cc -o demo1 main.o foo.o bar.o
  35. cc -o demo2 main.o -L. -lfoo -lbar
  36. cc -o demo3 main.o -L. -lbar -lfoo
  37. $ ./demo1; ./demo2; ./demo3
  38. x=1,y=2
  39. x=1,y=2
  40. x=0,y=2
  41. x=0,y=2
  42. x=1,y=0
  43. x=1,y=0
  44. $
复制代码

作者: mymbsd    时间: 2019-11-04 09:29
回复 2# mymbsd

希望对楼主有用





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2