gcc编译工具使用记录
- 前言
- gcc编译过程
- 代码文件示例
- 直接编译出可执行文件
- 分步编译
- 预编译生成.i预编译文件
- 编译生成.s文件
- 汇编生成.o文件
- 链接生成可执行文件
- 链接库
- 动态编译
- 生成动态链接库.so
- 静态编译
- 生成静态链接库.a
- 编译时指定链接库
- 运行时指定链接库
- 指定包含文件
前言
其实现在基本上不直接使用gcc/g++来编译了,因为工程中涉及的包、文件很多,用cmake比较方便。
但是记录一下gcc的使用,对于编译过程的理解还是有好处的。
注意:在使用上gcc和g++区别不大,g++在链接时会自动链接STL库但gcc不会。
gcc编译过程
如下图所见,包含预编译、编译、汇编和链接四个阶段。
#mermaid-svg-LmRAqPhyoq6vyCqt .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .label text{fill:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .node rect,#mermaid-svg-LmRAqPhyoq6vyCqt .node circle,#mermaid-svg-LmRAqPhyoq6vyCqt .node ellipse,#mermaid-svg-LmRAqPhyoq6vyCqt .node polygon,#mermaid-svg-LmRAqPhyoq6vyCqt .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-LmRAqPhyoq6vyCqt .node .label{text-align:center;fill:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .node.clickable{cursor:pointer}#mermaid-svg-LmRAqPhyoq6vyCqt .arrowheadPath{fill:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-LmRAqPhyoq6vyCqt .flowchart-link{stroke:#333;fill:none}#mermaid-svg-LmRAqPhyoq6vyCqt .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-LmRAqPhyoq6vyCqt .edgeLabel rect{opacity:0.9}#mermaid-svg-LmRAqPhyoq6vyCqt .edgeLabel span{color:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-LmRAqPhyoq6vyCqt .cluster text{fill:#333}#mermaid-svg-LmRAqPhyoq6vyCqt div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-LmRAqPhyoq6vyCqt .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-LmRAqPhyoq6vyCqt text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-LmRAqPhyoq6vyCqt .actor-line{stroke:grey}#mermaid-svg-LmRAqPhyoq6vyCqt .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-LmRAqPhyoq6vyCqt #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .sequenceNumber{fill:#fff}#mermaid-svg-LmRAqPhyoq6vyCqt #sequencenumber{fill:#333}#mermaid-svg-LmRAqPhyoq6vyCqt #crosshead path{fill:#333;stroke:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .messageText{fill:#333;stroke:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-LmRAqPhyoq6vyCqt .labelText,#mermaid-svg-LmRAqPhyoq6vyCqt .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-LmRAqPhyoq6vyCqt .loopText,#mermaid-svg-LmRAqPhyoq6vyCqt .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-LmRAqPhyoq6vyCqt .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-LmRAqPhyoq6vyCqt .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-LmRAqPhyoq6vyCqt .noteText,#mermaid-svg-LmRAqPhyoq6vyCqt .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-LmRAqPhyoq6vyCqt .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-LmRAqPhyoq6vyCqt .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-LmRAqPhyoq6vyCqt .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-LmRAqPhyoq6vyCqt .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LmRAqPhyoq6vyCqt .section{stroke:none;opacity:0.2}#mermaid-svg-LmRAqPhyoq6vyCqt .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-LmRAqPhyoq6vyCqt .section2{fill:#fff400}#mermaid-svg-LmRAqPhyoq6vyCqt .section1,#mermaid-svg-LmRAqPhyoq6vyCqt .section3{fill:#fff;opacity:0.2}#mermaid-svg-LmRAqPhyoq6vyCqt .sectionTitle0{fill:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .sectionTitle1{fill:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .sectionTitle2{fill:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .sectionTitle3{fill:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LmRAqPhyoq6vyCqt .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-LmRAqPhyoq6vyCqt .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LmRAqPhyoq6vyCqt .grid path{stroke-width:0}#mermaid-svg-LmRAqPhyoq6vyCqt .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-LmRAqPhyoq6vyCqt .task{stroke-width:2}#mermaid-svg-LmRAqPhyoq6vyCqt .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LmRAqPhyoq6vyCqt .taskText:not([font-size]){font-size:11px}#mermaid-svg-LmRAqPhyoq6vyCqt .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LmRAqPhyoq6vyCqt .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-LmRAqPhyoq6vyCqt .task.clickable{cursor:pointer}#mermaid-svg-LmRAqPhyoq6vyCqt .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-LmRAqPhyoq6vyCqt .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-LmRAqPhyoq6vyCqt .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-LmRAqPhyoq6vyCqt .taskText0,#mermaid-svg-LmRAqPhyoq6vyCqt .taskText1,#mermaid-svg-LmRAqPhyoq6vyCqt .taskText2,#mermaid-svg-LmRAqPhyoq6vyCqt .taskText3{fill:#fff}#mermaid-svg-LmRAqPhyoq6vyCqt .task0,#mermaid-svg-LmRAqPhyoq6vyCqt .task1,#mermaid-svg-LmRAqPhyoq6vyCqt .task2,#mermaid-svg-LmRAqPhyoq6vyCqt .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-LmRAqPhyoq6vyCqt .taskTextOutside0,#mermaid-svg-LmRAqPhyoq6vyCqt .taskTextOutside2{fill:#000}#mermaid-svg-LmRAqPhyoq6vyCqt .taskTextOutside1,#mermaid-svg-LmRAqPhyoq6vyCqt .taskTextOutside3{fill:#000}#mermaid-svg-LmRAqPhyoq6vyCqt .active0,#mermaid-svg-LmRAqPhyoq6vyCqt .active1,#mermaid-svg-LmRAqPhyoq6vyCqt .active2,#mermaid-svg-LmRAqPhyoq6vyCqt .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-LmRAqPhyoq6vyCqt .activeText0,#mermaid-svg-LmRAqPhyoq6vyCqt .activeText1,#mermaid-svg-LmRAqPhyoq6vyCqt .activeText2,#mermaid-svg-LmRAqPhyoq6vyCqt .activeText3{fill:#000 !important}#mermaid-svg-LmRAqPhyoq6vyCqt .done0,#mermaid-svg-LmRAqPhyoq6vyCqt .done1,#mermaid-svg-LmRAqPhyoq6vyCqt .done2,#mermaid-svg-LmRAqPhyoq6vyCqt .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-LmRAqPhyoq6vyCqt .doneText0,#mermaid-svg-LmRAqPhyoq6vyCqt .doneText1,#mermaid-svg-LmRAqPhyoq6vyCqt .doneText2,#mermaid-svg-LmRAqPhyoq6vyCqt .doneText3{fill:#000 !important}#mermaid-svg-LmRAqPhyoq6vyCqt .crit0,#mermaid-svg-LmRAqPhyoq6vyCqt .crit1,#mermaid-svg-LmRAqPhyoq6vyCqt .crit2,#mermaid-svg-LmRAqPhyoq6vyCqt .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-LmRAqPhyoq6vyCqt .activeCrit0,#mermaid-svg-LmRAqPhyoq6vyCqt .activeCrit1,#mermaid-svg-LmRAqPhyoq6vyCqt .activeCrit2,#mermaid-svg-LmRAqPhyoq6vyCqt .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-LmRAqPhyoq6vyCqt .doneCrit0,#mermaid-svg-LmRAqPhyoq6vyCqt .doneCrit1,#mermaid-svg-LmRAqPhyoq6vyCqt .doneCrit2,#mermaid-svg-LmRAqPhyoq6vyCqt .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-LmRAqPhyoq6vyCqt .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-LmRAqPhyoq6vyCqt .milestoneText{font-style:italic}#mermaid-svg-LmRAqPhyoq6vyCqt .doneCritText0,#mermaid-svg-LmRAqPhyoq6vyCqt .doneCritText1,#mermaid-svg-LmRAqPhyoq6vyCqt .doneCritText2,#mermaid-svg-LmRAqPhyoq6vyCqt .doneCritText3{fill:#000 !important}#mermaid-svg-LmRAqPhyoq6vyCqt .activeCritText0,#mermaid-svg-LmRAqPhyoq6vyCqt .activeCritText1,#mermaid-svg-LmRAqPhyoq6vyCqt .activeCritText2,#mermaid-svg-LmRAqPhyoq6vyCqt .activeCritText3{fill:#000 !important}#mermaid-svg-LmRAqPhyoq6vyCqt .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LmRAqPhyoq6vyCqt g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-LmRAqPhyoq6vyCqt g.classGroup text .title{font-weight:bolder}#mermaid-svg-LmRAqPhyoq6vyCqt g.clickable{cursor:pointer}#mermaid-svg-LmRAqPhyoq6vyCqt g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-LmRAqPhyoq6vyCqt g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-LmRAqPhyoq6vyCqt .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-LmRAqPhyoq6vyCqt .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-LmRAqPhyoq6vyCqt .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-LmRAqPhyoq6vyCqt .dashed-line{stroke-dasharray:3}#mermaid-svg-LmRAqPhyoq6vyCqt #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LmRAqPhyoq6vyCqt #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LmRAqPhyoq6vyCqt #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-LmRAqPhyoq6vyCqt #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-LmRAqPhyoq6vyCqt #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LmRAqPhyoq6vyCqt #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LmRAqPhyoq6vyCqt #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LmRAqPhyoq6vyCqt #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LmRAqPhyoq6vyCqt .commit-id,#mermaid-svg-LmRAqPhyoq6vyCqt .commit-msg,#mermaid-svg-LmRAqPhyoq6vyCqt .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LmRAqPhyoq6vyCqt .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LmRAqPhyoq6vyCqt .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LmRAqPhyoq6vyCqt g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LmRAqPhyoq6vyCqt g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-LmRAqPhyoq6vyCqt g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-LmRAqPhyoq6vyCqt g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-LmRAqPhyoq6vyCqt g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-LmRAqPhyoq6vyCqt g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-LmRAqPhyoq6vyCqt .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-LmRAqPhyoq6vyCqt .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-LmRAqPhyoq6vyCqt .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-LmRAqPhyoq6vyCqt .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-LmRAqPhyoq6vyCqt .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-LmRAqPhyoq6vyCqt .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-LmRAqPhyoq6vyCqt .edgeLabel text{fill:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LmRAqPhyoq6vyCqt .node circle.state-start{fill:black;stroke:black}#mermaid-svg-LmRAqPhyoq6vyCqt .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-LmRAqPhyoq6vyCqt #statediagram-barbEnd{fill:#9370db}#mermaid-svg-LmRAqPhyoq6vyCqt .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-LmRAqPhyoq6vyCqt .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-LmRAqPhyoq6vyCqt .statediagram-state .divider{stroke:#9370db}#mermaid-svg-LmRAqPhyoq6vyCqt .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-LmRAqPhyoq6vyCqt .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-LmRAqPhyoq6vyCqt .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-LmRAqPhyoq6vyCqt .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-LmRAqPhyoq6vyCqt .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-LmRAqPhyoq6vyCqt .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-LmRAqPhyoq6vyCqt .note-edge{stroke-dasharray:5}#mermaid-svg-LmRAqPhyoq6vyCqt .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-LmRAqPhyoq6vyCqt .error-icon{fill:#522}#mermaid-svg-LmRAqPhyoq6vyCqt .error-text{fill:#522;stroke:#522}#mermaid-svg-LmRAqPhyoq6vyCqt .edge-thickness-normal{stroke-width:2px}#mermaid-svg-LmRAqPhyoq6vyCqt .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-LmRAqPhyoq6vyCqt .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-LmRAqPhyoq6vyCqt .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-LmRAqPhyoq6vyCqt .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-LmRAqPhyoq6vyCqt .marker{fill:#333}#mermaid-svg-LmRAqPhyoq6vyCqt .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-LmRAqPhyoq6vyCqt {
color: rgba(0, 0, 0, 0.75);
font: ;
}
预编译
编译
汇编
链接
预编译:把.h文件插入源文件中(#include),替换宏定义(#define),选择需要编译的代码(#ifdef等),删除注释等,产生预编译文件(.i文件)
编译:检查C代码语法错误,然后把C代码翻译为汇编代码(.s文件)。
汇编:将汇编代码翻译为机器代码(二进制.o文件)。
链接:将以上的二进制码文件与需要用到的各种库文件连接,生成可执行文件。
代码文件示例
新建一个文件夹example,在其中建立src、include、lib、bin和build五个文件夹:
mkdir example && cd example
mkdir src include lib bin build
然后在src中创建main.c如下:
#include "stdio.h"
#include "func.h"
int main(){
int a=1, b=6;
int c = func(a, b);
printf("%d + %d = %d\n", a, b, c);
return 1;
}
在lib中创建func.c如下:
int func(int a, int b){
return a+b;
}
直接编译出可执行文件
在gcc中,直接使用gcc即可将源码编译为可执行文件,并自动命名为a.out:
# terminal在example/build目录下
gcc ../src/main.c ../lib/func.c
./a.out
可以通过-o参数设置编译产生文件的名字:
gcc -o main ../src/main.c ../lib/func.c
./main

分步编译
除了直接编译出可执行文件外,也可以通过参数设置,一步步的完成整个源码的编译。
预编译生成.i预编译文件
参数-E表示预编译后停止,与参数-o一同使用来保存预编译文件。
gcc -E -o main.i ../src/main.c
gcc -E -o func.i ../lib/func.c
ls
# func.i main.i
编译生成.s文件
参数-S表示编译后停止
gcc -S func.i main.i
ls
# func.s main.s func.i main.i
汇编生成.o文件
参数-c表示汇编后停止
gcc -c main.s
gcc -c func.s
ls
# func.o main.o func.s main.s func.i main.i
链接生成可执行文件
gcc func.o main.o -o main
ls
# a.out func.o main.o func.s main.s func.i main.i
./a.out
# 1 + 6 = 7
链接库
当源文件较多时不易于管理,编译多文件也会花费更多的时间,因此可以通过gcc将一些功能函数打包成库文件的形式,方便调用。
下面将把func.c打包成库文件。
动态编译
使用动态库(.so或.dll)进行链接。链接动态库时,基本不会把动态库中需要的代码拷贝到可执行文件中,仅在运行时执行相关函数时才进行真正的链接。
动态编译产生的可执行文件比较小。
生成动态链接库.so
通过-shared生成动态库,最好同时加上-fPIC命令:
gcc -shared -fPIC ../lib/func.c -o ../lib/func.so
静态编译
使用静态库(.a或.lib)进行链接。链接静态库时,编译器会把目标文件和静态库中目标文件需要的代码一同拷贝进可执行文件中。
静态编译产生的可执行文件较大,并且如果静态库发生了变化,就需要重新编译整个可执行文件。
生成静态链接库.a
通过ar生成静态库,需要从.o文件生成:
ar crsv ../lib/libfunc.a func.o
编译时指定链接库
无论静态编译还是动态编译都需要指定链接库,最直白的方法是直接通过路径链接,库的路径必须在.o文件的后面:
gcc main.o ../lib/libfunc.a
# or
gcc main.o ../lib/libfunc.so
也可以通过-l指定库的名字和-L指定编译时搜索库的路径,此时如果路径同时存在同名动态和静态库,会优先选择动态链接,可以使用-static强制静态链接:
# 动态链接
gcc main.o -lfunc -L ../lib
# 静态链接
gcc main.o -lfunc -L ../lib -static
运行时指定链接库
静态编译时,由于已经从链接库中把代码拷贝到可执行文件中,因此可以直接运行。
动态编译时,编译器只是将动态库符号位置告诉了可执行文件,在运行时仍然需要动态库来实现链接。而当动态编译时使用-L参数只是指定了编译时搜寻库的位置。
因此需要通过-rpath来指定运行时搜索库的位置:
gcc gcc main.o -lfunc -L ../lib -Wl,-rpath=../lib
指定包含文件
大型工程往往包含很多第三方头文件.h,这些文件的位置可以通过-I指定位置:
gcc -i ../include ../src/main.c
将.h文件对应的.c文件编译成库,然后将.h文件与库文件发布,可以较好的保护.c文件的内容。