gcc -l verlinkt mit einer Bibliotheksdatei.
gcc -L sucht im Verzeichnis nach Bibliotheksdateien.
$ gcc [options] [source files] [object files] [-Ldir] -llibname [-o outfile]
Link -l mit dem Bibliotheksnamen ohne das Präfix lib und die Erweiterungen .a oder .so .
Für die statische Bibliotheksdatei libmath. a use -lmath :
$ gcc -static myfile.c -lmath -o myfile
Für die gemeinsam genutzte Bibliotheksdatei libmath. Verwenden Sie also -lmath :
$ gcc myfile.c -lmath -o myfile
Datei1.c:
// file1.c
#include <stdio.h>
void main()
{
printf("main() run!\n");
myfunc();
}
Datei2.c:
// file2.c
#include <stdio.h>
void myfunc()
{
printf("myfunc() run!\n");
}
Erstellen Sie file2.c , kopieren Sie die Objektdatei file2.o in das Verzeichnis libs und archivieren Sie sie in der statischen Bibliothek libmylib.a :
$ gcc -c file2.c
$ mkdir libs
$ cp file2.o libs
$ cd libs
$ ar rcs libmylib.a file2.o
Erstellen Sie file1.c mit der statischen Bibliothek libmylib.a im libs - Verzeichnis.
Build ohne -L-Ergebnisse mit einem Fehler:
$ gcc file1.c -lmylib -o outfile
/usr/bin/ld: cannot find -llibs
collect2: ld returned 1 exit status
$
Mit -L bauen und ausführen:
$ gcc file1.c -Llibs -lmylib -o outfile
$ ./outfile
main() run!
myfunc() run!
$