gcc编译程序时,可能会用到“-I”(大写i),“-L”(大写l),“-l”(小写l)等参数
例1:
gcc -o e e.c -I /usr/local/include/freetype2 -lfreetype -lm
-I /usr/local/include/freetype2
表示将/usr/local/include/freetype2
作为第一个寻找头文件的目录
-lfreetype
,-l (小写的l)
参数就是用来指定程序要链接的库,-l
参数紧接着就是库名。指定程序链接的库名是freetype
-lm
表示程序指定的链接库名是m (math数学库)
例2:
gcc -o hello hello.c -I /home/hello/include -L /home/hello/lib -lworld
-I /home/hello/include
表示将/home/hello/include
目录作为第一个寻找头文件的目录,寻找的顺序是:/home/hello/include-->/usr/include-->/usr/local/include
-lworld
表示在上面的lib的路径中寻找libworld.so动态库文件(如果gcc编译选项中加入了“-static”表示寻找libworld.a
静态库文件),程序链接的库名是world
-l
和-L
的区别:
-l
参数就是用来指定程序要链接的库,库名是m,则库文件名是libm.so,把库文件名的头lib和尾.so去掉就是库名了。
比如我们自已要用到一个第三方提供的库名字叫libtest.so
,那么我们只要把libtest.so
拷贝到/usr/lib
里,编译时加上-ltest
参数,我们就能用上libtest.so
库了(当然要用libtest.so库里的函数,我们还需要与libtest.so配套的头文件)。
放在/lib和/usr/lib和/usr/local/lib里的库直接用-l参数就能链接了
如果不在/lib和/usr/lib和/usr/local/lib里面,那么就要-L /aaa/bbb
指明目录(当然最好用.
)
网友评论