{"id":364,"date":"2012-02-17T11:00:22","date_gmt":"2012-02-17T03:00:22","guid":{"rendered":"http:\/\/guoh.ai\/lifelog\/?p=364"},"modified":"2012-06-07T00:31:34","modified_gmt":"2012-06-06T16:31:34","slug":"run-native-c-code-on-android","status":"publish","type":"post","link":"https:\/\/guoh.ai\/lifelog\/2012\/02\/run-native-c-code-on-android\/","title":{"rendered":"\u5728Android\u4e0a\u904b\u884c\u672c\u5730C\u8a9e\u8a00\u4ee3\u78bc(Hello C on Android)"},"content":{"rendered":"<div class=\"gfmr-markdown-container\"><div class=\"gfmr-markdown-source\" style=\"display: none;\">&lt;p&gt;\u7f16\u8bd1\u53ef\u4ee5\u5728Android\u4e0a\u8fd0\u884c\u7684native(C\/C++)\u7a0b\u5e8f Hello C on Android&lt;\/p&gt;\n&lt;p&gt;JNI\u7f16\u7a0b\u90fd\u4f1a\uff0c\u5199\u51fa\u4e00\u4e2a\u5171\u4eab\u5e93(.so)\uff0c\u7136\u540eJava\u4ee3\u7801\u6765\u8c03\u7528&lt;\/p&gt;\n&lt;p&gt;\u73b0\u5728\u6765\u7f16\u8bd1\u4e00\u4e2a\u53ef\u6267\u884c\u7684native\u7a0b\u5e8f\u76f4\u63a5\u5728Android Device\u4e0a\u8fd0\u884c&lt;br \/&gt;\n\u8fd9\u91cc\u4ee5C\u8bed\u8a00\u6765\u6d4b\u8bd5&lt;\/p&gt;\n&lt;p&gt;\u4e00\u6837\u6309\u7167\u901a\u5e38\u7684\u7f16\u7a0b\u6b65\u9aa4\u6765\u8fdb\u884c\uff0c\u7f16\u5199\u6e90\u6587\u4ef6\uff0c\u7528\u7f16\u8bd1\u5668\u7f16\u8bd1\uff0c\u94fe\u63a5\u5668\u94fe\u63a5\uff0c\u7136\u540e\u8fd0\u884c&lt;\/p&gt;\n&lt;p&gt;\u7f16\u5199\u6e90\u6587\u4ef6\u548c\u8fd0\u884c\u7684\u73af\u5883\u90fd\u6bd4\u8f83\u597d\u5f04\uff0c\u7f16\u8bd1\u5668\u548c\u94fe\u63a5\u5668\u8fd9\u91cc\u5c31\u7a0d\u7a0d\u6709\u70b9\u4e0d\u540c\u3002&lt;br \/&gt;\n\u56e0\u4e3aAndroid\u6240\u91c7\u7528\u7684kernel\u4e0d\u662f\u6807\u51c6\u7684Linux Kernel\uff0cC\u5e93\u7528\u7684\u662fBionic(http:\/\/en.wikipedia.org\/wiki\/Bionic_(software))&lt;br \/&gt;\n\u6240\u4ee5\u7528\u666e\u901a\u7684\u4e00\u5957GNU\u5de5\u5177\u7f16\u8bd1\u51fa\u6765\u7684\u662f\u4e0d\u80fd\u5728Android\u4e0a\u9762\u8fd0\u884c\u7684\uff0c\u8fd8\u597dAndroid\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\u6765\u5b8c\u6210\u8fd9\u4e9b\u3002&lt;\/p&gt;\n&lt;p&gt;\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8fbe\u5230\u8fd9\u4e2a\u76ee\u7684\uff1a&lt;br \/&gt;\n1\u3001\u7f16\u8bd1Android ROM\u7684\u65f6\u5019\u4f1a\u751f\u6210\u8fd9\u4e9b\u76f8\u5173\u7684\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u7f16\u8bd1\u751f\u6210\u6211\u4eec\u7684\u7a0b\u5e8f\u3002&lt;br \/&gt;\nhttp:\/\/android-tricks.blogspot.com\/2009\/02\/hello-world-c-program-on-using-android.html&lt;br \/&gt;\nhttp:\/\/plausible.org\/andy\/agcc&lt;br \/&gt;\n\u4e2d\u6587\u7684\u4ecb\u7ecd\u53ef\u4ee5\u770b\u8fd9\u91cchttp:\/\/blog.claudxiao.net\/2011\/10\/android_agcc\/&lt;br \/&gt;\n\u4f46\u662f\u6211\u6d4b\u8bd5\u4e86\u4e0b\uff0c\u6ca1\u6709\u6210\u529f\uff0c\u65f6\u95f4\u6bd4\u8f83\u7d27\u5c31\u6ca1\u6709\u6765\u5177\u4f53\u53bb\u67e5\u539f\u56e0\uff0c\u6709\u7a7a\u518d\u6765\u770b\u770b\u3002&lt;br \/&gt;\n\u4e0b\u9762\u662f\u6211\u7528\u8fd9\u79cd\u65b9\u5f0f\u6240\u4ea7\u751f\u7684\u9519\u8bef\uff0c\u5982\u679c\u4f60\u77e5\u9053\u539f\u56e0\uff0c\u6b22\u8fce\u80fd\u544a\u8bc9\u6211&lt;\/p&gt;\n&lt;pre class=&quot;brush: bash; title: ; notranslate&quot; title=&quot;&quot;&gt;$ agcc.pl -o hello hello.c&lt;\/pre&gt;\n&lt;p&gt;\/home\/guohai\/src\/ics\/prebuilt\/linux-x86\/toolchain\/arm-eabi-4.3.1\/bin\/..\/lib\/gcc\/arm-eabi\/4.3.1\/..\/..\/..\/..\/arm-eabi\/bin\/ld: warning: \/tmp\/cc8eCaMQ.o uses variable-size enums yet the output is to use 32-bit enums; use of enum values across objects may fail&lt;\/p&gt;\n&lt;p&gt;\u66f4\u591a\u5173\u4e8eaggc\u7684\u4fe1\u606f&lt;br \/&gt;\nhttp:\/\/blog.v-lad.org\/archives\/26&lt;\/p&gt;\n&lt;blockquote class=&quot;wp-embedded-content&quot; data-secret=&quot;UB6OQGY7qc&quot;&gt;&lt;p&gt;&lt;a href=&quot;https:\/\/www.pocketmagic.net\/android-c-native-development-take-full-control\/&quot;&gt;Android C native development &#8211; take full control!&lt;\/a&gt;&lt;\/p&gt;&lt;\/blockquote&gt;\n&lt;p&gt;&lt;iframe class=&quot;wp-embedded-content&quot; sandbox=&quot;allow-scripts&quot; security=&quot;restricted&quot; style=&quot;position: absolute; visibility: hidden;&quot; title=&quot;&#8220;Android C native development &#8211; take full control!&#8221; &#8212; PocketMagic&quot; src=&quot;https:\/\/www.pocketmagic.net\/android-c-native-development-take-full-control\/embed\/#?secret=cfg0FMmy1h#?secret=UB6OQGY7qc&quot; data-secret=&quot;UB6OQGY7qc&quot; width=&quot;600&quot; height=&quot;338&quot; frameborder=&quot;0&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; scrolling=&quot;no&quot;&gt;&lt;\/iframe&gt;&lt;br \/&gt;\nhttp:\/\/betelco.blogspot.com\/2010\/01\/buildingdebugging-android-native-c.html&lt;\/p&gt;\n&lt;p&gt;2\u3001\u901a\u8fc7Android NDK\u6765\u7f16\u8bd1\u751f\u6210\uff0c\u8fd9\u4e2a\u6bd4\u8f83\u7b80\u5355&lt;br \/&gt;\n\u6211\u662f\u53c2\u7167http:\/\/ideone.com\/lt6BW\u4fee\u6539\u8fc7\u6765\u7684&lt;br \/&gt;\n\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u811a\u672c\u91cc\u9762\u7684\u8def\u5f84\u90fd\u662fhard code\uff0c\u6211\u4fee\u6539\u8fc7\u7684&lt;a href=&quot;https:\/\/github.com\/guohai\/and-tools\/blob\/master\/agcc&quot; target=&quot;_blank&quot;&gt;\u7248\u672c&lt;\/a&gt;\u53c2\u89c1https:\/\/github.com\/guohai\/and-tools\/blob\/master\/agcc\u3002&lt;br \/&gt;\n\u5bf9\u6bd4\u4e0b\u5e94\u8be5\u5f88\u597d\u6539\u51fa\u81ea\u5df1\u7684\u7248\u672c\uff0c\u4f7f\u7528\u7684\u65f6\u5019\u8981\u8bb0\u5f97\u7ed9\u8fd9\u4e2a\u811a\u672c\u53ef\u6267\u884c\u6743\u9650\uff0c\u5e76\u4e14\u6700\u597d\u628a\u8be5\u811a\u672c\u589e\u52a0\u5230PATH\u5f53\u4e2d\uff0c\u65b9\u4fbf\u540e\u9762\u7684\u4f7f\u7528\u3002&lt;\/p&gt;\n&lt;p&gt;\u4f46\u662f\u76f4\u63a5\u8fd0\u884cagcc\u4f1a\u62a5\u9519\uff0c\u5982\u4e0b&lt;\/p&gt;\n&lt;pre class=&quot;brush: bash; title: ; notranslate&quot; title=&quot;&quot;&gt;$ agcc&lt;\/pre&gt;\n&lt;p&gt;GCC:\/home\/guohai\/dev\/android-ndk-r7\/toolchains\/arm-linux-androideabi-4.4.3\/prebuilt\/linux-x86\/bin\/arm-linux-androideabi-gcc LIB:\/home\/guohai\/vocume\/dev\/android-ndk-r7\/platforms\/android-14\/arch-arm\/usr\/lib LINKER:\/system\/bin\/linker PARAMS:&lt;br \/&gt;\n\/home\/guohai\/dev\/android-ndk-r7\/platforms\/android-14\/arch-arm\/usr\/lib\/crtbegin_dynamic.o: In function `_start&#8217;:&lt;br \/&gt;\n(.text+0x14): undefined reference to `main&#8217;&lt;br \/&gt;\ncollect2: ld returned 1 exit status&lt;br \/&gt;\n\u5982\u679c\u4f60\u53d1\u73b0\u662f\u8fd9\u4e2a\u9519\u8bef\uff0c\u4e0d\u8981\u62c5\u5fc3\uff0c\u53ef\u80fd\u662f\u8fd9\u4e2a\u539f\u56e0http:\/\/www.mobitnt.com\/Blog\/?p=150\u3002&lt;br \/&gt;\n\u53ea\u8981\u4f60\u8f93\u5165\u7684\u547d\u4ee4\u662f\u5b8c\u6574\u7684\uff0c\u6bd4\u5982agcc hello.c -o hello\uff0c\u5e94\u8be5\u5c31\u4e0d\u4f1a\u51fa\u9519\u3002&lt;\/p&gt;\n&lt;p&gt;\u5047\u8bbe\u540e\u9762\u8be5\u811a\u672c\u7684\u4f7f\u7528\u90fd\u662f\u6ee1\u8db3\u4e0a\u9762\u8fd9\u4e9b\u6761\u4ef6\u7684\u3002&lt;\/p&gt;\n&lt;p&gt;\u73b0\u5728\u57fa\u672c\u73af\u5883\u90fd\u642d\u5efa\u597d\u4e86\uff0c\u6211\u4eec\u8fd8\u6ca1\u6709\u6e90\u7a0b\u5e8f\u3002&lt;\/p&gt;\n&lt;p&gt;\u5148\u5728Host Machine\u4e0a\u7f16\u5199\u4e00\u4e2ahello.c\u6587\u4ef6&lt;\/p&gt;\n&lt;pre class=&quot;brush: bash; title: ; notranslate&quot; title=&quot;&quot;&gt;#include &lt;stdio.h&gt;\r\n\r\nmain()\r\n{\r\n    printf (&quot;Hello C on Android!\\n&quot;);\r\n}&lt;\/pre&gt;\n&lt;p&gt;\u7136\u540e\u8fd0\u884cagcc hello.c -o hello\uff0c\u5982\u679c\u6ca1\u6709\u4efb\u4f55\u9519\u8bef\u7684\u8bdd\uff0c\u5c31\u5e94\u8be5\u770b\u5230\u6709\u5982\u4e0b\u7684\u8fd4\u56de\uff0c\u5e76\u4e14\u751f\u6210\u4e86\u4e00\u4e2ahello\u6587\u4ef6\u3002&lt;\/p&gt;\n&lt;pre class=&quot;brush: bash; title: ; notranslate&quot; title=&quot;&quot;&gt;$ agcc hello.c -o hello\r\nGCC:\/home\/guohai\/dev\/android-ndk-r7\/toolchains\/arm-linux-androideabi-4.4.3\/prebuilt\/linux-x86\/bin\/arm-linux-androideabi-gcc LIB:\/home\/guohai\/dev\/android-ndk-r7\/platforms\/android-14\/arch-arm\/usr\/lib LINKER:\/system\/bin\/linker PARAMS:hello.c -o hello&lt;\/pre&gt;\n&lt;p&gt;\u6267\u884c&lt;\/p&gt;\n&lt;pre class=&quot;brush: bash; title: ; notranslate&quot; title=&quot;&quot;&gt;$ file hello\r\nhello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped&lt;\/pre&gt;\n&lt;p&gt;\u8fd4\u56de\u4fe1\u606f\u5e94\u8be5\u662f\u7c7b\u4f3c\u4e0a\u9762\u8fd9\u6837\u7684\u3002&lt;\/p&gt;\n&lt;p&gt;\u73b0\u5728\u5c31\u53ea\u5dee\u6700\u540e\u4e00\u6b65\u4e86\uff0c\u628a\u7a0b\u5e8f\u653e\u5230Android\u4e0a\u53bb\u8fd0\u884c&lt;\/p&gt;\n&lt;pre class=&quot;brush: bash; title: ; notranslate&quot; title=&quot;&quot;&gt;$ adb remount\r\n$ adb shell mkdir \/data\/hello_c\r\n$ adb push hello \/data\/hello_c\r\n$ adb shell&lt;\/pre&gt;\n&lt;pre class=&quot;brush: bash; title: ; notranslate&quot; title=&quot;&quot;&gt;# cd \/data\/hello_c\/\r\n# .\/hello&lt;\/pre&gt;\n&lt;p&gt;\u5e94\u8be5\u5728ADB Shell\u4e0a\u4f1a\u51fa\u73b0&lt;\/p&gt;\n&lt;pre class=&quot;brush: bash; title: ; notranslate&quot; title=&quot;&quot;&gt;Hello C on Android!&lt;\/pre&gt;\n&lt;p&gt;\u8fd9\u6837\u8fd9\u4e2a\u5b9e\u9a8c\u5c31\u7b97\u5b8c\u6210\u4e86\u3002\u540e\u9762\u4f1a\u6709\u5b9e\u9645\u7684\u5e94\u7528\u3002&lt;\/p&gt;\n&lt;p&gt;PS.&lt;br \/&gt;\n\u5982\u679c\u4f60\u76f4\u63a5\u7528gcc\u7f16\u8bd1\u51fa\u6765\uff0c\u653e\u5230Android\u4e0a\u9762\u53bb\u8fd0\u884c\u4f1a\u51fa\u73b0&lt;br \/&gt;\n\/system\/bin\/sh: .\/android-smemcap: not executable: magic 7F45&lt;\/p&gt;\n<\/div><div class=\"gfmr-markdown-rendered\"><p>\u7f16\u8bd1\u53ef\u4ee5\u5728Android\u4e0a\u8fd0\u884c\u7684native(C\/C++)\u7a0b\u5e8f Hello C on Android<\/p>\n<p>JNI\u7f16\u7a0b\u90fd\u4f1a\uff0c\u5199\u51fa\u4e00\u4e2a\u5171\u4eab\u5e93(.so)\uff0c\u7136\u540eJava\u4ee3\u7801\u6765\u8c03\u7528<\/p>\n<p>\u73b0\u5728\u6765\u7f16\u8bd1\u4e00\u4e2a\u53ef\u6267\u884c\u7684native\u7a0b\u5e8f\u76f4\u63a5\u5728Android Device\u4e0a\u8fd0\u884c<br \/>\n\u8fd9\u91cc\u4ee5C\u8bed\u8a00\u6765\u6d4b\u8bd5<\/p>\n<p>\u4e00\u6837\u6309\u7167\u901a\u5e38\u7684\u7f16\u7a0b\u6b65\u9aa4\u6765\u8fdb\u884c\uff0c\u7f16\u5199\u6e90\u6587\u4ef6\uff0c\u7528\u7f16\u8bd1\u5668\u7f16\u8bd1\uff0c\u94fe\u63a5\u5668\u94fe\u63a5\uff0c\u7136\u540e\u8fd0\u884c<\/p>\n<p>\u7f16\u5199\u6e90\u6587\u4ef6\u548c\u8fd0\u884c\u7684\u73af\u5883\u90fd\u6bd4\u8f83\u597d\u5f04\uff0c\u7f16\u8bd1\u5668\u548c\u94fe\u63a5\u5668\u8fd9\u91cc\u5c31\u7a0d\u7a0d\u6709\u70b9\u4e0d\u540c\u3002<br \/>\n\u56e0\u4e3aAndroid\u6240\u91c7\u7528\u7684kernel\u4e0d\u662f\u6807\u51c6\u7684Linux Kernel\uff0cC\u5e93\u7528\u7684\u662fBionic(http:\/\/en.wikipedia.org\/wiki\/Bionic_(software))<br \/>\n\u6240\u4ee5\u7528\u666e\u901a\u7684\u4e00\u5957GNU\u5de5\u5177\u7f16\u8bd1\u51fa\u6765\u7684\u662f\u4e0d\u80fd\u5728Android\u4e0a\u9762\u8fd0\u884c\u7684\uff0c\u8fd8\u597dAndroid\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\u6765\u5b8c\u6210\u8fd9\u4e9b\u3002<\/p>\n<p>\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8fbe\u5230\u8fd9\u4e2a\u76ee\u7684\uff1a<br \/>\n1\u3001\u7f16\u8bd1Android ROM\u7684\u65f6\u5019\u4f1a\u751f\u6210\u8fd9\u4e9b\u76f8\u5173\u7684\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u7f16\u8bd1\u751f\u6210\u6211\u4eec\u7684\u7a0b\u5e8f\u3002<br \/>\nhttp:\/\/android-tricks.blogspot.com\/2009\/02\/hello-world-c-program-on-using-android.html<br \/>\nhttp:\/\/plausible.org\/andy\/agcc<br \/>\n\u4e2d\u6587\u7684\u4ecb\u7ecd\u53ef\u4ee5\u770b\u8fd9\u91cchttp:\/\/blog.claudxiao.net\/2011\/10\/android_agcc\/<br \/>\n\u4f46\u662f\u6211\u6d4b\u8bd5\u4e86\u4e0b\uff0c\u6ca1\u6709\u6210\u529f\uff0c\u65f6\u95f4\u6bd4\u8f83\u7d27\u5c31\u6ca1\u6709\u6765\u5177\u4f53\u53bb\u67e5\u539f\u56e0\uff0c\u6709\u7a7a\u518d\u6765\u770b\u770b\u3002<br \/>\n\u4e0b\u9762\u662f\u6211\u7528\u8fd9\u79cd\u65b9\u5f0f\u6240\u4ea7\u751f\u7684\u9519\u8bef\uff0c\u5982\u679c\u4f60\u77e5\u9053\u539f\u56e0\uff0c\u6b22\u8fce\u80fd\u544a\u8bc9\u6211<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">$ agcc.pl -o hello hello.c<\/pre>\n<p>\/home\/guohai\/src\/ics\/prebuilt\/linux-x86\/toolchain\/arm-eabi-4.3.1\/bin\/..\/lib\/gcc\/arm-eabi\/4.3.1\/..\/..\/..\/..\/arm-eabi\/bin\/ld: warning: \/tmp\/cc8eCaMQ.o uses variable-size enums yet the output is to use 32-bit enums; use of enum values across objects may fail<\/p>\n<p>\u66f4\u591a\u5173\u4e8eaggc\u7684\u4fe1\u606f<br \/>\nhttp:\/\/blog.v-lad.org\/archives\/26<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"UB6OQGY7qc\"><p><a href=\"https:\/\/www.pocketmagic.net\/android-c-native-development-take-full-control\/\">Android C native development &#8211; take full control!<\/a><\/p><\/blockquote>\n<p><br \/>\nhttp:\/\/betelco.blogspot.com\/2010\/01\/buildingdebugging-android-native-c.html<\/p>\n<p>2\u3001\u901a\u8fc7Android NDK\u6765\u7f16\u8bd1\u751f\u6210\uff0c\u8fd9\u4e2a\u6bd4\u8f83\u7b80\u5355<br \/>\n\u6211\u662f\u53c2\u7167http:\/\/ideone.com\/lt6BW\u4fee\u6539\u8fc7\u6765\u7684<br \/>\n\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u811a\u672c\u91cc\u9762\u7684\u8def\u5f84\u90fd\u662fhard code\uff0c\u6211\u4fee\u6539\u8fc7\u7684<a href=\"https:\/\/github.com\/guohai\/and-tools\/blob\/master\/agcc\" target=\"_blank\">\u7248\u672c<\/a>\u53c2\u89c1https:\/\/github.com\/guohai\/and-tools\/blob\/master\/agcc\u3002<br \/>\n\u5bf9\u6bd4\u4e0b\u5e94\u8be5\u5f88\u597d\u6539\u51fa\u81ea\u5df1\u7684\u7248\u672c\uff0c\u4f7f\u7528\u7684\u65f6\u5019\u8981\u8bb0\u5f97\u7ed9\u8fd9\u4e2a\u811a\u672c\u53ef\u6267\u884c\u6743\u9650\uff0c\u5e76\u4e14\u6700\u597d\u628a\u8be5\u811a\u672c\u589e\u52a0\u5230PATH\u5f53\u4e2d\uff0c\u65b9\u4fbf\u540e\u9762\u7684\u4f7f\u7528\u3002<\/p>\n<p>\u4f46\u662f\u76f4\u63a5\u8fd0\u884cagcc\u4f1a\u62a5\u9519\uff0c\u5982\u4e0b<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">$ agcc<\/pre>\n<p>GCC:\/home\/guohai\/dev\/android-ndk-r7\/toolchains\/arm-linux-androideabi-4.4.3\/prebuilt\/linux-x86\/bin\/arm-linux-androideabi-gcc LIB:\/home\/guohai\/vocume\/dev\/android-ndk-r7\/platforms\/android-14\/arch-arm\/usr\/lib LINKER:\/system\/bin\/linker PARAMS:<br \/>\n\/home\/guohai\/dev\/android-ndk-r7\/platforms\/android-14\/arch-arm\/usr\/lib\/crtbegin_dynamic.o: In function `_start&#8217;:<br \/>\n(.text+0x14): undefined reference to `main&#8217;<br \/>\ncollect2: ld returned 1 exit status<br \/>\n\u5982\u679c\u4f60\u53d1\u73b0\u662f\u8fd9\u4e2a\u9519\u8bef\uff0c\u4e0d\u8981\u62c5\u5fc3\uff0c\u53ef\u80fd\u662f\u8fd9\u4e2a\u539f\u56e0http:\/\/www.mobitnt.com\/Blog\/?p=150\u3002<br \/>\n\u53ea\u8981\u4f60\u8f93\u5165\u7684\u547d\u4ee4\u662f\u5b8c\u6574\u7684\uff0c\u6bd4\u5982agcc hello.c -o hello\uff0c\u5e94\u8be5\u5c31\u4e0d\u4f1a\u51fa\u9519\u3002<\/p>\n<p>\u5047\u8bbe\u540e\u9762\u8be5\u811a\u672c\u7684\u4f7f\u7528\u90fd\u662f\u6ee1\u8db3\u4e0a\u9762\u8fd9\u4e9b\u6761\u4ef6\u7684\u3002<\/p>\n<p>\u73b0\u5728\u57fa\u672c\u73af\u5883\u90fd\u642d\u5efa\u597d\u4e86\uff0c\u6211\u4eec\u8fd8\u6ca1\u6709\u6e90\u7a0b\u5e8f\u3002<\/p>\n<p>\u5148\u5728Host Machine\u4e0a\u7f16\u5199\u4e00\u4e2ahello.c\u6587\u4ef6<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">#include &lt;stdio.h&gt;\r\n\r\nmain()\r\n{\r\n    printf (&quot;Hello C on Android!\\n&quot;);\r\n}<\/pre>\n<p>\u7136\u540e\u8fd0\u884cagcc hello.c -o hello\uff0c\u5982\u679c\u6ca1\u6709\u4efb\u4f55\u9519\u8bef\u7684\u8bdd\uff0c\u5c31\u5e94\u8be5\u770b\u5230\u6709\u5982\u4e0b\u7684\u8fd4\u56de\uff0c\u5e76\u4e14\u751f\u6210\u4e86\u4e00\u4e2ahello\u6587\u4ef6\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">$ agcc hello.c -o hello\r\nGCC:\/home\/guohai\/dev\/android-ndk-r7\/toolchains\/arm-linux-androideabi-4.4.3\/prebuilt\/linux-x86\/bin\/arm-linux-androideabi-gcc LIB:\/home\/guohai\/dev\/android-ndk-r7\/platforms\/android-14\/arch-arm\/usr\/lib LINKER:\/system\/bin\/linker PARAMS:hello.c -o hello<\/pre>\n<p>\u6267\u884c<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">$ file hello\r\nhello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped<\/pre>\n<p>\u8fd4\u56de\u4fe1\u606f\u5e94\u8be5\u662f\u7c7b\u4f3c\u4e0a\u9762\u8fd9\u6837\u7684\u3002<\/p>\n<p>\u73b0\u5728\u5c31\u53ea\u5dee\u6700\u540e\u4e00\u6b65\u4e86\uff0c\u628a\u7a0b\u5e8f\u653e\u5230Android\u4e0a\u53bb\u8fd0\u884c<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">$ adb remount\r\n$ adb shell mkdir \/data\/hello_c\r\n$ adb push hello \/data\/hello_c\r\n$ adb shell<\/pre>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"># cd \/data\/hello_c\/\r\n# .\/hello<\/pre>\n<p>\u5e94\u8be5\u5728ADB Shell\u4e0a\u4f1a\u51fa\u73b0<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">Hello C on Android!<\/pre>\n<p>\u8fd9\u6837\u8fd9\u4e2a\u5b9e\u9a8c\u5c31\u7b97\u5b8c\u6210\u4e86\u3002\u540e\u9762\u4f1a\u6709\u5b9e\u9645\u7684\u5e94\u7528\u3002<\/p>\n<p>PS.<br \/>\n\u5982\u679c\u4f60\u76f4\u63a5\u7528gcc\u7f16\u8bd1\u51fa\u6765\uff0c\u653e\u5230Android\u4e0a\u9762\u53bb\u8fd0\u884c\u4f1a\u51fa\u73b0<br \/>\n\/system\/bin\/sh: .\/android-smemcap: not executable: magic 7F45<\/p>\n<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>&lt;p&gt;\u7f16\u8bd1\u53ef\u4ee5\u5728Android\u4e0a\u8fd0\u884c\u7684native(C\/C++)\u7a0b\u5e8f Hello C on Android&lt;\/p&gt; &lt;p&gt;JNI\u7f16\u7a0b\u90fd\u4f1a\uff0c\u5199\u51fa\u4e00\u4e2a\u5171\u4eab\u5e93(.so)\uff0c\u7136\u540eJava\u4ee3\u7801\u6765\u8c03\u7528&lt;\/p&gt; &lt;p&gt;\u73b0\u5728\u6765\u7f16\u8bd1\u4e00\u4e2a\u53ef\u6267\u884c\u7684native\u7a0b\u5e8f\u76f4\u63a5\u5728Android Device\u4e0a\u8fd0\u884c&lt;br \/&gt; \u8fd9\u91cc\u4ee5C\u8bed\u8a00\u6765\u6d4b\u8bd5&lt;\/p&gt; &lt;p&gt;\u4e00\u6837\u6309\u7167\u901a\u5e38\u7684\u7f16\u7a0b\u6b65\u9aa4\u6765\u8fdb\u884c\uff0c\u7f16\u5199\u6e90\u6587\u4ef6\uff0c\u7528\u7f16\u8bd1\u5668\u7f16\u8bd1\uff0c\u94fe\u63a5\u5668\u94fe\u63a5\uff0c\u7136\u540e\u8fd0\u884c&lt;\/p&gt; &lt;p&gt;\u7f16\u5199\u6e90\u6587\u4ef6\u548c\u8fd0\u884c\u7684\u73af\u5883\u90fd\u6bd4\u8f83\u597d\u5f04\uff0c\u7f16\u8bd1\u5668\u548c\u94fe\u63a5\u5668\u8fd9\u91cc\u5c31\u7a0d\u7a0d\u6709\u70b9\u4e0d\u540c\u3002&lt;br \/&gt; \u56e0\u4e3aAndroid\u6240\u91c7\u7528\u7684kernel\u4e0d\u662f\u6807\u51c6\u7684Linux Kernel\uff0cC\u5e93\u7528\u7684\u662fBionic(http:\/\/en.wikipedia.org\/wiki\/Bionic_(software))&lt;br \/&gt; \u6240\u4ee5\u7528\u666e\u901a\u7684\u4e00\u5957GNU\u5de5\u5177\u7f16\u8bd1\u51fa\u6765\u7684\u662f\u4e0d\u80fd\u5728Android\u4e0a\u9762\u8fd0\u884c\u7684\uff0c\u8fd8\u597dAndroid\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\u6765\u5b8c\u6210\u8fd9\u4e9b\u3002&lt;\/p&gt; &lt;p&gt;\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8fbe\u5230\u8fd9\u4e2a\u76ee\u7684\uff1a&lt;br \/&gt; 1\u3001\u7f16\u8bd1Android ROM\u7684\u65f6\u5019\u4f1a\u751f\u6210\u8fd9\u4e9b\u76f8\u5173\u7684\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u7f16\u8bd1\u751f\u6210\u6211\u4eec\u7684\u7a0b\u5e8f\u3002&lt;br \/&gt; http:\/\/android-tricks.blogspot.com\/2009\/02\/hello-world-c-program-on-using-android.html&lt;br \/&gt; http:\/\/plausible.org\/andy\/agcc&lt;br \/&gt; \u4e2d\u6587\u7684\u4ecb\u7ecd\u53ef\u4ee5\u770b\u8fd9\u91cchttp:\/\/blog.claudxiao.net\/2011\/10\/android_agcc\/&lt;br \/&gt; \u4f46\u662f\u6211\u6d4b\u8bd5\u4e86\u4e0b\uff0c\u6ca1\u6709\u6210\u529f\uff0c\u65f6\u95f4\u6bd4\u8f83\u7d27\u5c31\u6ca1\u6709\u6765\u5177\u4f53\u53bb\u67e5\u539f\u56e0\uff0c\u6709\u7a7a\u518d\u6765\u770b\u770b\u3002&lt;br \/&gt; \u4e0b\u9762\u662f\u6211\u7528\u8fd9\u79cd\u65b9\u5f0f\u6240\u4ea7\u751f\u7684\u9519\u8bef\uff0c\u5982\u679c\u4f60\u77e5\u9053\u539f\u56e0\uff0c\u6b22\u8fce\u80fd\u544a\u8bc9\u6211&lt;\/p&gt; &lt;pre class=&quot;brush: bash; title: ; notranslate&quot; title=&quot;&quot;&gt;$ agcc.pl -o hello hello.c&lt;\/pre&gt; &lt;p&gt;\/home\/guohai\/src\/ics\/prebuilt\/linux-x86\/toolchain\/arm-eabi-4.3.1\/bin\/..\/lib\/gcc\/arm-eabi\/4.3.1\/..\/..\/..\/..\/arm-eabi\/bin\/ld: warning: \/tmp\/cc8eCaMQ.o uses variable-size enums yet the output is to use 32-bit &hellip; <a href=\"https:\/\/guoh.ai\/lifelog\/2012\/02\/run-native-c-code-on-android\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">\u5728Android\u4e0a\u904b\u884c\u672c\u5730C\u8a9e\u8a00\u4ee3\u78bc(Hello C on Android)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_gfmr_meta_descriptions":[],"_gfmr_multilingual_taxonomy_terms":[],"footnotes":""},"categories":[7],"tags":[20],"class_list":["post-364","post","type-post","status-publish","format-standard","hentry","category-android","tag-ndk"],"_links":{"self":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/comments?post=364"}],"version-history":[{"count":7,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/364\/revisions"}],"predecessor-version":[{"id":448,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/364\/revisions\/448"}],"wp:attachment":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/media?parent=364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/categories?post=364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/tags?post=364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}