{"id":7142,"date":"2013-03-16T13:54:36","date_gmt":"2013-03-16T18:54:36","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=7142"},"modified":"2013-03-16T13:54:37","modified_gmt":"2013-03-16T18:54:37","slug":"shebang-objective-c","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2013\/03\/16\/shebang-objective-c\/","title":{"rendered":"Shebang Objective-C"},"content":{"rendered":"<p><a href=\"http:\/\/bou.io\/HowToWriteASelfCompilingObjectiveCFile.html\">Nicolas Bouilleaud<\/a> has a clever trick for making Objective-C source files self-compiling (via <a href=\"https:\/\/twitter.com\/romainbriche\/status\/312314787503697920\">Romain Briche<\/a>):<\/p>\n<blockquote cite=\"http:\/\/bou.io\/HowToWriteASelfCompilingObjectiveCFile.html\"><p>The idea is to play with shell and C comments so that the file is both a valid shell script and an Objective-C program. The shell script calls <code>clang<\/code> to compile itself, and then runs the output executable.<\/p><\/blockquote>\n<p>I&rsquo;ve fixed the quoting so that it also works when the path contains a space:<\/p>\n<pre>\/*\/..\/bin\/ls &gt; \/dev\/null\nCOMPILED=\"${0%.*}\"\nclang \"$0\" -o \"$COMPILED\" -framework Foundation;\n\"$COMPILED\"; rm \"$COMPILED\"; exit;\n*\/\n\n#import &lt;Foundation\/Foundation.h&gt;\n  \nint main(int argc, char *argv[])\n{\n    @autoreleasepool\n    {\n        NSLog(@\"Hello %@\",NSProcessInfo.processInfo.environment[@\"USER\"]);\n    }\n    return 0;\n}\n<\/pre>\n<p>Make the <tt>.m<\/tt> file executable and you can run it from Terminal. Unfortunately, it won&rsquo;t run from BBEdit&rsquo;s #! menu because the file doesn&rsquo;t actually begin with <code>#!<\/code>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Nicolas Bouilleaud has a clever trick for making Objective-C source files self-compiling (via Romain Briche): The idea is to play with shell and C comments so that the file is both a valid shell script and an Objective-C program. The shell script calls clang to compile itself, and then runs the output executable. I&rsquo;ve fixed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[154,30,54,71,318],"class_list":["post-7142","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-bbedit","tag-mac","tag-objective-c","tag-programming","tag-terminal"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/7142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=7142"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/7142\/revisions"}],"predecessor-version":[{"id":7143,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/7142\/revisions\/7143"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=7142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=7142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=7142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}