{"id":80,"date":"2011-10-12T00:19:26","date_gmt":"2011-10-11T15:19:26","guid":{"rendered":"http:\/\/nextstep.onionmixer.net\/wordpress\/?p=80"},"modified":"2011-10-13T16:18:49","modified_gmt":"2011-10-13T07:18:49","slug":"appledata-1436-mixing-objective-c-and-c-code","status":"publish","type":"post","link":"http:\/\/nextstep.onionmixer.net\/wordpress\/?p=80","title":{"rendered":"NeXTanswers :: 1436 :: mixing objective-C and C code"},"content":{"rendered":"<p>Q: \u00a0How does one mix C and Objective-C code? \u00a0How can one call Objective-C from C?<br \/>\n<span style=\"color: #3366ff;\">\uc5b4\ub5bb\uac8c\ud558\uba74 Objective \ucf54\ub4dc\uc640 C\ub97c \uac19\uc774 \uc4f8 \uc218 \uc788\ub098\uc694? \uc5b4\ub5bb\uac8c C\uc5d0\uc11c Objective C\ub97c\u00a0\ud638\ucd9c\ud560 \uc218 \uc788\ub098\uc694?\u00a0<\/span><\/p>\n<p>A: \u00a0Just do it! \u00a0C functions can freely send Objective-C messages, and Objective-C methods can freely call C functions. \u00a0 For an example, look at<strong> main() <\/strong>of one of the applications in \/NextDeveloper\/Examples.<br \/>\n<span style=\"color: #3366ff;\">\uc77c\ub2e8 \ud574\ubcf4\uc138\uc694! C\ud568\uc218\ub294 Objective C \uba54\uc138\uc9c0\ub97c \uc804\ub2ec \ud560 \uc218 \uc788\uace0, Objective C \uba54\uc18c\ub4dc\ub294 \uc790\uc720\ub86d\uac8c C\ud568\uc218\ub97c \ud638\ucd9c\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc608\uc81c\ub4e4\uc774 \uc788\uc2b5\ub2c8\ub2e4.\u00a0\/NextDeveloper\/Examples \uc5d0 \ub4e4\uc5b4\uc788\ub294 \uc5b4\ud50c\ub9ac\ucf00\uc774\uc158\ub4e4\uc758 main()\uc744 \ubcf4\uc138\uc694.<\/span><\/p>\n<p>If you place Objective-C code in a &#8220;.c&#8221; file, \u00a0you need to compile the file with the Objective -C option. \u00a0The simplest way to do this is to change the &#8220;.c&#8221; suffix to &#8220;.m&#8221;, and then add the file name under &#8220;.m (other)&#8221; \u00a0in InterfaceBuilder&#8217;s Project Inspector. \u00a0 \u00a0If it&#8217;s not possible to change the suffix, compile the file using the <strong>-ObjC<\/strong> compiler flag.<br \/>\n<span style=\"color: #3366ff;\">\ub2f9\uc2e0\uc758 \ud504\ub85c\uc81d\ud2b8\uc911 &#8220;.c&#8221; \ud30c\uc77c\uc5d0 \ub4e4\uc5b4\uc788\ub294 Objective C\ucf54\ub4dc\uac00 \uc788\ub2e4\uba74, compile\ud560\ub54c file\uacfc \ud568\uaed8 Objective C option\uc774 \ud544\uc694\ud558\uac8c \ub420\uac81\ub2c8\ub2e4. \uac04\ub2e8\ud55c \ubc29\ubc95\uc744 \uc4f0\uc790\uba74 &#8220;.c&#8221; \ud30c\uc77c\uc758 \ud655\uc7a5\uc790\ub97c &#8220;.m&#8221;\uc73c\ub85c \ubc14\uafb8\uace0 Interface Builder\uc758 Project Inspector\uc5d0 &#8220;.m&#8221;\uc73c\ub85c \ubc14\uafbc \ud30c\uc77c\uc774\ub984\uc744 \ucd94\uac00\ud569\ub2c8\ub2e4. \ud655\uc7a5\uc790\ub97c \uc808\ub300 \ubc14\uafc0 \uc218 \uc5c6\ub2e4\uba74 file\uc744 compile\ud560\ub54c -Objc \ucef4\ud30c\uc77c\ub7ec \ud50c\ub798\uadf8\ub97c \uc0ac\uc6a9\ud558\uc2dc\uba74 \ub429\ub2c8\ub2e4.<\/span><\/p>\n<p>You can place any C code in a &#8220;.m&#8221; file without any special handling. \u00a0The C code needs to extern id&#8217;s as per regular C scoping rules.<br \/>\n<span style=\"color: #3366ff;\">\ub2f9\uc2e0\uc740 &#8220;.m&#8221;\ud30c\uc77c\uc5d0 \ubcc4\ub3c4\uc758 \uc5b4\ub5a4 \ud2b9\ubcc4\ud55c \uc870\uc791\ub3c4 \ud558\uc9c0\uc54a\uc740 \uc5b4\ub5a4 C\ucf54\ub4dc\ub3c4 \ub123\uc744 \uc218 \uc788\uc2b5\ub2c8\ub2e4. C\ucf54\ub4dc\ub294 \uc815\uaddc C Scoping \uaddc\uce59\uc5d0 \ub530\ub978 extern id\ub97c \ud544\uc694\ub85c \ud569\ub2c8\ub2e4.<\/span><\/p>\n<p>Only within Objective-C methods can you directly access instance variables and the hidden variable &#8220;self.&#8221; \u00a0You must pass an object to a C function that needs access to it. \u00a0 For example, this C function returns the width of a View:<br \/>\n<span style=\"color: #3366ff;\">\ub2f9\uc2e0\uc740 \uc624\uc9c1 Objective-C \uc758\uba54\uc18c\ub4dc\ub97c \ud1b5\ud574\uc11c\ub9cc \uc778\uc2a4\ud134\uc2a4 \ubcc0\uc218\uc640 \uc228\uaca8\uc9c4 &#8220;self&#8221;\ubcc0\uc218\uc5d0 \uc9c1\uc811 \uc5d1\uc138\uc2a4 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/span><br \/>\n<span style=\"color: #3366ff;\">\ub2f9\uc2e0\uc740 \ubc18\ub4dc\uc2dc \uc811\uadfc\ud558\ub824\ub294 \uac1d\uccb4\uc5d0 \u00a0\uc694\uad6c\uac00 \uc788\ub294\u00a0 C \ud568\uc218\uc5d0\uac8c \uc624\ube0c\uc81d\ud2b8\ub97c \ub118\uaca8\uc57c\ud569\ub2c8\ub2e4(\uc778\uc218\ud615\ud0dc\ub85c). \uc608\ub97c \ub4e4\uc790\uba74 \uc774 C\ud568\uc218\ub294 View\uc758 \ub108\ube44\ub97c \ubc18\ud658\ud569\ub2c8\ub2e4.<\/span><\/p>\n<p>NXCoord widthOfView(View *self) {\u00a0 \u00a0 NXRect b;\u00a0 \u00a0 [self getBounds:&amp;b];\u00a0 \u00a0return b.size.width; }<\/p>\n<p>You may access public instance variables like this:<br \/>\n<span style=\"color: #3366ff;\">\ub2f9\uc2e0\uc774 public \uc778\uc2a4\ud134\uc2a4 \ubcc0\uc218\uc5d0 \uc811\uadfc\ud558\ub824\uba74 \uc774\ub807\uac8c \ud574\ub3c4 \ub429\ub2c8\ub2e4.:<\/span><\/p>\n<p>void setNumber(MyObject *self,int newValue) { \u00a0 \u00a0self-&gt;number = newValue; }<\/p>\n<p>Remember that all instance variables are considered private unless explicitly declared public. \u00a0However, within the confines of a class implementation, all instance variables of that class are considered public. \u00a0So in the implementation for MyControl (a subclass of Control), you could write a C function to set the tag of a MyControl object like this:<br \/>\n<span style=\"color: #3366ff;\">\uae30\uc5b5\ud558\uc138\uc694. \ubaa8\ub4e0 \uc778\uc2a4\ud134\uc2a4 \ubcc0\uc218\ub294 \uba85\uc2dc\uc801\uc73c\ub85c public\uc73c\ub85c \uc120\uc5b8\ub418\uc9c0 \uc54a\ub294\uacbd\uc6b0 private\uc774\ub77c\ub294\uac78 \uc0dd\uac01\ud574\uc57c \ud569\ub2c8\ub2e4. \uadf8\ub7ec\ub098 Class \uad6c\ud604\uc758 \uacbd\uacc4\uc548\uc5d0\uc11c, \ud074\ub798\uc2a4\uc548\uc758 \ubaa8\ub4e0 \uc778\uc2a4\ud134\uc2a4 \ubcc0\uc218\ub294 public\uc774\ub77c\ub294\uac83\ub3c4 \uc0dd\uac01\ud574\uc57c \ud569\ub2c8\ub2e4. (Control\uc758 \uc11c\ube0c\ud074\ub798\uc2a4\uc778)\u00a0MyControl\uc758 \uad6c\ud604\uc5d0\uc11c, \ub2f9\uc2e0\uc740\u00a0MyControl\uac1d\uccb4\uc758 \ud0dc\uadf8\ub97c \uc124\uc815\ud558\ub294 C\ud568\uc218\ub97c \uc791\uc131\ud560 \uc218 \uc788\uc5c8\uc2b5\ub2c8\ub2e4.<\/span><\/p>\n<p>void setTag(MyControl *self,int newTag) { \u00a0self-&gt;tag = newTag; }<\/p>\n<p>QA27<\/p>\n<p>Valid for 1.0, 2.0, 3.0<\/p>\n<p>&nbsp;<\/p>\n<p>jachin, neogeo\ub2d8\uc758 \ub3c4\uc6c0\uc73c\ub85c \ubc88\uc5ed\ub418\uc5c8\uc2b5\ub2c8\ub2e4<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q: \u00a0How does one mix C and Objective-C code? \u00a0How can one call Objective-C from C? \uc5b4\ub5bb\uac8c\ud558\uba74 Objective \ucf54\ub4dc\uc640 C\ub97c \uac19\uc774 \uc4f8 \uc218 \uc788\ub098\uc694? \uc5b4\ub5bb\uac8c C\uc5d0\uc11c Objective C\ub97c\u00a0\ud638\ucd9c\ud560 \uc218 \uc788\ub098\uc694?\u00a0 A: \u00a0Just do it! \u00a0C functions can freely send Objective-C messages, and Objective-C methods can freely call C functions. \u00a0 For an example, look at [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"http:\/\/nextstep.onionmixer.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/80"}],"collection":[{"href":"http:\/\/nextstep.onionmixer.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nextstep.onionmixer.net\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nextstep.onionmixer.net\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/nextstep.onionmixer.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=80"}],"version-history":[{"count":10,"href":"http:\/\/nextstep.onionmixer.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":118,"href":"http:\/\/nextstep.onionmixer.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/80\/revisions\/118"}],"wp:attachment":[{"href":"http:\/\/nextstep.onionmixer.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nextstep.onionmixer.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nextstep.onionmixer.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}