<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>不然你要我怎么样 &#187; Native</title>
	<atom:link href="http://www.xiangmocheng.com/tag/native/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xiangmocheng.com</link>
	<description>彪悍的代码不需要注释</description>
	<lastBuildDate>Wed, 30 Nov 2011 11:47:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>【转贴】Android编译环境(2) &#8211; 手工编译C模块</title>
		<link>http://www.xiangmocheng.com/2009/06/android-build-environment-how-to-compile-native-c-module-manually/</link>
		<comments>http://www.xiangmocheng.com/2009/06/android-build-environment-how-to-compile-native-c-module-manually/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 12:51:46 +0000</pubDate>
		<dc:creator>xiangmocheng</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Native]]></category>

		<guid isPermaLink="false">http://xiangmocheng.yo2.cn/articles/%e3%80%90%e8%bd%ac%e8%b4%b4%e3%80%91android%e7%bc%96%e8%af%91%e7%8e%af%e5%a2%832-%e6%89%8b%e5%b7%a5%e7%bc%96%e8%af%91c%e6%a8%a1%e5%9d%97.html</guid>
		<description><![CDATA[原文点击这里 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- Android编译环境提供了”showcommands”选项来显示编译命令行，我们可以通过打开这个选项来查看一些编译时的细节。当然，在这之前要把上一篇中的helloworld模块clean: # make clean-helloworld 上面的“make clean-$(LOCAL_MODULE)”是Android编译环境提供的make clean的方式。 接下来使用showcommands选项重新编译helloworld: # make helloworld showcommands build/core/product_config.mk:229: WARNING: adding test OTA key target thumb C: helloworld &#60;= development/hello/hello.c prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc  -I system/core/include   -I hardware/libhardware/include   -I hardware/ril/include   -I dalvik/libnativehelper/include   -I frameworks/base/include   -I external/skia/include   -I out/target/product/generic/obj/include   -I bionic/libc/arch-arm/include   -I bionic/libc/include   -I bionic/libstdc++/include   -I bionic/libc/kernel/common   -I bionic/libc/kernel/arch-arm   -I bionic/libm/include   -I bionic/libm/include/arch/arm   [...]]]></description>
			<content:encoded><![CDATA[<p>原文点击<a href="http://www.top-e.org/jiaoshi/html/?161.html" target="_blank">这里</a></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>Android编译环境提供了”showcommands”选项来显示编译命令行，我们可以通过打开这个选项来查看一些编译时的细节。当然，在这之前要把上一篇中的helloworld模块clean:</p>
<p># make clean-helloworld</p>
<p>上面的“make clean-$(LOCAL_MODULE)”是Android编译环境提供的make clean的方式。</p>
<p>接下来使用showcommands选项重新编译helloworld:</p>
<table class="MsoTableGrid" style="border: medium none; margin: auto auto auto 23.4pt; background: none repeat scroll 0% 0% #b3b3b3; width: 378pt; border-collapse: collapse;" border="1" cellspacing="0" cellpadding="0" width="504">
<tbody>
<tr>
<td style="border: 1pt solid windowtext; padding: 0cm 5.4pt; width: 378pt; background-color: transparent;" width="504" valign="top">
<p># make helloworld showcommands</p>
<p>build/core/product_config.mk:229: WARNING: adding test OTA key</p>
<p>target thumb C: helloworld &lt;= development/hello/hello.c</p>
<p>prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc  -I system/core/include   -I hardware/libhardware/include   -I hardware/ril/include   -I dalvik/libnativehelper/include   -I frameworks/base/include   -I external/skia/include   -I out/target/product/generic/obj/include   -I bionic/libc/arch-arm/include   -I bionic/libc/include   -I bionic/libstdc++/include   -I bionic/libc/kernel/common   -I bionic/libc/kernel/arch-arm   -I bionic/libm/include   -I bionic/libm/include/arch/arm   -I bionic/libthread_db/include   -I development/hello   -I out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates  -c  -fno-exceptions<br />
-Wno-multichar -march=armv5te -mtune=xscale -msoft-float -fpic<br />
-mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector<br />
-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__<br />
-include system/core/include/arch/linux-arm/AndroidConfig.h -DANDROID<br />
-fmessage-length=0 -W -Wall -Wno-unused -DSK_RELEASE -DNDEBUG -O2 -g<br />
-Wstrict-aliasing=2 -finline-functions<br />
-fno-inline-functions-called-once -fgcse-after-reload<br />
-frerun-cse-after-loop -frename-registers -DNDEBUG -UDEBUG -mthumb -Os<br />
-fomit-frame-pointer -fno-strict-aliasing -finline-limit=64      -MD -o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o development/hello/hello.c</p>
<p>target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)</p>
<p>prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-g++<br />
-nostdlib -Bdynamic -Wl,-T,build/core/armelf.x<br />
-Wl,-dynamic-linker,/system/bin/linker -Wl,&#8211;gc-sections<br />
-Wl,-z,nocopyreloc -o<br />
out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld<br />
-Lout/target/product/generic/obj/lib<br />
-Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lstdc++ -lm  out/target/product/generic/obj/lib/crtbegin_dynamic.o         out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o       -Wl,&#8211;no-undefined<br />
prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/../lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a<br />
out/target/product/generic/obj/lib/crtend_android.o</p>
<p>target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld)</p>
<p>out/host/linux-x86/bin/acp<br />
-fpt<br />
out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld<br />
out/target/product/generic/symbols/system/bin/helloworld</p>
<p>target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld)</p>
<p>out/host/linux-x86/bin/soslim<br />
&#8211;strip &#8211;shady &#8211;quiet<br />
out/target/product/generic/symbols/system/bin/helloworld &#8211;outfile<br />
out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld</p>
<p>Install: out/target/product/generic/system/bin/helloworld</p>
<p>out/host/linux-x86/bin/acp<br />
-fpt<br />
out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld<br />
out/target/product/generic/system/bin/helloworld</td>
</tr>
</tbody>
</table>
<p>从上面的命令行可以看到，Android编译环境所用的交叉编译工具链是prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc，-I和-L参数指定了所用的C库头文件和动态库文件路径分别是bionic/libc/include 和out/target/product/generic/obj/lib，其他还包括很多编译选项以及-D所定义的预编译宏。</p>
<p>我们可以利用上面的编译命令，稍加简化来手工编译helloworld程序。先手工删除上次编译得到的helloworld程序：</p>
<table class="MsoTableGrid" style="border: medium none; margin: auto auto auto 23.4pt; background: none repeat scroll 0% 0% #b3b3b3; width: 378pt; border-collapse: collapse;" border="1" cellspacing="0" cellpadding="0" width="504">
<tbody>
<tr>
<td style="border: 1pt solid windowtext; padding: 0cm 5.4pt; width: 378pt; background-color: transparent;" width="504" valign="top">
<p># rm out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o</p>
<p># rm out/target/product/generic/system/bin/helloworld</td>
</tr>
</tbody>
</table>
<p>再用gcc编译，生成目标文件：</p>
<table class="MsoTableGrid" style="border: medium none; margin: auto auto auto 23.4pt; background: none repeat scroll 0% 0% #b3b3b3; width: 378pt; border-collapse: collapse;" border="1" cellspacing="0" cellpadding="0" width="504">
<tbody>
<tr>
<td style="border: 1pt solid windowtext; padding: 0cm 5.4pt; width: 378pt; background-color: transparent;" width="504" valign="top">#<br />
prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -I<br />
bionic/libc/arch-arm/include -I bionic/libc/include -I<br />
bionic/libc/kernel/common   -I bionic/libc/kernel/arch-arm -c  -fno-exceptions<br />
-Wno-multichar -march=armv5te -mtune=xscale -msoft-float -fpic<br />
-mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector<br />
-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__<br />
-include system/core/include/arch/linux-arm/AndroidConfig.h -DANDROID<br />
-fmessage-length=0 -W -Wall -Wno-unused -DSK_RELEASE -DNDEBUG -O2 -g<br />
-Wstrict-aliasing=2 -finline-functions<br />
-fno-inline-functions-called-once -fgcse-after-reload<br />
-frerun-cse-after-loop -frename-registers -DNDEBUG -UDEBUG -mthumb -Os<br />
-fomit-frame-pointer -fno-strict-aliasing -finline-limit=64      -MD -o out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o development/hello/hello.c</td>
</tr>
</tbody>
</table>
<p>与Android.mk编译参数比较，上面主要减少了不必要的-I参数。</p>
<p>接下来生成可执行文件：</p>
<table class="MsoTableGrid" style="border: medium none; margin: auto auto auto 23.4pt; background: none repeat scroll 0% 0% #b3b3b3; width: 378pt; border-collapse: collapse;" border="1" cellspacing="0" cellpadding="0" width="504">
<tbody>
<tr>
<td style="border: 1pt solid windowtext; padding: 0cm 5.4pt; width: 378pt; background-color: transparent;" width="504" valign="top">#<br />
prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -nostdlib<br />
-Bdynamic -Wl,-T,build/core/armelf.x<br />
-Wl,-dynamic-linker,/system/bin/linker -Wl,&#8211;gc-sections<br />
-Wl,-z,nocopyreloc -o<br />
out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld<br />
-Lout/target/product/generic/obj/lib<br />
-Wl,-rpath-link=out/target/product/generic/obj/lib -lc -lm  out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/hello.o<br />
out/target/product/generic/obj/lib/crtbegin_dynamic.o<br />
-Wl,&#8211;no-undefined<br />
./prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/../lib/gcc/arm-eabi/4.2.1/interwork/libgcc.a<br />
out/target/product/generic/obj/lib/crtend_android.o</td>
</tr>
</tbody>
</table>
<p>这里值得留意的是参数“-Wl,-dynamic-linker,/system/bin/linker”，它指定了Android专用的动态链接器/system/bin/linker，而不是通常所用的ld.so。</p>
<p>生成的可执行程序可用file和readelf命令来查看一下：</p>
<table class="MsoTableGrid" style="border: medium none; margin: auto auto auto 23.4pt; background: none repeat scroll 0% 0% #b3b3b3; width: 378pt; border-collapse: collapse;" border="1" cellspacing="0" cellpadding="0" width="504">
<tbody>
<tr>
<td style="border: 1pt solid windowtext; padding: 0cm 5.4pt; width: 378pt; background-color: transparent;" width="504" valign="top">
<p># file out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld</p>
<p>out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld:<br />
ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked<br />
(uses shared libs), not stripped</p>
<p>#  readelf -d out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld |grep NEEDED</p>
<p>0&#215;00000001 (NEEDED)                     Shared library: [libc.so]</p>
<p>0&#215;00000001 (NEEDED)                     Shared library: [libm.so]</td>
</tr>
</tbody>
</table>
<p>这是ARM格式的动态链接可执行文件，运行时需要libc.so和libm.so。“not stripped”表示它还没被STRIP。嵌入式系统中为节省空间通常将编译完成的可执行文件或动态库进行STRIP，即去掉其中多余的符号表信息。在前面“make helloworld showcommands”命令的最后我们也可以看到，Android编译环境中使用了out/host/linux-x86/bin/soslim工具进行STRIP。</p>
<p>有关Android Toolchain的其他一些内容可参考：<a href="http://www.top-e.org/jiaoshi/html/?151.html" target="_self"><strong>Android Toolchain与Bionic Libc</strong></a></p>
<div style="margin-top: 10px">
<p><strong>转载请注明：</strong> 转载自<a href="http://www.xiangmocheng.com/">不然你要我怎么样</a></t>        </br><strong>本文链接地址:</strong> <a href="http://www.xiangmocheng.com/2009/06/android-build-environment-how-to-compile-native-c-module-manually/">【转贴】Android编译环境(2) &#8211; 手工编译C模块</a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.xiangmocheng.com/2009/06/android-build-environment-how-to-compile-native-c-module-manually/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>【转贴】Android编译环境(1) &#8211; 编译Native C的helloworld模块</title>
		<link>http://www.xiangmocheng.com/2009/06/android-build-environment-how-to-compile-native-c-module/</link>
		<comments>http://www.xiangmocheng.com/2009/06/android-build-environment-how-to-compile-native-c-module/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 12:37:27 +0000</pubDate>
		<dc:creator>xiangmocheng</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Native]]></category>

		<guid isPermaLink="false">http://xiangmocheng.yo2.cn/articles/%e3%80%90%e8%bd%ac%e8%b4%b4%e3%80%91android%e7%bc%96%e8%af%91%e7%8e%af%e5%a2%831-%e7%bc%96%e8%af%91native-c%e7%9a%84helloworld%e6%a8%a1%e5%9d%97.html</guid>
		<description><![CDATA[原文点击这里 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; Android 编译环境本身比较复杂，且不像普通的编译环境：只有顶层目录下才有Makefile文件，而其他的每个component都使用统一标准的 Android.mk. Android.mk文件本身是比较简单的，不过它并不是我们熟悉的Makefile，而是经过了Android自身编译系统的很多处理，因此要真正理清楚其中的联系还比较复杂，不过这种方式的好处在于，编写一个新的Android.mk来给Android增加一个新的Component会比较简单。编译Java程序可以直接采用Eclipse的集成环境来完成，这里就不重复了。我们主要针对C/C++来说明，下面通过一个小例子来说明，如何在Android 中增加一个C程序的Hello World： 1. 在$(YOUR_ANDROID)/development目录下创建hello目录，其中$(YOUR_ANDROID)指Android源代码所在的目录。 $ mkdir $(YOUR_ANDROID)/development/hello 2. 在$(YOUR_ANDROID)/development/hello/目录编写hello.c文件，hello.c的内容当然就是经典的HelloWorld程序： 3.在$(YOUR_ANDROID)/development/hello/目录编写Android.mk文件。这是Android Makefile的标准命名，不要更改。Android.mk文件的格式和内容可以参考其他已有的Android.mk文件的写法，针对helloworld程序的Android.mk文件内容如下： 注意上面LOCAL_SRC_FILES用来指定源文件，LOCAL_MODULE指定要编译的模块的名字，下一步骤编译时就要用到。include $(BUILD_EXECUTABLE)表示要编译成一个可执行文件，如果想编译成动态库则可用BUILD_SHARED_LIBRARY，这些可以在$(YOUR_ANDROID)/build/core/config.mk查到。 4. 回到Android源代码顶层目录进行编译： # cd $(YOUR_ANDROID) &#38;&#38; make helloworld 注意make helloworld中的目标名helloworld就是上面Android.mk文件中由LOCAL_MODULE指定的模块名。编译结果如下： target thumb C: helloworld &#60;= development/hello/hello.c target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld) target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld) target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld) Install: out/target/product/generic/system/bin/helloworld 5．如上面的编译结果所示，编译后的可执行文件存放在out/target/product/generic/system/bin/helloworld，通过”adb push”将它传送到模拟器上，再通过”adb shell”登录到模拟器终端，就可以执行了 转载请注明： 转载自不然你要我怎么样 本文链接地址: [...]]]></description>
			<content:encoded><![CDATA[<p>原文点击<a href="http://www.top-e.org/jiaoshi/html/?157.html" target="_blank">这里</a></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>Android 编译环境本身比较复杂，且不像普通的编译环境：只有顶层目录下才有Makefile文件，而其他的每个component都使用统一标准的 Android.mk. Android.mk文件本身是比较简单的，不过它并不是我们熟悉的Makefile，而是经过了Android自身编译系统的很多处理，因此要真正理清楚其中的联系还比较复杂，不过这种方式的好处在于，编写一个新的Android.mk来给Android增加一个新的Component会比较简单。编译Java程序可以直接采用Eclipse的集成环境来完成，这里就不重复了。我们主要针对C/C++来说明，下面通过一个小例子来说明，如何在Android 中增加一个C程序的Hello World：</p>
<p>1. 在<strong>$(YOUR_ANDROID)/development</strong>目录下创建<strong>hello</strong>目录，其中<strong>$(YOUR_ANDROID)</strong>指Android源代码所在的目录。</p>
<p><strong>$ mkdir $(YOUR_ANDROID)/development/hello</strong></p>
<p>2. 在<strong>$(YOUR_ANDROID)/development/hello/</strong>目录编写<strong>hello.c</strong>文件，hello.c的内容当然就是经典的HelloWorld程序：</p>
<pre class="brush: cpp; title: ; notranslate">int main()
{
printf(&quot;Hello World!\n&quot;);
return 0;
}</pre>
<p>3.在<strong>$(YOUR_ANDROID)/development/hello/</strong>目录编写<strong>Android.mk</strong>文件。这是Android Makefile的标准命名，不要更改。Android.mk文件的格式和内容可以参考其他已有的Android.mk文件的写法，针对helloworld程序的Android.mk文件内容如下：</p>
<pre class="brush: cpp; title: ; notranslate">LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
hello.c

LOCAL_MODULE := helloworld
include $(BUILD_EXECUTABLE)</pre>
<p>注意上面LOCAL_SRC_FILES用来指定源文件，LOCAL_MODULE指定要编译的模块的名字，下一步骤编译时就要用到。include $(BUILD_EXECUTABLE)表示要编译成一个可执行文件，如果想编译成动态库则可用BUILD_SHARED_LIBRARY，这些可以在<strong>$(YOUR_ANDROID)/build/core/config.mk</strong>查到。</p>
<p>4. 回到Android源代码顶层目录进行编译：</p>
<p><strong># cd $(YOUR_ANDROID) &amp;&amp; make helloworld</strong></p>
<p>注意make helloworld中的目标名helloworld就是上面Android.mk文件中由LOCAL_MODULE指定的模块名。编译结果如下：</p>
<p><strong>target thumb C: helloworld &lt;= development/hello/hello.c<br />
target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)<br />
target Non-prelinked: helloworld (out/target/product/generic/symbols/system/bin/helloworld)<br />
target Strip: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/helloworld)<br />
Install: out/target/product/generic/system/bin/helloworld</strong></p>
<p>5．如上面的编译结果所示，编译后的可执行文件存放在<strong>out/target/product/generic/system/bin/helloworld</strong>，通过”adb push”将它传送到模拟器上，再通过”adb shell”登录到模拟器终端，就可以执行了
<div style="margin-top: 10px">
<p><strong>转载请注明：</strong> 转载自<a href="http://www.xiangmocheng.com/">不然你要我怎么样</a></t>        </br><strong>本文链接地址:</strong> <a href="http://www.xiangmocheng.com/2009/06/android-build-environment-how-to-compile-native-c-module/">【转贴】Android编译环境(1) &#8211; 编译Native C的helloworld模块</a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.xiangmocheng.com/2009/06/android-build-environment-how-to-compile-native-c-module/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

