Makefile与共享库的链接失败

Pra*_*eep 5 c c++ makefile

我试图编写一个共享库并尝试将其链接以形成最终的可执行文件.

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
Run Code Online (Sandbox Code Playgroud)

这是错误消息:

 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
Run Code Online (Sandbox Code Playgroud)

我已经出口了 LD_LIBRARY_PATH

echo $LD_LIBRARY_PATH
/home/pradheep/myexploration/mystring
Run Code Online (Sandbox Code Playgroud)

我的libmystring.so的输出

 000004ca T mystrncpy
 0000045c T mystrcpy
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

解:

问题是库-l使用的顺序.它应该在源文件之后使用,并且Dayal rai指出的正确顺序是gcc -I.-L/home/pradheep/myexploration/mystring/main.c -lmystring -o mystring.out并且它可以运行.

Day*_*rai 5

链接器的传统行为是在命令行中指定的库中从左到右搜索外部函数.这意味着包含函数定义的库应该出现在使用它的任何源文件或目标文件之后.