{"id":1642,"date":"2024-11-01T16:48:52","date_gmt":"2024-11-01T08:48:52","guid":{"rendered":"https:\/\/www.madbull.site\/?p=1642"},"modified":"2024-11-01T16:49:39","modified_gmt":"2024-11-01T08:49:39","slug":"c%e8%af%ad%e8%a8%80main%e4%b9%8b%e5%89%8d%e5%92%8c%e4%b9%8b%e5%90%8e%e8%b0%83%e7%94%a8%e7%9a%84%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=1642","title":{"rendered":"C\u8bed\u8a00main\u4e4b\u524d\u548c\u4e4b\u540e\u8c03\u7528\u7684\u51fd\u6570"},"content":{"rendered":"\n<p>\u53d1\u73b0\u4e00\u4e2a\u597d\u73a9\u7684C\u8bed\u8a00\u7279\u6027\uff0c\u5728main\u51fd\u6570\u8c03\u7528\u4e4b\u524d\u548c\u8c03\u7528\u4e4b\u540e\u4e5f\u53ef\u4ee5\u6267\u884c\u4e00\u4e9b\u6784\u9020\u548c\u6790\u6784\u7684\u64cd\u4f5c\uff0c\u4e00\u822c\u7528\u6765\u521d\u59cb\u5316\u6570\u636e\u548c\u8d44\u6e90\u56de\u6536\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u5148\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>tcnst.c:<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ gcc -o test tcnst.c \n\n#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;unistd.h>\n\nvoid bf_print() __attribute__((constructor)) ;\nvoid af_print() __attribute__((destructor)) ;\n\nvoid bf_print() {\n    printf(\"bfbfbfbfbefore main\\n\") ;\n}\n\nvoid af_print() {\n    printf(\"afafafafafter main\\n\") ;\n}\n\nint main(int argc, char * argv&#91;]) {\n    sleep(1) ;\n    printf(\"I am main\\n\") ;\n    sleep(1) ;\n    return 0 ;\n}<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u7f16\u8bd1\u6267\u884c\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"436\" height=\"134\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-2.png\" alt=\"\" class=\"wp-image-1644\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-2.png 436w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-2-300x92.png 300w\" sizes=\"auto, (max-width: 436px) 100vw, 436px\" \/><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u4ece\u4e0a\u8fb9\u7684\u8fd0\u884c\u7ed3\u679c\u770b\u51fa\u6765\uff0c\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u4f7f\u7528 <code>__attribute__((constructor))<\/code> \u4fee\u9970\u5219\u4f1a\u5728main\u4e4b\u524d\u6267\u884c\uff1b\u800c\u4f7f\u7528 <code>__attribute__((destructor))<\/code> \u4fee\u9970\u5219\u4f1a\u5728main\u8fd0\u884c\u7ed3\u675f\u4e4b\u540e\u6267\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53d1\u73b0\u4e00\u4e2a\u597d\u73a9\u7684C\u8bed\u8a00\u7279\u6027\uff0c\u5728main\u51fd\u6570\u8c03\u7528\u4e4b\u524d\u548c\u8c03\u7528\u4e4b\u540e\u4e5f\u53ef\u4ee5\u6267\u884c\u4e00\u4e9b\u6784\u9020\u548c\u6790\u6784\u7684\u64cd\u4f5c\uff0c\u4e00\u822c\u7528\u6765\u521d\u59cb\u5316\u6570\u636e\u548c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[156,154],"tags":[527,528,529],"class_list":["post-1642","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-154","tag-__attribute__","tag-constructor","tag-destructor"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1642"}],"version-history":[{"count":2,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1642\/revisions"}],"predecessor-version":[{"id":1646,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1642\/revisions\/1646"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}