{"id":1426,"date":"2015-02-26T16:37:25","date_gmt":"2015-02-26T21:37:25","guid":{"rendered":"http:\/\/www.jaimerios.com\/?p=1426"},"modified":"2015-02-26T16:37:25","modified_gmt":"2015-02-26T21:37:25","slug":"xcode-dylib-constructor-destructor","status":"publish","type":"post","link":"https:\/\/jaimerios.com\/?p=1426","title":{"rendered":"Xcode dylib constructor destructor"},"content":{"rendered":"<p>I meant to post this link a while ago, but here it is anyway:<br \/>\n<a href=\"https:\/\/developer.apple.com\/library\/mac\/documentation\/DeveloperTools\/Conceptual\/DynamicLibraries\/100-Articles\/DynamicLibraryDesignGuidelines.html#\/\/apple_ref\/doc\/uid\/TP40002013-SW17\" title=\"Running functions when loading or unloading a dylib\" target=\"_blank\" rel=\"noopener\">TP40002013-SW17<\/a><\/p>\n<p>So, in a dylib, you can have code execute when the dylib is loaded and execute code when the dylib is being unloaded:<\/p>\n<pre lang=\"C\">\n__attribute__((constructor))\nstatic void initializer1() {\n    printf(\"[%s] [%s]\\n\", __FILE__, __FUNCTION__);\n}\n \n__attribute__((constructor))\nstatic void initializer2() {\n    printf(\"[%s] [%s]\\n\", __FILE__, __FUNCTION__);\n} \n__attribute__((destructor))\nstatic void finalizer1() {\n    printf(\"[%s] [%s]\\n\", __FILE__, __FUNCTION__);\n}\n \n__attribute__((destructor))\nstatic void finalizer2() {\n    printf(\"[%s] [%s]\\n\", __FILE__, __FUNCTION__);\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I meant to post this link a while ago, but here it is anyway: TP40002013-SW17 So, in a dylib, you can have code execute when the dylib is loaded and execute code when the dylib is being unloaded: __attribute__((constructor)) static void initializer1() { printf(&#8220;[%s] [%s]\\n&#8221;, __FILE__, __FUNCTION__); } __attribute__((constructor)) static void initializer2() { printf(&#8220;[%s] [%s]\\n&#8221;, &#8230; <a title=\"Xcode dylib constructor destructor\" class=\"read-more\" href=\"https:\/\/jaimerios.com\/?p=1426\" aria-label=\"Read more about Xcode dylib constructor destructor\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[180,506],"class_list":["post-1426","post","type-post","status-publish","format-standard","hentry","category-coding","tag-dylib","tag-xcode"],"_links":{"self":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1426"}],"version-history":[{"count":0,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1426\/revisions"}],"wp:attachment":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}