Makefile与共享库链接失败
Makefile Linking with shared library fails
我试图编写一个共享库,并试图将其链接以形成最终的可执行文件。
Makefile
mystring.out:main.c libmystring.so
gcc -I. -L/home/pradheep/myexploration/mystring/ -lmystring main.c -o mystring.out
libmystring.so:mystring.o
gcc -shared -Wl,-soname,libmystring.so -o libmystring.so mystring.o
libmystring.a:mystring.o
ar -r libmystring.a mystring.o
mystring.o:mystring.h mystring.c
gcc -Wall -g -c -fPIC -I. mystring.c
clean:
rm *.o
rm *.a
rm *.so
rm *.out
这是错误消息:
gcc -I. -L/home/pradheep/myexploration/mystring/ -lmystring main.c -o mystring.out
/tmp/ccS9UDPS.o: In function `main':
main.c:(.text+0x2d): undefined reference to `mystrcpy'
main.c:(.text+0x5a): undefined reference to `mystrncpy'
main.c:(.text+0x87): undefined reference to `mystrncpy'
main.c:(.text+0xa4): undefined reference to `mystrlen'
collect2: ld returned 1 exit status
make: *** [mystring.out] Error 1
我已经导出了LD_LIBRARY_PATH
echo $LD_LIBRARY_PATH
/home/pradheep/myexploration/mystring
我的libmystring.so 的输出
000004ca T mystrncpy
0000045c T mystrcpy
我错过了什么?
解决方案:
问题是库-l的使用顺序。它应该在源文件之后使用达雅尔赖指出的正确顺序是gcc-I-L/home/pradhep/myexploration/mystring/main.c-lmystring-o mystring.out欢呼吧,它起作用了。
链接器的传统行为是在命令行上指定的库中从左到右搜索外部函数。这意味着包含函数定义的库应该出现在任何使用它的源文件或对象文件之后
相关文章:
- mapreduce c编程与-fPIC链接失败,如何解决?
- 引用构造函数时链接失败
- 嵌入 python3.6 和 MinGW 在C++链接失败
- MAKE/CMAKE子目录链接到外部库的链接失败
- 与 CMAKE 的静态链接失败
- Android中的OpenCV 4.0.1链接失败
- 在静态库中链接失败,但链接共享库成功
- .so中存在的符号已编译.o引用了它,但链接失败
- 使用签证编译 dll / exe - 链接失败
- 在C++程序上使用mpicc链接失败
- 与iOS项目上的C 库链接失败
- C++链接失败-ld:找不到[makefile][gcc/cygwin]
- clang++链接失败:错误:源文件不是有效的UTF-8
- OpenCV:提供给imread()的链接失败
- 错误: /usr/bin/ld: 最终链接失败: 文件被截断
- 链接失败。如何使用'NtQuerySystemTime'窗口功能?
- 变量模板链接失败
- 克隆链接失败
- 安特尔和链接失败
- 最终链接失败:包含 windows.h 时参数无效