A pit GCC link library: dynamic inventory has prompted an undefined function dynamic library

background

Link Library has been specified in the GCC, but the compiler when prompted dynamic library function is undefined!

Test error appears as follows:

[GMPY@13:48 tmp]$gcc -o test -L. -lmylib test.c 
/tmp/ccysQZI3.o:在函数‘main’中:
test.c:(.text+0x1a):对‘func_lib’未定义的引用
collect2: error: ld returned 1 exit status

In the dynamic test library libmylib.sois a function definition func_libof

[GMPY@13:55 tmp]$cat mylib.c
#include <stdio.h>

int func_lib(void)
{
    printf("In share library\n");
    return 0;
}
[GMPY@13:56 tmp]$gcc -fPIC -shared mylib.c -o libmylib.so

GCC links pit

In compiled with gcc, we can use -Lthe specified link library location, with -lspecified.

man gccQuery I found such a description:

-llibrary
-l library
    ... ## 这里为了方便阅读,对原文进行了换行排版优化
    It makes a difference where in the command you write this option; 
    the linker searches and processes libraries and object files in the order they are specified. 
    Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. 
    If bar.o refers to functions in z, those functions may not be loaded.
    ...

Ah, this, then what does that mean? If the -llink library source code before, it will not link the library! !

Like the difference between the following two commands:

异常:gcc -o test -L. -lmylib test.c
正常:gcc -o test -L. test.c -lmylib

Even when the position of execution parameters are required, but also drunk

Guess you like

Origin www.cnblogs.com/gmpy/p/11089572.html