{"id":258753,"date":"2024-05-15T12:47:21","date_gmt":"2024-05-15T04:47:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/258753.html"},"modified":"2024-05-15T12:47:28","modified_gmt":"2024-05-15T04:47:28","slug":"c-%e8%af%ad%e8%a8%80%e4%b8%ad%e5%a6%82%e4%bd%95%e5%9c%a8-main-%e5%87%bd%e6%95%b0%e4%b9%8b%e5%89%8d%e8%bf%90%e8%a1%8c%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/258753.html","title":{"rendered":"C \u8bed\u8a00\u4e2d\u5982\u4f55\u5728 main \u51fd\u6570\u4e4b\u524d\u8fd0\u884c\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27105752\/acd7e515-c4a4-4e39-b395-a1726ccb87a6.webp\" alt=\"C \u8bed\u8a00\u4e2d\u5982\u4f55\u5728 main \u51fd\u6570\u4e4b\u524d\u8fd0\u884c\u4ee3\u7801\" \/><\/p>\n<p><p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u5728m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n\u51fd\u6570\u4e4b\u524d\u6267\u884c\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u6280\u672f\u5b9e\u73b0\uff0c<strong>\u5982\u4f7f\u7528\u6784\u9020\u51fd\u6570\uff08GNU C\uff09\u3001\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316<\/strong>\u3001\u4ee5\u53ca<strong>\u5229\u7528\u94fe\u63a5\u5668\u811a\u672c<\/strong>\u3002<strong>\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\u53ef\u4ee5\u88ab\u7528\u6765\u89e6\u53d1\u4ee3\u7801\u6267\u884c\uff0c\u56e0\u4e3a\u5b83\u53d1\u751f\u5728main\u51fd\u6570\u88ab\u8c03\u7528\u4e4b\u524d\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\uff08GNU C\uff09\u63d0\u4f9b\u4e86\u4e00\u79cd\u76f4\u63a5\u4e14\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u5728<code>main<\/code>\u51fd\u6570\u6267\u884c\u4e4b\u524d\u8fd0\u884c\u4ee3\u7801\u3002\u6784\u9020\u51fd\u6570\u662f\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u901a\u8fc7GNU C\u7684<code>__attribute__((constructor))<\/code>\u5c5e\u6027\u5b9a\u4e49\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u76f4\u89c2\u6613\u7528\uff0c\u4f46\u5b83\u4e3b\u8981\u5c40\u9650\u4e8e\u4f7f\u7528GNU C\u7f16\u8bd1\u5668\u7684\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528\u6784\u9020\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u5141\u8bb8\u5f00\u53d1\u8005\u5b9a\u4e49\u5728<code>main<\/code>\u51fd\u6570\u6267\u884c\u4e4b\u524d\u81ea\u52a8\u8c03\u7528\u7684\u51fd\u6570\u3002\u8fd9\u4e2a\u529f\u80fd\u4e3b\u8981\u4f9d\u8d56\u4e8eGNU\u7f16\u8bd1\u5668\u63d0\u4f9b\u7684<code>__attribute__<\/code>\u8bed\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>void preMainInit() __attribute__ ((constructor));<\/p>\n<p>void preMainInit() {<\/p>\n<p>    printf(&quot;\u8fd0\u884c\u5728main\u51fd\u6570\u4e4b\u524d\u3002\\n&quot;);<\/p>\n<p>}<\/p>\n<p>int main() {<\/p>\n<p>    printf(&quot;\u8fd9\u662fmain\u51fd\u6570\u7684\u5f00\u59cb\u3002\\n&quot;);<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u3002\u901a\u8fc7\u6dfb\u52a0<code>__attribute__((constructor))<\/code>\u5c5e\u6027\uff0c<code>preMainInit<\/code>\u51fd\u6570\u4f1a\u5728<code>main<\/code>\u51fd\u6570\u5f00\u59cb\u6267\u884c\u4e4b\u524d\u8c03\u7528\u3002\u8fd9\u4e3a\u521d\u59cb\u5316\u5de5\u4f5c\u6216\u8005\u7279\u5b9a\u7684\u8bbe\u7f6e\u63d0\u4f9b\u4e86\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u9759\u6001\u53d8\u91cf\uff08\u5305\u62ec\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u5b58\u50a8\u671f\u7684\u5c40\u90e8\u53d8\u91cf\uff09\u7684\u521d\u59cb\u5316\u662f\u5728main\u51fd\u6570\u6267\u884c\u4e4b\u524d\u5b8c\u6210\u7684\u3002\u901a\u8fc7\u5de7\u5999\u8bbe\u8ba1\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\u8868\u8fbe\u5f0f\u6216\u8005\u521d\u59cb\u5316\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5728main\u51fd\u6570\u5f00\u59cb\u4e4b\u524d\u6267\u884c\u7279\u5b9a\u4ee3\u7801\u7684\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>int preMainAction() {<\/p>\n<p>    printf(&quot;\u901a\u8fc7\u9759\u6001\u53d8\u91cf\u521d\u59cb\u5316\u8fd0\u884c\u3002\\n&quot;);<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p>static int dummy = preMainAction();<\/p>\n<p>int main() {<\/p>\n<p>    printf(&quot;\u8fd9\u662fmain\u51fd\u6570\u3002\\n&quot;);<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9759\u6001\u53d8\u91cf<code>dummy<\/code>\uff0c\u5e76\u901a\u8fc7\u8c03\u7528<code>preMainAction<\/code>\u51fd\u6570\u6765\u521d\u59cb\u5316\u5b83\u3002\u7531\u4e8e\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\u53d1\u751f\u5728<code>main<\/code>\u51fd\u6570\u6267\u884c\u4e4b\u524d\uff0c\u8fd9\u6837\u4e5f\u5c31\u8fbe\u5230\u4e86\u5728main\u51fd\u6570\u4e4b\u524d\u6267\u884c<code>preMainAction<\/code>\u7684\u76ee\u7684\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u5229\u7528\u94fe\u63a5\u5668\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u94fe\u63a5\u5668\u811a\u672c\u662f\u4e00\u4e2a\u63a7\u5236\u94fe\u63a5\u5668\u884c\u4e3a\u7684\u811a\u672c\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u7cbe\u7ec6\u5730\u63a7\u5236\u4ee3\u7801\u6bb5\u3001\u6570\u636e\u6bb5\u7684\u5e03\u5c40\u3002\u867d\u7136\u8fd9\u662f\u4e00\u79cd\u66f4\u4e3a\u5e95\u5c42\u548c\u590d\u6742\u7684\u65b9\u6cd5\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u5728\u7a0b\u5e8f\u542f\u52a8\u6d41\u7a0b\u4e2d\u66f4\u65e9\u6267\u884c\u4ee3\u7801\u7684\u53ef\u80fd\u6027\uff0c\u751a\u81f3\u6bd4\u6784\u9020\u51fd\u6570\u8fd8\u8981\u65e9\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u94fe\u63a5\u5668\u811a\u672c\u8981\u6c42\u5bf9\u94fe\u63a5\u5668\u53ca\u5176\u811a\u672c\u8bed\u8a00\u6709\u6df1\u5165\u7684\u4e86\u89e3\uff0c\u5e76\u4e14\u8fd9\u79cd\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u65b9\u5f0f\u4f1a\u56e0\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u548c\u94fe\u63a5\u5668\u800c\u6709\u6240\u5dee\u5f02\uff0c\u8fd9\u91cc\u4e0d\u6df1\u5165\u8bb2\u89e3\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\uff0c\u4f46\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u63a2\u7d22GCC\u94fe\u63a5\u5668\u811a\u672c\u7684\u76f8\u5173\u6587\u6863\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u7ed3\u8bba<\/strong><\/p>\n<\/p>\n<p><p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u5728main\u51fd\u6570\u4e4b\u524d\u8fd0\u884c\u4ee3\u7801\u6709\u5176\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\uff0c\u5982\u8fdb\u884c\u7a0b\u5e8f\u521d\u59cb\u5316\u8bbe\u7f6e\u3001\u73af\u5883\u68c0\u67e5\u7b49\u3002\u901a\u8fc7\u4e0a\u9762\u4ecb\u7ecd\u7684\u51e0\u79cd\u65b9\u6cd5\u2014\u2014\u6784\u9020\u51fd\u6570\u3001\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\u3001\u94fe\u63a5\u5668\u811a\u672c\uff0c\u80fd\u591f\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\u3002\u5e94\u5f53\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u65b9\u6cd5\u5728\u4e0d\u540c\u5e73\u53f0\u548c\u7f16\u8bd1\u5668\u4e0b\u7684\u652f\u6301\u5ea6\u548c\u5177\u4f53\u5b9e\u73b0\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u5728\u9009\u62e9\u4f7f\u7528\u67d0\u4e00\u6280\u672f\u65f6\uff0c\u9700\u8981\u8003\u8651\u76ee\u6807\u73af\u5883\u7684\u517c\u5bb9\u6027\u548c\u9002\u7528\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728 C \u8bed\u8a00\u4e2d\u5728 main \u51fd\u6570\u4e4b\u524d\u8fd0\u884c\u4ee3\u7801\uff1f<\/strong><\/p>\n<p>\u5728 C \u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<strong>\u5168\u5c40\u53d8\u91cf<\/strong>\u6216<strong>\u9759\u6001\u53d8\u91cf<\/strong>\u6765\u5b9e\u73b0\u5728 main \u51fd\u6570\u4e4b\u524d\u8fd0\u884c\u4ee3\u7801\u7684\u6548\u679c\u3002\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\u4f1a\u5728\u7a0b\u5e8f\u6267\u884c\u4e4b\u524d\u5b8c\u6210\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5c06\u9700\u8981\u5728 main \u51fd\u6570\u4e4b\u524d\u8fd0\u884c\u7684\u4ee3\u7801\u653e\u5728\u5168\u5c40\u53d8\u91cf\u6216\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\u90e8\u5206\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u5728\u5176\u521d\u59cb\u5316\u90e8\u5206\u653e\u7f6e\u6211\u4eec\u60f3\u8981\u6267\u884c\u7684\u4ee3\u7801\u3002\u8fd9\u6837\uff0c\u5f53\u7a0b\u5e8f\u5f00\u59cb\u8fd0\u884c\u65f6\uff0c\u8be5\u5168\u5c40\u53d8\u91cf\u7684\u521d\u59cb\u5316\u90e8\u5206\u4f1a\u5148\u6267\u884c\u6211\u4eec\u6240\u5199\u7684\u4ee3\u7801\u3002<\/p>\n<p><strong>2. C \u8bed\u8a00\u4e2d\u53ef\u4ee5\u5728 main \u51fd\u6570\u4e4b\u524d\u6267\u884c\u4ee3\u7801\u5417\uff1f<\/strong><\/p>\n<p>\u5728\u6807\u51c6\u7684 C \u8bed\u8a00\u4e2d\uff0c\u4ee3\u7801\u7684\u6267\u884c\u662f\u4ece main \u51fd\u6570\u5f00\u59cb\u7684\uff0c\u56e0\u6b64\u5728 main \u51fd\u6570\u4e4b\u524d\u76f4\u63a5\u6267\u884c\u4ee3\u7801\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\u3002\u7136\u800c\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u6280\u5de7\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\u3002<\/p>\n<p>\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6216\u9759\u6001\u53d8\u91cf\uff0c\u5728\u5176\u521d\u59cb\u5316\u90e8\u5206\u653e\u7f6e\u6211\u4eec\u60f3\u8981\u6267\u884c\u7684\u4ee3\u7801\u3002\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\u4f1a\u5728\u7a0b\u5e8f\u6267\u884c\u4e4b\u524d\u5b8c\u6210\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\u6765\u5728 main \u51fd\u6570\u4e4b\u524d\u8fd0\u884c\u4e00\u4e9b\u4ee3\u7801\u3002<\/p>\n<p><strong>3. \u6709\u6ca1\u6709\u529e\u6cd5\u5728 C \u8bed\u8a00\u4e2d\u5b9e\u73b0\u5728 main \u51fd\u6570\u4e4b\u524d\u6267\u884c\u4ee3\u7801\u7684\u529f\u80fd\uff1f<\/strong><\/p>\n<p>\u867d\u7136\u5728 C \u8bed\u8a00\u4e2d\u76f4\u63a5\u5728 main \u51fd\u6570\u4e4b\u524d\u6267\u884c\u4ee3\u7801\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u6280\u5de7\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\u3002<\/p>\n<p>\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6216\u9759\u6001\u53d8\u91cf\uff0c\u5e76\u5728\u5176\u521d\u59cb\u5316\u90e8\u5206\u653e\u7f6e\u6211\u4eec\u60f3\u8981\u6267\u884c\u7684\u4ee3\u7801\u3002\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\u4f1a\u5728\u7a0b\u5e8f\u6267\u884c\u4e4b\u524d\u5b8c\u6210\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\u6765\u5728 main \u51fd\u6570\u4e4b\u524d\u8fd0\u884c\u4e00\u4e9b\u4ee3\u7801\u3002<\/p>\n<p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u9700\u8981\u5728 main \u51fd\u6570\u4e4b\u524d\u8fd0\u884c\u7684\u4ee3\u7801\u5c01\u88c5\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u5e76\u5728 main \u51fd\u6570\u7684\u6700\u5f00\u59cb\u8c03\u7528\u8be5\u51fd\u6570\u3002\u8fd9\u6837\uff0c\u5728\u7a0b\u5e8f\u6267\u884c\u65f6\u4f1a\u5148\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u5728 main \u51fd\u6570\u4e4b\u524d\u6267\u884c\u4ee3\u7801\u7684\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728C\u8bed\u8a00\u4e2d\uff0c\u5728mAIn\u51fd\u6570\u4e4b\u524d\u6267\u884c\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u6280\u672f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528\u6784\u9020\u51fd\u6570\uff08GNU C\uff09\u3001\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\u3001\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":258766,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/258753"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=258753"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/258753\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/258766"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=258753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=258753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=258753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}