-I,添加包含路径(大写i)
-I 在编译时用,告诉编译器去哪个路径下找文件
如:-I /home/hello/include
表示将/home/hello/include目录作为第一个寻找头文件的目录。
编译器的寻找顺序是:/home/hello/include-->/usr/include-->/usr/local/include。如果在/home/hello/include中有个文件hello.h,则在程序中用#include<hello.h>就能引用到这个文件。
可以加多个包含路径,编译器的寻找顺序为添加的顺序。
-l,添加引用链接库(小写L)
-l 在链接时用到,它的作用是告诉链接器,要用到哪个库。
如:-l pthread
告诉链接器(linker),程序需要链接pthread这个库,这里的pthread是库名不是文件名,具体来说文件句是libpthread.so。
-L,添加链接库路径
-L 后跟路径,告诉链接器从哪找库(.so文件),只有在链接时会用到。
如:-L /home/hello/lib
表示将/home/hello/lib目录作为第一个寻找库文件的目录,寻找顺序是:/home/hello/lib-->/usr/lib-->/usr/local/lib。
可以加多个包含路径,链接器的寻找顺序为添加的顺序。
-Wl,rpath,添加运行时库路径
-Wl:rpath 后面也是路径,运行的时候用。这条编译指令会在编译时记录到target文件中,所以编译之后的target文件在执行时会按这里给出的路径去找库文件。
如:-Wl:rpath=/home/hello/lib
表示将/home/hello/lib目录作为程序运行时第一个寻找库文件的目录,程序寻找顺序是:/home/hello/lib-->/usr/lib-->/usr/local/lib。
可以加多个包含路径,程序在运行时的寻找顺序为添加的顺序。