{"id":272,"date":"2023-11-16T17:19:00","date_gmt":"2023-11-16T09:19:00","guid":{"rendered":"https:\/\/solost.cn\/?p=272"},"modified":"2023-11-16T17:22:38","modified_gmt":"2023-11-16T09:22:38","slug":"403","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=272","title":{"rendered":"CMake\u548cvscode\u6587\u4ef6\u914d\u7f6e"},"content":{"rendered":"<h3>\u7b2c\u4e00\u8bb2\uff1aGCC\u7f16\u8bd1\u5668<\/h3>\n<h4>1.1 \u7f16\u8bd1\u8fc7\u7a0b<\/h4>\n<h5>\u9884\u5904\u7406-Pre-Processing    \/\/.i\u6587\u4ef6<\/h5>\n<pre><code class=\"lang-bash\"># -E \u9009\u9879\u6307\u793a\u7f16\u8bd1\u5668\u4ec5\u5bf9\u8f93\u5165\u6587\u4ef6\u8fdb\u884c\u9884\u5904\u7406 \ng++  -E  test.cpp  -o  test.i    \/\/.i\u6587\u4ef6<\/code><\/pre>\n<h5>\u7f16\u8bd1-Compiling    \/\/ .s\u6587\u4ef6<\/h5>\n<pre><code class=\"lang-bash\"># -S \u7f16\u8bd1\u9009\u9879\u544a\u8bc9 g++ \u5728\u4e3a C++ \u4ee3\u7801\u4ea7\u751f\u4e86\u6c47\u7f16\u8bed\u8a00\u6587\u4ef6\u540e\u505c\u6b62\u7f16\u8bd1 \n#  g++ \u4ea7\u751f\u7684\u6c47\u7f16\u8bed\u8a00\u6587\u4ef6\u7684\u7f3a\u7701\u6269\u5c55\u540d\u662f .s \ng++  -S  test.i  -o   test.s<\/code><\/pre>\n<h5>\u6c47\u7f16-Assembling    \/\/ .o\u6587\u4ef6<\/h5>\n<pre><code class=\"lang-bash\"># -c \u9009\u9879\u544a\u8bc9 g++ \u4ec5\u628a\u6e90\u4ee3\u7801\u7f16\u8bd1\u4e3a\u673a\u5668\u8bed\u8a00\u7684\u76ee\u6807\u4ee3\u7801 \n# \u7f3a\u7701\u65f6 g++ \u5efa\u7acb\u7684\u76ee\u6807\u4ee3\u7801\u6587\u4ef6\u6709\u4e00\u4e2a .o \u7684\u6269\u5c55\u540d\u3002 \ng++  -c  test.s  -o  test.o<\/code><\/pre>\n<h5>\u94fe\u63a5-Linking    \/\/ bin\u6587\u4ef6<\/h5>\n<pre><code class=\"lang-bash\"># -o \u7f16\u8bd1\u9009\u9879\u6765\u4e3a\u5c06\u4ea7\u751f\u7684\u53ef\u6267\u884c\u6587\u4ef6\u7528\u6307\u5b9a\u7684\u6587\u4ef6\u540d \ng++  test.o  -o  test<\/code><\/pre>\n<h4>1.2 g++\u91cd\u8981\u7f16\u8bd1\u53c2\u6570<\/h4>\n<h5>g \u7f16\u8bd1\u5e26\u8c03\u8bd5\u4fe1\u606f\u7684\u53ef\u6267\u884c\u6587\u4ef6<\/h5>\n<pre><code class=\"lang-bash\"># -g \u9009\u9879\u544a\u8bc9 GCC \u4ea7\u751f\u80fd\u88ab GNU \u8c03\u8bd5\u5668GDB\u4f7f\u7528\u7684\u8c03\u8bd5\u4fe1\u606f\uff0c\u4ee5\u8c03\u8bd5\u7a0b\u5e8f\u3002 \n# \u4ea7\u751f\u5e26\u8c03\u8bd5\u4fe1\u606f\u7684\u53ef\u6267\u884c\u6587\u4ef6test \ng++ -g test.cpp<\/code><\/pre>\n<h5>-O[n] \u4f18\u5316\u6e90\u4ee3\u7801<\/h5>\n<pre><code class=\"lang-bash\">## \u6240\u8c13\u4f18\u5316\uff0c\u4f8b\u5982\u7701\u7565\u6389\u4ee3\u7801\u4e2d\u4ece\u672a\u4f7f\u7528\u8fc7\u7684\u53d8\u91cf\u3001\u76f4\u63a5\u5c06\u5e38\u91cf\u8868\u8fbe\u5f0f\u7528\u7ed3\u679c\u503c\u4ee3\u66ff\u7b49\u7b49\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u4f1a\u7f29\u51cf\u76ee\u6807\u6587\u4ef6\u6240\u5305\u542b\u7684\u4ee3\u7801\u91cf\uff0c\u63d0\u9ad8\u6700\u7ec8\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8fd0\u884c\u6548\u7387\u3002 \n# -O \u9009\u9879\u544a\u8bc9 g++ \u5bf9\u6e90\u4ee3\u7801\u8fdb\u884c\u57fa\u672c\u4f18\u5316\u3002\u8fd9\u4e9b\u4f18\u5316\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u4f1a\u4f7f\u7a0b\u5e8f\u6267\u884c\u7684\u66f4\u5feb\u3002 -O2 \u9009\u9879\u544a\u8bc9 g++ \u4ea7\u751f\u5c3d\u53ef\u80fd\u5c0f\u548c\u5c3d\u53ef\u80fd\u5feb\u7684\u4ee3\u7801\u3002 \u5982-O2\uff0c-O3\uff0c-On\uff08n \u5e38\u4e3a0\u20133\uff09 \n# -O \u540c\u65f6\u51cf\u5c0f\u4ee3\u7801\u7684\u957f\u5ea6\u548c\u6267\u884c\u65f6\u95f4\uff0c\u5176\u6548\u679c\u7b49\u4ef7\u4e8e-O1 \n# -O0 \u8868\u793a\u4e0d\u505a\u4f18\u5316 \n# -O1 \u4e3a\u9ed8\u8ba4\u4f18\u5316\n# -O2 \u9664\u4e86\u5b8c\u6210-O1\u7684\u4f18\u5316\u4e4b\u5916\uff0c\u8fd8\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u8c03\u6574\u5de5\u4f5c\uff0c\u5982\u6307\u4ee4\u8c03\u6574\u7b49\u3002 \n# -O3 \u5219\u5305\u62ec\u5faa\u73af\u5c55\u5f00\u548c\u5176\u4ed6\u4e00\u4e9b\u4e0e\u5904\u7406\u7279\u6027\u76f8\u5173\u7684\u4f18\u5316\u5de5\u4f5c\u3002 \n# \u9009\u9879\u5c06\u4f7f\u7f16\u8bd1\u7684\u901f\u5ea6\u6bd4\u4f7f\u7528 -O \u65f6\u6162\uff0c \u4f46\u901a\u5e38\u4ea7\u751f\u7684\u4ee3\u7801\u6267\u884c\u901f\u5ea6\u4f1a\u66f4\u5feb\u3002 \n# \u4f7f\u7528 -O2\u4f18\u5316\u6e90\u4ee3\u7801\uff0c\u5e76\u8f93\u51fa\u53ef\u6267\u884c\u6587\u4ef6 \ng++ -O2 test.cpp<\/code><\/pre>\n<h5>l \u548c -L \u6307\u5b9a\u5e93\u6587\u4ef6 | \u6307\u5b9a\u5e93\u6587\u4ef6\u8def\u5f84<\/h5>\n<pre><code class=\"lang-bash\"># -l\u53c2\u6570(\u5c0f\u5199)\u5c31\u662f\u7528\u6765\u6307\u5b9a\u7a0b\u5e8f\u8981\u94fe\u63a5\u7684\u5e93\uff0c-l\u53c2\u6570\u7d27\u63a5\u7740\u5c31\u662f\u5e93\u540d \n# \u5728\/lib\u548c\/usr\/lib\u548c\/usr\/local\/lib\u91cc\u7684\u5e93\u76f4\u63a5\u7528-l\u53c2\u6570\u5c31\u80fd\u94fe\u63a5 \n\n# \u94fe\u63a5glog\u5e93 \ng++ -lglog test.cpp \n\n# \u5982\u679c\u5e93\u6587\u4ef6\u6ca1\u653e\u5728\u4e0a\u9762\u4e09\u4e2a\u76ee\u5f55\u91cc\uff0c\u9700\u8981\u4f7f\u7528-L\u53c2\u6570(\u5927\u5199)\u6307\u5b9a\u5e93\u6587\u4ef6\u6240\u5728\u76ee\u5f55 \n# -L\u53c2\u6570\u8ddf\u7740\u7684\u662f\u5e93\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u540d \n \n# \u94fe\u63a5mytest\u5e93\uff0clibmytest.so\u5728\/home\/bing\/mytestlibfolder\u76ee\u5f55\u4e0b \ng++ -L\/home\/bing\/mytestlibfolder -lmytest test.cpp<\/code><\/pre>\n<h5>I \u6307\u5b9a\u5934\u6587\u4ef6\u641c\u7d22\u76ee\u5f55<\/h5>\n<pre><code class=\"lang-bash\"># -I \n# \/usr\/include\u76ee\u5f55\u4e00\u822c\u662f\u4e0d\u7528\u6307\u5b9a\u7684\uff0cgcc\u77e5\u9053\u53bb\u90a3\u91cc\u627e\uff0c\u4f46 \u662f\u5982\u679c\u5934\u6587\u4ef6\u4e0d\u5728\/usr\/icnclude\u91cc\u6211\u4eec\u5c31\u8981\u7528-I\u53c2\u6570\u6307\u5b9a\u4e86\uff0c\u6bd4\u5982\u5934\u6587\u4ef6\u653e\u5728\/myinclude\u76ee\u5f55\u91cc\uff0c\u90a3\u7f16\u8bd1\u547d\u4ee4\u884c\u5c31\u8981\u52a0\u4e0a-I\/myinclude \u53c2\u6570\u4e86\uff0c\u5982\u679c\u4e0d\u52a0\u4f60\u4f1a\u5f97\u5230\u4e00\u4e2a\u201dxxxx.h: No such file or directory\u201d\u7684\u9519\u8bef\u3002-I\u53c2\u6570\u53ef\u4ee5\u7528\u76f8\u5bf9\u8def\u5f84\uff0c\u6bd4\u5982\u5934\u6587\u4ef6\u5728\u5f53\u524d \u76ee\u5f55\uff0c\u53ef\u4ee5\u7528-I.\u6765\u6307\u5b9a\u3002\u4e0a\u9762\u6211\u4eec\u63d0\u5230\u7684\u2013cflags\u53c2\u6570\u5c31\u662f\u7528\u6765\u751f\u6210-I\u53c2\u6570\u7684\u3002 g++ -I\/myinclude test.cpp<\/code><\/pre>\n<h5>Wall \u6253\u5370\u8b66\u544a\u4fe1\u606f<\/h5>\n<pre><code class=\"lang-bash\"># \u6253\u5370\u51fagcc\u63d0\u4f9b\u7684\u8b66\u544a\u4fe1\u606f g++ -Wall test.cpp<\/code><\/pre>\n<h5>w \u5173\u95ed\u8b66\u544a\u4fe1\u606f<\/h5>\n<pre><code class=\"lang-bash\"># \u5173\u95ed\u6240\u6709\u8b66\u544a\u4fe1\u606f g++ -w test.cpp<\/code><\/pre>\n<h5>std=c++11 \u8bbe\u7f6e\u7f16\u8bd1\u6807\u51c6<\/h5>\n<pre><code class=\"lang-bash\"># \u4f7f\u7528 c++11 \u6807\u51c6\u7f16\u8bd1 test.cpp g++ -std=c++11 test.cpp<\/code><\/pre>\n<h5>o \u6307\u5b9a\u8f93\u51fa\u6587\u4ef6\u540d<\/h5>\n<pre><code class=\"lang-bash\"># \u6307\u5b9a\u5373\u5c06\u4ea7\u751f\u7684\u6587\u4ef6\u540d \n# \u6307\u5b9a\u8f93\u51fa\u53ef\u6267\u884c\u6587\u4ef6\u540d\u4e3atest g++ test.cpp -o test<\/code><\/pre>\n<h5>D\u5b9a\u4e49\u5b8f<\/h5>\n<pre><code class=\"lang-bash\"># \u5728\u4f7f\u7528gcc\/g++\u7f16\u8bd1\u7684\u65f6\u5019\u5b9a\u4e49\u5b8f \n# \u5e38\u7528\u573a\u666f\uff1a \n# -DDEBUG \u5b9a\u4e49DEBUG\u5b8f\uff0c\u53ef\u80fd\u6587\u4ef6\u4e2d\u6709DEBUG\u5b8f\u90e8\u5206\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u7528\u4e2aDDEBUG\u6765\u9009\u62e9\u5f00\u542f\u6216\u5173\u95edDEBUG<\/code><\/pre>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"c++\">\/\/ -Dname \u5b9a\u4e49\u5b8fname,\u9ed8\u8ba4\u5b9a\u4e49\u5185\u5bb9\u4e3a\u5b57\u7b26\u4e32\u201c1\u201d \n#include &lt;stdio.h&gt; \nint main() {  \n    #ifdef DEBUG  \n    printf(&quot;DEBUG LOGn&quot;);  \n    #endif  \n    printf(&quot;inn&quot;); \n} \n\/\/ 1. \u5728\u7f16\u8bd1\u7684\u65f6\u5019\uff0c\u4f7f\u7528gcc -DDEBUG main.cpp \n\/\/ 2. \u7b2c\u4e03\u884c\u4ee3\u7801\u53ef\u4ee5\u88ab\u6267\u884c<\/code><\/pre>\n<p>\u6ce8\uff1a\u4f7f\u7528 <code>man gcc<\/code>\u547d\u4ee4\u53ef\u4ee5\u67e5\u770bgcc\u82f1\u6587\u4f7f\u7528\u624b\u518c\uff0c\u89c1\u4e0b\u56fe<\/p>\n<h4>1.3 \u3010\u5b9e\u6218\u3011g++<code>\u547d\u4ee4\u884c\u7f16\u8bd1<\/code><\/h4>\n<p>\u6848\u4f8b\uff1a\u6700\u521d\u76ee\u5f55\u7ed3\u6784: 2 directories, 3 files<\/p>\n<pre><code class=\"lang-bash\"># \u6700\u521d\u76ee\u5f55\u7ed3\u6784 \n. \n\u251c\u2500\u2500 include \n\u2502   \u2514\u2500\u2500 Swap.h \n\u251c\u2500\u2500 main.cpp \n\u2514\u2500\u2500 src  \n    \u2514\u2500\u2500 Swap.cpp \n2 directories, 3 files<\/code><\/pre>\n<h4>1.3.1 \u76f4\u63a5\u7f16\u8bd1<\/h4>\n<p>\u6700\u7b80\u5355\u7684\u7f16\u8bd1\uff0c\u5e76\u8fd0\u884c<\/p>\n<pre><code class=\"lang-bash\"># \u5c06 main.cpp src\/Swap.cpp \u7f16\u8bd1\u4e3a\u53ef\u6267\u884c\u6587\u4ef6 g++ main.cpp src\/Swap.cpp -Iinclude \n# \u8fd0\u884ca.out .\/a.out<\/code><\/pre>\n<p>\u589e\u52a0\u53c2\u6570\u7f16\u8bd1\uff0c\u5e76\u8fd0\u884c<\/p>\n<pre><code class=\"lang-bash\"># \u5c06 main.cpp src\/Swap.cpp \u7f16\u8bd1\u4e3a\u53ef\u6267\u884c\u6587\u4ef6 \u9644\u5e26\u4e00\u5806\u53c2\u6570 \ng++ main.cpp src\/Swap.cpp -Iinclude -std=c++11 -O2 -Wall -o b.out \n# \u8fd0\u884c b.out \n.\/b.out<\/code><\/pre>\n<h4>1.3.2 \u751f\u6210\u5e93\u6587\u4ef6\u5e76\u7f16\u8bd1<\/h4>\n<p>\u94fe\u63a5\u9759\u6001\u5e93\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u2460\uff1a<\/p>\n<pre><code class=\"lang-bash\">## \u8fdb\u5165src\u76ee\u5f55\u4e0b \n$cd src \n\n# \u6c47\u7f16\uff0c\u751f\u6210Swap.o\u6587\u4ef6 \ng++ Swap.cpp -c -I..\/include \n# \u751f\u6210\u9759\u6001\u5e93libSwap.a \n  ar rs libSwap.a Swap.o \n\n## \u56de\u5230\u4e0a\u7ea7\u76ee\u5f55 \n$cd .. \n\n# \u94fe\u63a5\uff0c\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6:staticmain \ng++ main.cpp -Iinclude -Lsrc -lSwap -o staticmain<\/code><\/pre>\n<p>\u94fe\u63a5\u52a8\u6001\u5e93\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u2461\uff1a<\/p>\n<pre><code class=\"lang-bash\">## \u8fdb\u5165src\u76ee\u5f55\u4e0b \n$cd src \n\n# \u751f\u6210\u52a8\u6001\u5e93libSwap.so \ng++ Swap.cpp -I..\/include -fPIC -shared -o libSwap.so \n\n## \u4e0a\u9762\u547d\u4ee4\u7b49\u4ef7\u4e8e\u4ee5\u4e0b\u4e24\u6761\u547d\u4ee4 \n# gcc Swap.cpp -I..\/include -c -fPIC\n# gcc -shared -o libSwap.so Swap.o ## \u56de\u5230\u4e0a\u7ea7\u76ee\u5f55 \n$cd .. \n\n# \u94fe\u63a5\uff0c\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6:sharemain \ng++ main.cpp -Iinclude -Lsrc -lSwap -o sharemain<\/code><\/pre>\n<p>\u7f16\u8bd1\u5b8c\u6210\u540e\u7684\u76ee\u5f55\u7ed3\u6784<br \/>\u6700\u7ec8\u76ee\u5f55\u7ed3\u6784\uff1a2 directories, 8 files<\/p>\n<pre><code class=\"lang-bash\"># \u6700\u7ec8\u76ee\u5f55\u7ed3\u6784 \n. \n\u251c\u2500\u2500 include \n\u2502   \u2514\u2500\u2500 Swap.h \n\u251c\u2500\u2500 main.cpp \n\u251c\u2500\u2500 sharemain \n\u251c\u2500\u2500 src \n\u2502   \u251c\u2500\u2500 libSwap.a \n\u2502   \u251c\u2500\u2500 libSwap.so \n\u2502   \u251c\u2500\u2500 Swap.cpp \n\u2502   \u2514\u2500\u2500 Swap.o \n\u2514\u2500\u2500 staticmain 2 directories, 8 files<\/code><\/pre>\n<h4>1.3.3 \u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6<\/h4>\n<p>\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\u2460<br \/>Plain Text<\/p>\n<pre><code class=\"lang-bash\"># \u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6 \n.\/staticmain<\/code><\/pre>\n<p>\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\u2461<\/p>\n<pre><code class=\"lang-bash\"># \u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6 \nLD_LIBRARY_PATH=src .\/sharemain<\/code><\/pre>\n<h3>\u7b2c\u4e8c\u8bb2\uff1aGDB\u8c03\u8bd5\u5668<\/h3>\n<p>\u524d\u8a00\uff1a<\/p>\n<ul>\n<li><strong>GDB(GNU Debugger)<\/strong>\u662f\u4e00\u4e2a\u7528\u6765<strong>\u8c03\u8bd5C\/C++\u7a0b\u5e8f<\/strong>\u7684\u529f\u80fd\u5f3a\u5927\u7684<strong>\u8c03\u8bd5\u5668<\/strong>\uff0c\u662fLinux\u7cfb\u7edf\u5f00\u53d1C\/C++\u6700\u5e38\u7528\u7684\u8c03\u8bd5\u5668<\/li>\n<li>\u7a0b\u5e8f\u5458\u53ef\u4ee5<strong>\u4f7f\u7528GDB\u6765\u8ddf\u8e2a\u7a0b\u5e8f\u4e2d\u7684\u9519\u8bef<\/strong>\uff0c\u4ece\u800c\u51cf\u5c11\u7a0b\u5e8f\u5458\u7684\u5de5\u4f5c\u91cf\u3002<\/li>\n<li>Linux \u5f00\u53d1C\/C++ \u4e00\u5b9a\u8981\u719f\u6089 GDB<\/li>\n<li><strong>VSCode\u662f\u901a\u8fc7\u8c03\u7528GDB\u8c03\u8bd5\u5668\u6765\u5b9e\u73b0C\/C++\u7684\u8c03\u8bd5\u5de5\u4f5c\u7684\uff1b<\/strong><\/li>\n<\/ul>\n<blockquote><p>Windows \u7cfb\u7edf\u4e2d\uff0c\u5e38\u89c1\u7684\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\uff0c\u5982 VS\u3001VC\u7b49\uff0c\u5b83\u4eec\u5185\u90e8\u5df2\u7ecf\u5d4c\u5957\u4e86\u76f8\u5e94\u7684\u8c03\u8bd5\u5668<\/p><\/blockquote>\n<p><strong>GDB\u4e3b\u8981\u529f\u80fd\uff1a<\/strong><\/p>\n<ul>\n<li>\u8bbe\u7f6e\u65ad\u70b9(\u65ad\u70b9\u53ef\u4ee5\u662f\u6761\u4ef6\u8868\u8fbe\u5f0f)<\/li>\n<li>\u4f7f\u7a0b\u5e8f\u5728\u6307\u5b9a\u7684\u4ee3\u7801\u884c\u4e0a\u6682\u505c\u6267\u884c\uff0c\u4fbf\u4e8e\u89c2\u5bdf<\/li>\n<li>\u5355\u4e0d\u6267\u884c\u7a0b\u5e8f\uff0c\u4fbf\u4e8e\u8c03\u8bd5<\/li>\n<li>\u67e5\u770b\u7a0b\u5e8f\u4e2d\u53d8\u91cf\u503c\u7684\u53d8\u5316<\/li>\n<li>\u52a8\u6001\u6539\u53d8\u7a0b\u5e8f\u7684\u6267\u884c\u73af\u5883<\/li>\n<li>\u5206\u6790\u5d29\u6e83\u7a0b\u5e8f\u4ea7\u751f\u7684core\u6587\u4ef6<\/li>\n<\/ul>\n<h4>2.1 \u5e38\u7528\u8c03\u8bd5\u547d\u4ee4\u53c2\u6570<\/h4>\n<p>\u8c03\u8bd5\u5f00\u59cb\uff1a\u6267\u884cgdb [exefilename] \uff0c\u8fdb\u5165gdb\u8c03\u8bd5\u7a0b\u5e8f\uff0c\u5176\u4e2dexefilename\u4e3a\u8981\u8c03\u8bd5\u7684\u53ef\u6267\u884c\u6587\u4ef6\u540d<\/p>\n<pre><code class=\"lang-bash\">## \u4ee5\u4e0b\u547d\u4ee4\u540e\u62ec\u53f7\u5185\u4e3a\u547d\u4ee4\u7684\u7b80\u5316\u4f7f\u7528\uff0c\u6bd4\u5982run\uff08r\uff09\uff0c\u76f4\u63a5\u8f93\u5165\u547d\u4ee4 r \u5c31\u4ee3\u8868\u547d\u4ee4run\n\n$(gdb)help(h)    # \u67e5\u770b\u547d\u4ee4\u5e2e\u52a9\uff0c\u5177\u4f53\u547d\u4ee4\u67e5\u8be2\u5728gdb\u4e2d\u8f93\u5165help + \u547d\u4ee4\n\n$(gdb)run(r)    # \u91cd\u65b0\u5f00\u59cb\u8fd0\u884c\u6587\u4ef6\uff08run-text\uff1a\u52a0\u8f7d\u6587\u672c\u6587\u4ef6\uff0crun-bin\uff1a\u52a0\u8f7d\u4e8c\u8fdb\u5236\u6587\u4ef6\uff09\n\n$(gdb)start        # \u5355\u6b65\u6267\u884c\uff0c\u8fd0\u884c\u7a0b\u5e8f\uff0c\u505c\u5728\u7b2c\u4e00\u884c\u6267\u884c\u8bed\u53e5\n\n$(gdb)list(l)     # \u67e5\u770b\u539f\u4ee3\u7801\uff08list-n,\u4ece\u7b2cn\u884c\u5f00\u59cb\u67e5\u770b\u4ee3\u7801\u3002list+ \u51fd\u6570\u540d\uff1a\u67e5\u770b\u5177\u4f53\u51fd\u6570\uff09\n\n$(gdb)set        # \u8bbe\u7f6e\u53d8\u91cf\u7684\u503c\n\n$(gdb)next(n)   # \u5355\u6b65\u8c03\u8bd5\uff08\u9010\u8fc7\u7a0b\uff0c\u51fd\u6570\u76f4\u63a5\u6267\u884c\uff09\n\n$(gdb)step(s)    # \u5355\u6b65\u8c03\u8bd5\uff08\u9010\u8bed\u53e5\uff1a\u8df3\u5165\u81ea\u5b9a\u4e49\u51fd\u6570\u5185\u90e8\u6267\u884c\uff09\n\n$(gdb)backtrace(bt)    # \u67e5\u770b\u51fd\u6570\u7684\u8c03\u7528\u7684\u6808\u5e27\u548c\u5c42\u7ea7\u5173\u7cfb\n\n$(gdb)frame(f)     # \u5207\u6362\u51fd\u6570\u7684\u6808\u5e27\n\n\n$(gdb)info(i)     # \u67e5\u770b\u51fd\u6570\u5185\u90e8\u5c40\u90e8\u53d8\u91cf\u7684\u6570\u503c\n\n$(gdb)finish    # \u7ed3\u675f\u5f53\u524d\u51fd\u6570\uff0c\u8fd4\u56de\u5230\u51fd\u6570\u8c03\u7528\u70b9\n\n$(gdb)continue(c)    # \u7ee7\u7eed\u8fd0\u884c\n\n$(gdb)print(p)    # \u6253\u5370\u503c\u53ca\u5730\u5740\n\n$(gdb)quit(q)     # \u9000\u51fagdb<\/code><\/pre>\n<pre><code class=\"lang-bash\">$(gdb)break+num(b)            # \u5728\u7b2cnum\u884c\u8bbe\u7f6e\u65ad\u70b9\n\n$(gdb)info breakpoints        # \u67e5\u770b\u5f53\u524d\u8bbe\u7f6e\u7684\u6240\u6709\u65ad\u70b9\n\n$(gdb)delete breakpoints num(d)    # \u5220\u9664\u7b2cnum\u4e2a\u65ad\u70b9\n\n$(gdb)display                # \u8ffd\u8e2a\u67e5\u770b\u5177\u4f53\u53d8\u91cf\u503c\n\n$(gdb)undisplay                # \u53d6\u6d88\u8ffd\u8e2a\u89c2\u5bdf\u53d8\u91cf\n\n$(gdb)watch                    # \u88ab\u8bbe\u7f6e\u89c2\u5bdf\u70b9\u7684\u53d8\u91cf\u53d1\u751f\u4fee\u6539\u65f6\uff0c\u6253\u5370\u663e\u793a\n\n$(gdb)i watch                # \u663e\u793a\u89c2\u5bdf\u70b9\n\n$(gdb)enable breakpoints    # \u542f\u7528\u65ad\u70b9\n\n$(gdb)disable breakpoints    # \u7981\u7528\u65ad\u70b9\n\n$(gdb)x                        # \u67e5\u770b\u5185\u5b58x\/20xw \u663e\u793a20\u4e2a\u5355\u5143\uff0c16\u8fdb\u5236\uff0c4\u5b57\u8282\u6bcf\u5355\u5143\n\n$(gdb)run argv[1] argv[2]    # \u8c03\u8bd5\u65f6\u547d\u4ee4\u884c\u4f20\u53c2\n\n$(gdb)set follow-fork-mode child#Makefile\u9879\u76ee\u7ba1\u7406\uff1a\u9009\u62e9\u8ddf\u8e2a\u7236\u5b50\u8fdb\u7a0b\uff08fork()\uff09<\/code><\/pre>\n<blockquote>\n<p>Tips:<\/p>\n<ol>\n<li>\u7f16\u8bd1\u7a0b\u5e8f\u65f6\u9700\u8981\u52a0\u4e0a-g\uff0c\u4e4b\u540e\u624d\u80fd\u7528gdb\u8fdb\u884c\u8c03\u8bd5\uff1agcc -g main.c -o main<\/li>\n<li>\u56de\u8f66\u952e\uff1a\u91cd\u590d\u4e0a\u4e00\u547d\u4ee4<\/li>\n<\/ol>\n<\/blockquote>\n<h4>2.2 \u3010\u5b9e\u6218\u3011\u547d\u4ee4\u884c\u8c03\u8bd5<\/h4>\n<p>\u7ed9\u51fa\u4e00\u6bb5\u7b80\u5355\u4ee3\u7801\uff0c\u51c6\u5907\u8c03\u8bd5\u3002<br \/>C++<\/p>\n<pre><code class=\"C++\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main(int argc,char **argv)\n{\n    int N = 100;\n    int sum = 0;\n    int i = 1;\n\n\/\/ calculate sum from 1 to 100\n    while (i &lt;= N)\n    {\n        sum = sum + i;\n        i = i + 1;\n    }\n\n    cout &lt;&lt; &quot;sum = &quot; &lt;&lt; sum &lt;&lt; endl;\n    cout &lt;&lt; &quot;The program is over.&quot;   &lt;&lt; endl;\n\n    return 0;\n}<\/code><\/pre>\n<h3>\u7b2c\u4e09\u8bb2\uff1aCMake<\/h3>\n<p>\u524d\u8a00\uff1a<\/p>\n<ul>\n<li>CMake\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u5b89\u88c5\u7f16\u8bd1\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u7b80\u5355\u7684\u8bed\u53e5\u6765\u63cf\u8ff0\u6240\u6709\u5e73\u53f0\u7684\u5b89\u88c5(\u7f16\u8bd1\u8fc7\u7a0b)\u3002<\/li>\n<li>CMake\u53ef\u4ee5\u8bf4\u5df2\u7ecf\u6210\u4e3a\u5927\u90e8\u5206C++\u5f00\u6e90\u9879\u76ee\u6807\u914d<\/li>\n<\/ul>\n<h4>3.1 \u8bed\u6cd5\u7279\u6027\u4ecb\u7ecd<\/h4>\n<ul>\n<li>\n<p>\u57fa\u672c\u8bed\u6cd5\u683c\u5f0f\uff1a\u6307\u4ee4(\u53c2\u6570 1 \u53c2\u6570 2...)<\/p>\n<ul>\n<li>\u53c2\u6570\u4f7f\u7528\u62ec\u5f27\u62ec\u8d77<\/li>\n<li>\u53c2\u6570\u4e4b\u95f4\u4f7f\u7528\u7a7a\u683c\u6216\u5206\u53f7\u5206\u5f00<\/li>\n<\/ul>\n<\/li>\n<li>\u6307\u4ee4\u662f\u5927\u5c0f\u5199\u65e0\u5173\u7684\uff0c\u53c2\u6570\u548c\u53d8\u91cf\u662f\u5927\u5c0f\u5199\u76f8\u5173\u7684<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\">set(HELLO hello.cpp) \nadd_executable(hello main.cpp hello.cpp) \nADD_EXECUTABLE(hello main.cpp ${HELLO})<\/code><\/pre>\n<ul>\n<li>\u53d8\u91cf\u4f7f\u7528${}\u65b9\u5f0f\u53d6\u503c\uff0c\u4f46\u662f\u5728 IF \u63a7\u5236\u8bed\u53e5\u4e2d\u662f\u76f4\u63a5\u4f7f\u7528\u53d8\u91cf\u540d<\/li>\n<\/ul>\n<h4>3.2 \u91cd\u8981\u6307\u4ee4\u548cCMake\u5e38\u7528\u53d8\u91cf<\/h4>\n<h4>3.2.1 \u91cd\u8981\u6307\u4ee4<\/h4>\n<ul>\n<li>\n<p>cmake_minimum_required \u6307\u5b9aCMake\u7684\u6700\u5c0f\u7248\u672c\u8981\u6c42<\/p>\n<ul>\n<li>\u8bed\u6cd5\uff1a cmake_minimum_required(VERSION versionNumber [FATAL_ERROR])<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\"># CMake\u6700\u5c0f\u7248\u672c\u8981\u6c42\u4e3a2.8.3 cmake_minimum_required(VERSION 2.8.3)<\/code><\/pre>\n<ul>\n<li>\n<p>project \u5b9a\u4e49\u5de5\u7a0b\u540d\u79f0\uff0c\u5e76\u53ef\u6307\u5b9a\u5de5\u7a0b\u652f\u6301\u7684\u8bed\u8a00<\/p>\n<ul>\n<li>\u8bed\u6cd5\uff1a project(projectname [CXX] [C] [Java])<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\"># \u6307\u5b9a\u5de5\u7a0b\u540d\u4e3aHELLOWORLD project(HELLOWORLD)<\/code><\/pre>\n<ul>\n<li>\n<p>set \u663e\u5f0f\u7684\u5b9a\u4e49\u53d8\u91cf<\/p>\n<ul>\n<li>\u8bed\u6cd5\uff1aset(VAR [VALUE] [CACHE TYPE DOCSTRING [FORCE]])<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\"># \u5b9a\u4e49SRC\u53d8\u91cf\uff0c\u5176\u503c\u4e3asayhello.cpp hello.cpp set(SRC sayhello.cpp hello.cpp)<\/code><\/pre>\n<ul>\n<li>\n<p>include_directories \u5411\u5de5\u7a0b\u6dfb\u52a0\u591a\u4e2a\u7279\u5b9a\u7684\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84 ---&gt;\u76f8\u5f53\u4e8e\u6307\u5b9ag++\u7f16\u8bd1\u5668\u7684-I\u53c2\u6570<\/p>\n<ul>\n<li>\u8bed\u6cd5\uff1a include_directories([AFTER|BEFORE] [SYSTEM] dir1 dir2 ...)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\"># \u5c06\/usr\/include\/myincludefolder \u548c .\/include \u6dfb\u52a0\u5230\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84 include_directories(\/usr\/include\/myincludefolder .\/include)<\/code><\/pre>\n<ul>\n<li>\n<p>link_directories \u5411\u5de5\u7a0b\u6dfb\u52a0\u591a\u4e2a\u7279\u5b9a\u7684\u5e93\u6587\u4ef6\u641c\u7d22\u8def\u5f84 ---&gt;\u76f8\u5f53\u4e8e\u6307\u5b9ag++\u7f16\u8bd1\u5668\u7684-L\u53c2\u6570<\/p>\n<ul>\n<li>\u8bed\u6cd5\uff1a link_directories(dir1 dir2 ...)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\"># \u5c06\/usr\/lib\/mylibfolder \u548c .\/lib \u6dfb\u52a0\u5230\u5e93\u6587\u4ef6\u641c\u7d22\u8def\u5f84 link_directories(\/usr\/lib\/mylibfolder .\/lib)<\/code><\/pre>\n<ul>\n<li>\n<p>add_library \u751f\u6210\u5e93\u6587\u4ef6<\/p>\n<ul>\n<li>\u8bed\u6cd5\uff1a add_library(libname [SHARED|STATIC|MODULE] [EXCLUDE_FROM_ALL] source1 source2 ... sourceN)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\"># \u901a\u8fc7\u53d8\u91cf SRC \u751f\u6210 libhello.so \u5171\u4eab\u5e93 add_library(hello SHARED ${SRC})<\/code><\/pre>\n<ul>\n<li>\n<p>add_compile_options \u6dfb\u52a0\u7f16\u8bd1\u53c2\u6570<\/p>\n<ul>\n<li>\u8bed\u6cd5\uff1aadd_compile_options(<option> ...)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\"># \u6dfb\u52a0\u7f16\u8bd1\u53c2\u6570 -Wall -std=c++11 -O2 add_compile_options(-Wall -std=c++11 -O2)<\/code><\/pre>\n<ul>\n<li>\n<p>add_executable \u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6<\/p>\n<ul>\n<li>\u8bed\u6cd5\uff1aadd_executable(exename source1 source2 ... sourceN)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\"># \u7f16\u8bd1main.cpp\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6main add_executable(main main.cpp)<\/code><\/pre>\n<ul>\n<li>\n<p>target_link_libraries \u4e3a target \u6dfb\u52a0\u9700\u8981\u94fe\u63a5\u7684\u5171\u4eab\u5e93 --&gt;\u76f8\u540c\u4e8e\u6307\u5b9ag++\u7f16\u8bd1\u5668-l\u53c2\u6570<\/p>\n<ul>\n<li>\u8bed\u6cd5\uff1a target_link_libraries(target library1<debug | optimized> library2...)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\"># \u5c06hello\u52a8\u6001\u5e93\u6587\u4ef6\u94fe\u63a5\u5230\u53ef\u6267\u884c\u6587\u4ef6main target_link_libraries(main hello)<\/code><\/pre>\n<ul>\n<li>\n<p>add_subdirectory \u5411\u5f53\u524d\u5de5\u7a0b\u6dfb\u52a0\u5b58\u653e\u6e90\u6587\u4ef6\u7684\u5b50\u76ee\u5f55\uff0c\u5e76\u53ef\u4ee5\u6307\u5b9a\u4e2d\u95f4\u4e8c\u8fdb\u5236\u548c\u76ee\u6807\u4e8c\u8fdb\u5236\u5b58\u653e\u7684\u4f4d\u7f6e<\/p>\n<ul>\n<li>\u8bed\u6cd5\uff1a add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\"># \u6dfb\u52a0src\u5b50\u76ee\u5f55\uff0csrc\u4e2d\u9700\u6709\u4e00\u4e2aCMakeLists.txt add_subdirectory(src)<\/code><\/pre>\n<ul>\n<li>\n<p>aux_source_directory \u53d1\u73b0\u4e00\u4e2a\u76ee\u5f55\u4e0b\u6240\u6709\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u5e76\u5c06\u5217\u8868\u5b58\u50a8\u5728\u4e00\u4e2a\u53d8\u91cf\u4e2d\uff0c\u8fd9\u4e2a\u6307\u4ee4\u4e34\u65f6\u88ab\u7528\u6765\u81ea\u52a8\u6784\u5efa\u6e90\u6587\u4ef6\u5217\u8868<\/p>\n<ul>\n<li>\u8bed\u6cd5\uff1a aux_source_directory(dir VARIABLE)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\"># \u5b9a\u4e49SRC\u53d8\u91cf\uff0c\u5176\u503c\u4e3a\u5f53\u524d\u76ee\u5f55\u4e0b\u6240\u6709\u7684\u6e90\u4ee3\u7801\u6587\u4ef6 aux_source_directory(. SRC) \n# \u7f16\u8bd1SRC\u53d8\u91cf\u6240\u4ee3\u8868\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\uff0c\u751f\u6210main\u53ef\u6267\u884c\u6587\u4ef6 add_executable(main ${SRC})<\/code><\/pre>\n<h4>3.2.2 CMake\u5e38\u7528\u53d8\u91cf<\/h4>\n<ul>\n<li>CMAKE_C_FLAGS gcc\u7f16\u8bd1\u9009\u9879<\/li>\n<li>CMAKE_CXX_FLAGS g++\u7f16\u8bd1\u9009\u9879<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\"># \u5728CMAKE_CXX_FLAGS\u7f16\u8bd1\u9009\u9879\u540e\u8ffd\u52a0-std=c++11 \nset( CMAKE_CXX_FLAGS &quot;${CMAKE_CXX_FLAGS} -std=c++11&quot;)<\/code><\/pre>\n<ul>\n<li>CMAKE_BUILD_TYPE \u7f16\u8bd1\u7c7b\u578b(Debug, Release)<\/li>\n<\/ul>\n<pre><code class=\"lang-Makefile\"># \u8bbe\u5b9a\u7f16\u8bd1\u7c7b\u578b\u4e3adebug\uff0c\u8c03\u8bd5\u65f6\u9700\u8981\u9009\u62e9debug set(CMAKE_BUILD_TYPE Debug) \n# \u8bbe\u5b9a\u7f16\u8bd1\u7c7b\u578b\u4e3arelease\uff0c\u53d1\u5e03\u65f6\u9700\u8981\u9009\u62e9release set(CMAKE_BUILD_TYPE Release)<\/code><\/pre>\n<ul>\n<li>CMAKE_BINARY_DIR<\/li>\n<\/ul>\n<p><strong>PROJECT_BINARY_DIR<\/strong><\/p>\n<p><projectname>_BINARY_DIR<\/p>\n<blockquote><p>\u8fd9\u4e09\u4e2a\u53d8\u91cf\u6307\u4ee3\u7684\u5185\u5bb9\u662f\u4e00\u81f4\u7684\u3002\u5982\u679c\u662f in source build\uff0c\u6307\u7684\u5c31\u662f\u5de5\u7a0b\u9876\u5c42\u76ee\u5f55\u3002\u5982\u679c\u662f out-of-source \u7f16\u8bd1,\u6307\u7684\u662f\u5de5\u7a0b\u7f16\u8bd1\u53d1\u751f\u7684\u76ee\u5f55\u3002PROJECT_BINARY_DIR \u8ddf\u5176\u4ed6\u6307\u4ee4\u7a0d\u6709\u533a\u522b\uff0c\u4e0d\u8fc7\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u7406\u89e3\u4e3a\u4ed6\u4eec\u662f\u4e00\u81f4\u7684\u3002<\/p><\/blockquote>\n<ul>\n<li>CMAKE_SOURCE_DIR<\/li>\n<li>PROJECT_SOURCE_DIR<projectname>_SOURCE_DIR<\/li>\n<li>\u8fd9\u4e09\u4e2a\u53d8\u91cf\u6307\u4ee3\u7684\u5185\u5bb9\u662f\u4e00\u81f4\u7684,\u4e0d\u8bba\u91c7\u7528\u4f55\u79cd\u7f16\u8bd1\u65b9\u5f0f,\u90fd\u662f\u5de5\u7a0b\u9876\u5c42\u76ee\u5f55\u3002\u4e5f\u5c31\u662f\u5728 in source build\u65f6,\u4ed6\u8ddf CMAKE_BINARY_DIR \u7b49\u53d8\u91cf\u4e00\u81f4\u3002PROJECT_SOURCE_DIR \u8ddf\u5176\u4ed6\u6307\u4ee4\u7a0d\u6709\u533a\u522b,\u73b0\u5728,\u4f60\u53ef\u4ee5\u7406\u89e3\u4e3a\u4ed6\u4eec\u662f\u4e00\u81f4\u7684\u3002<\/li>\n<li>CMAKE_C_COMPILER\uff1a\u6307\u5b9aC\u7f16\u8bd1\u5668<\/li>\n<li>CMAKE_CXX_COMPILER\uff1a\u6307\u5b9aC++\u7f16\u8bd1\u5668<\/li>\n<li>EXECUTABLE_OUTPUT_PATH\uff1a\u53ef\u6267\u884c\u6587\u4ef6\u8f93\u51fa\u7684\u5b58\u653e\u8def\u5f84<\/li>\n<li>LIBRARY_OUTPUT_PATH\uff1a\u5e93\u6587\u4ef6\u8f93\u51fa\u7684\u5b58\u653e\u8def\u5f84<\/li>\n<\/ul>\n<h4>3.3 CMake\u7f16\u8bd1\u5de5\u7a0b<\/h4>\n<p>CMake\u76ee\u5f55\u7ed3\u6784\uff1a\u9879\u76ee\u4e3b\u76ee\u5f55\u5b58\u5728\u4e00\u4e2aCMakeLists.txt\u6587\u4ef6<br \/>\u4e24\u79cd\u65b9\u5f0f\u8bbe\u7f6e\u7f16\u8bd1\u89c4\u5219\uff1a<\/p>\n<ol>\n<li>\u5305\u542b\u6e90\u6587\u4ef6\u7684\u5b50\u6587\u4ef6\u5939\u5305\u542bCMakeLists.txt\u6587\u4ef6\uff0c\u4e3b\u76ee\u5f55\u7684CMakeLists.txt\u901a\u8fc7add_subdirectory\u6dfb\u52a0\u5b50\u76ee\u5f55\u5373\u53ef\uff1b<\/li>\n<li>\u5305\u542b\u6e90\u6587\u4ef6\u7684\u5b50\u6587\u4ef6\u5939\u672a\u5305\u542bCMakeLists.txt\u6587\u4ef6\uff0c\u5b50\u76ee\u5f55\u7f16\u8bd1\u89c4\u5219\u4f53\u73b0\u5728\u4e3b\u76ee\u5f55\u7684CMakeLists.txt\u4e2d\uff1b<\/li>\n<\/ol>\n<h4>3.3.1 \u7f16\u8bd1\u6d41\u7a0b<\/h4>\n<p>\u5728 linux \u5e73\u53f0\u4e0b\u4f7f\u7528 CMake \u6784\u5efaC\/C++\u5de5\u7a0b\u7684\u6d41\u7a0b\u5982\u4e0b:<\/p>\n<ul>\n<li>\u624b\u52a8\u7f16\u5199 CMakeLists.txt\u3002<\/li>\n<li>\u6267\u884c\u547d\u4ee4 cmake PATH\u751f\u6210 Makefile ( PATH \u662f\u9876\u5c42CMakeLists.txt \u6240\u5728\u7684\u76ee\u5f55 )\u3002<\/li>\n<li>\u6267\u884c\u547d\u4ee4make \u8fdb\u884c\u7f16\u8bd1\u3002<\/li>\n<\/ul>\n<pre><code class=\"lang-bash\"># important tips .      \n# \u8868\u793a\u5f53\u524d\u76ee\u5f55 .\/     \n# \u8868\u793a\u5f53\u524d\u76ee\u5f55 ..  \n# \u8868\u793a\u4e0a\u7ea7\u76ee\u5f55 ..\/ \n# \u8868\u793a\u4e0a\u7ea7\u76ee\u5f55<\/code><\/pre>\n<h4>3.3.2 \u4e24\u79cd\u6784\u5efa\u65b9\u5f0f<\/h4>\n<ul>\n<li>\u5185\u90e8\u6784\u5efa(in-source build)\uff1a\u4e0d\u63a8\u8350\u4f7f\u7528<\/li>\n<\/ul>\n<p>\u5185\u90e8\u6784\u5efa\u4f1a\u5728\u540c\u7ea7\u76ee\u5f55\u4e0b\u4ea7\u751f\u4e00\u5927\u5806\u4e2d\u95f4\u6587\u4ef6\uff0c\u8fd9\u4e9b\u4e2d\u95f4\u6587\u4ef6\u5e76\u4e0d\u662f\u6211\u4eec\u6700\u7ec8\u6240\u9700\u8981\u7684\uff0c\u548c\u5de5\u7a0b\u6e90\u6587\u4ef6\u653e\u5728\u4e00\u8d77\u4f1a\u663e\u5f97\u6742\u4e71\u65e0\u7ae0\u3002<\/p>\n<pre><code class=\"lang-bash\">## \u5185\u90e8\u6784\u5efa # \u5728\u5f53\u524d\u76ee\u5f55\u4e0b\uff0c\u7f16\u8bd1\u672c\u76ee\u5f55\u7684CMakeLists.txt\uff0c\u751f\u6210Makefile\u548c\u5176\u4ed6\u6587\u4ef6 cmake . # \u6267\u884cmake\u547d\u4ee4\uff0c\u751f\u6210target make<\/code><\/pre>\n<ul>\n<li>\u5916\u90e8\u6784\u5efa(out-of-source build)\uff1a\u63a8\u8350\u4f7f\u7528<\/li>\n<\/ul>\n<pre><code>\u5c06\u7f16\u8bd1\u8f93\u51fa\u6587\u4ef6\u4e0e\u6e90\u6587\u4ef6\u653e\u5230\u4e0d\u540c\u76ee\u5f55\u4e2d<\/code><\/pre>\n<pre><code class=\"lang-bash\">## \u5916\u90e8\u6784\u5efa # 1. \u5728\u5f53\u524d\u76ee\u5f55\u4e0b\uff0c\u521b\u5efabuild\u6587\u4ef6\u5939 mkdir build # 2. \u8fdb\u5165\u5230build\u6587\u4ef6\u5939 cd build # 3. \u7f16\u8bd1\u4e0a\u7ea7\u76ee\u5f55\u7684CMakeLists.txt\uff0c\u751f\u6210Makefile\u548c\u5176\u4ed6\u6587\u4ef6 cmake .. # 4. \u6267\u884cmake\u547d\u4ee4\uff0c\u751f\u6210target make<\/code><\/pre>\n<h4>3.4 \u3010\u5b9e\u6218\u3011CMake\u4ee3\u7801\u5b9e\u8df5<\/h4>\n<p>\u9488\u5bf9\u7b2c\u4e94\u7ae0\u5199\u7684\u4e24\u4e2a\u5c0f\u9879\u76ee\u6765\u5199\u5bf9\u5e94\u7684CMakeLists.txt<\/p>\n<h4>3.4.1 \u6700\u5c0fCMake\u5de5\u7a0b<\/h4>\n<pre><code class=\"lang-bash\"># Set the minimum version of CMake that can be used\ncmake_minimum_required(VERSION 3.0)\n\n# Set the project name\nproject (HELLO)\n\n# Add an executable\nadd_executable(hello_cmake main.cpp)<\/code><\/pre>\n<h4>3.4.2 \u591a\u76ee\u5f55\u5de5\u7a0b - \u76f4\u63a5\u7f16\u8bd1<\/h4>\n<pre><code class=\"lang-shell\"># Set the minimum version of CMake that can be used\ncmake_minimum_required(VERSION 3.0)\n\n#project name\nproject(SWAP)\n\n#head file pat\ninclude_directories( include )\n\n#source directory files to var\nadd_subdirectory( src DIR_SRCS )\n\n#add executable file\nadd_executable(swap_02 ${TEST_MATH})\n\n#add link library\ntarget_link_libraries(${FS_BUILD_BINARY_PREFIX}sqrt ${LIBRARIES})<\/code><\/pre>\n<h4>3.4.3 \u591a\u76ee\u5f55\u5de5\u7a0b - \u751f\u6210\u5e93\u7f16\u8bd1<\/h4>\n<pre><code class=\"lang-Shell\"># Set the minimum version of CMake that can be used\ncmake_minimum_required(VERSION 3.0)\n\n#project name\nproject(SWAP_LIBRARY)\n\n#add compile options\nadd_compile_options(&quot;-Wall -std=c++11&quot;)\n\n#set CMAKE_BUILD_TYPE\nset( CMAKE_BUILD_TYPE Debug )\n\n# set output binary path\nset(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}\/bin)\n\n############################################################\n# Create a library\n############################################################\n\n#Generate the static library from the library sources\nadd_library( swap_library STATIC src\/Swap.cpp )\n\ntarget_include_directories( swap_lib PUBLIC ${PROJECT_SOURCE_DIR}\/include )\n\n############################################################\n# Create an executable\n############################################################\n\n# Add an executable with the above sources\nadd_executable( swap_01 main.cpp )\n\n# link the new swap_01 target with the swap_lib target\ntarget_link_libraries( swap_01 swap_liby )<\/code><\/pre>\n<h3>\u7b2c\u56db\u8bb2\uff1atasks\u548claunch\u6587\u4ef6\u7f16\u5199<\/h3>\n<pre><code class=\"lang-json\">\/\/launch.json\n{\n    \/\/ \u4f7f\u7528 IntelliSense \u4e86\u89e3\u76f8\u5173\u5c5e\u6027\u3002 \n    \/\/ \u60ac\u505c\u4ee5\u67e5\u770b\u73b0\u6709\u5c5e\u6027\u7684\u63cf\u8ff0\u3002\n    \/\/ \u6b32\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u8bbf\u95ee: https:\/\/go.microsoft.com\/fwlink\/?linkid=830387\n    &quot;version&quot;: &quot;0.2.0&quot;,\n    &quot;configurations&quot;: [\n        {\n            &quot;name&quot;: &quot;(gdb) \u542f\u52a8&quot;,\n            &quot;type&quot;: &quot;cppdbg&quot;,\n            &quot;request&quot;: &quot;launch&quot;,\n            &quot;program&quot;: &quot;${workspaceFolder}\/build\/kilo&quot;,\/\/\u542f\u52a8\u8def\u5f84\n            &quot;args&quot;: [],\n            &quot;stopAtEntry&quot;: false,\n            &quot;cwd&quot;: &quot;${fileDirname}&quot;,\n            &quot;environment&quot;: [],\n            &quot;externalConsole&quot;: false,\n            &quot;MIMode&quot;: &quot;gdb&quot;,\n            &quot;setupCommands&quot;: [\n                {\n                    &quot;description&quot;: &quot;\u4e3a gdb \u542f\u7528\u6574\u9f50\u6253\u5370&quot;,\n                    &quot;text&quot;: &quot;-enable-pretty-printing&quot;,\n                    &quot;ignoreFailures&quot;: true\n                },\n                {\n                    &quot;description&quot;: &quot;\u5c06\u53cd\u6c47\u7f16\u98ce\u683c\u8bbe\u7f6e\u4e3a Intel&quot;,\n                    &quot;text&quot;: &quot;-gdb-set disassembly-flavor intel&quot;,\n                    &quot;ignoreFailures&quot;: true\n                }\n            ],\n            &quot;preLaunchTask&quot;: &quot;cppbuild&quot;,\/\/\u6267\u884claunch\u4e4b\u524d\u5148\u6267\u884ctasks\u7684\u67d0\u4e2a\u4efb\u52a1\n        }\n\n    ]\n}<\/code><\/pre>\n<pre><code class=\"lang-json\">\/\/tasks.json\n{\n    &quot;options&quot;: {\n        &quot;cwd&quot;: &quot;${workspaceFolder}\/build&quot;\/\/\u5de5\u4f5c\u8def\u5f84\n    },\n    &quot;tasks&quot;: [\n        {\n            &quot;type&quot;: &quot;shell&quot;,\n            &quot;label&quot;: &quot;cmake&quot;,\/\/\u4efb\u52a1\u4e00\u5148\u6267\u884ccmake\n            &quot;command&quot;: &quot;cmake&quot;,\n            &quot;args&quot;: [\n                &quot;..&quot;\/\/\u76f8\u5f53\u4e8ecmake ..\n            ]\n        },\n        {\n            &quot;label&quot;: &quot;make&quot;,\/\/\u4efb\u52a1\u4e8cmake\n            &quot;group&quot;: {\n                &quot;kind&quot;: &quot;build&quot;,\n                &quot;isDefault&quot;: true\n            },\n            &quot;command&quot;: &quot;make&quot;,\n            &quot;args&quot;: [\n            \n            ]\n        },\n        {\n            &quot;type&quot;: &quot;cppbuild&quot;,\/\/\u4efb\u52a1\u4e09\u6267\u884c\n            &quot;label&quot;: &quot;C\/C++: gcc \u751f\u6210\u6d3b\u52a8\u6587\u4ef6&quot;,\n            &quot;dependsOrder&quot;: &quot;sequence&quot;,\/\/\u6309\u7167\u4efb\u52a1\u987a\u5e8f\u6267\u884c\n            &quot;dependsOn&quot;:[\n                &quot;cmake&quot;,\n                &quot;make&quot;\n            ],\n            &quot;command&quot;: &quot;\/usr\/bin\/gcc&quot;,\n            &quot;args&quot;: [\n                &quot;-fdiagnostics-color=always&quot;,\n                &quot;-g&quot;,\n                &quot;${file}&quot;,\n                &quot;-o&quot;,\n                &quot;${fileDirname}\/${fileBasenameNoExtension}&quot;\n            ],\n            &quot;options&quot;: {\n                &quot;cwd&quot;: &quot;${fileDirname}&quot;\n            },\n            &quot;problemMatcher&quot;: [\n                &quot;$gcc&quot;\n            ],\n            &quot;group&quot;: {\n                &quot;kind&quot;: &quot;build&quot;,\n                &quot;isDefault&quot;: true\n            },\n            &quot;detail&quot;: &quot;\u8c03\u8bd5\u5668\u751f\u6210\u7684\u4efb\u52a1\u3002&quot;\n        }\n    ],\n    &quot;version&quot;: &quot;2.0.0&quot;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b2c\u4e00\u8bb2\uff1aGCC\u7f16\u8bd1\u5668 1.1 \u7f16\u8bd1\u8fc7\u7a0b \u9884\u5904\u7406-Pre-Processing \/\/.i\u6587\u4ef6 # -E \u9009\u9879\u6307\u793a\u7f16\u8bd1\u5668\u4ec5\u5bf9\u8f93\u5165\u6587\u4ef6\u8fdb\u884c\u9884\u5904\u7406 g++ -E test.cpp -o test.i \/\/.i\u6587\u4ef6 \u7f16\u8bd1-Compiling \/\/ .s\u6587\u4ef6 # -S \u7f16\u8bd1\u9009\u9879\u544a\u8bc9 g++ \u5728\u4e3a C++ \u4ee3\u7801\u4ea7\u751f\u4e86\u6c47\u7f16\u8bed\u8a00\u6587\u4ef6\u540e\u505c\u6b62\u7f16\u8bd1 # g++ \u4ea7\u751f\u7684\u6c47\u7f16\u8bed\u8a00\u6587\u4ef6\u7684\u7f3a\u7701\u6269\u5c55\u540d\u662f .s g++ -S test.i -o test.s \u6c47\u7f16-Assembling \/\/ .o\u6587\u4ef6 # -c \u9009\u9879\u544a\u8bc9 g++ \u4ec5\u628a\u6e90\u4ee3\u7801\u7f16\u8bd1\u4e3a\u673a\u5668\u8bed\u8a00\u7684\u76ee\u6807\u4ee3\u7801 # \u7f3a\u7701\u65f6 g++ \u5efa\u7acb\u7684\u76ee\u6807\u4ee3\u7801\u6587\u4ef6\u6709\u4e00\u4e2a .o \u7684\u6269\u5c55\u540d\u3002 g++ -c test.s -o test.o \u94fe\u63a5-Linking \/\/ bin\u6587\u4ef6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,63],"tags":[],"class_list":["post-272","post","type-post","status-publish","format-standard","hentry","category-cmake","category-vscode"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=272"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/272\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}