2015/10/29

How to format C and C++ code in Sublime Text

没有评论:
How to format C and C++ code in Sublime Text

I am writing some C code on my mac using Sublime Text and I needed a quick way to format the code for better readability.

I found this package for Sublime Text named SublimeAStyleFormatter:

https://packagecontrol.io/packages/SublimeAStyleFormatter

I’ve installed it with git by going on the packages folder and cloning the git repo:

cd ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/
git clone git://github.com/timonwong/SublimeAStyleFormatter.git


And that was it! I can format my C code by pressing Ctrl + Alt + F.

2015/10/26

Cross-compiling a C application using the Android NDK

没有评论:
1 download ndk
export NDK=~/Downloads/android-ndk-r10e

2 Set cross-compiler for x86 (emulator)
mac:
export NDK_TOOLCHAIN=${NDK}/toolchains/x86-4.8/prebuilt/darwin-x86_64/bin/i686-linux-android-
export NDK_SYSROOT=${NDK}/platforms/android-19/arch-x86

Set cros-compiler for arm (device)
export NDK_TOOLCHAIN=${NDK}/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-
export NDK_SYSROOT=${NDK}/platforms/android-19/arch-arm

3 compile
// hello-world.c
#include <stdio.h>
int main(void)
{
printf("Hello world cross compiled on Android!\n");
 return 0;
}

make CC=${NDK_TOOLCHAIN}gcc CFLAGS=--sysroot=${NDK_SYSROOT} hello-world

4 push to test
$ adb push hello-world /data/

$ adb shell

# /data/hello-world

Hello world cross compiled on Android!

reference:
https://yaapb.wordpress.com/2012/09/27/cross-compiling-a-c-application-using-the-android-ndk/

2015/10/21

Linux ELF 文件操作的一些工具

没有评论:
Linux ELF 文件操作的一些工具

1 file yourlib.so 识别文件格式

nm -g yourlib.so 列举目标文件中的符号,
默认输出这个文件中声明的任何函数和全局变量的名称
nm -g yourlib.so   列出所有 extern & exported symbols
nm -gC  yourlib.so    #for C++
nm -t d -l -S -v ./a.out
nm -Ca lib.so     显示所有symbols and function names
nm -D lib.so      # list symbols in the dynamic symbol table, which you can find its address by dlsym.

3 ldd yourlib.so 解析动态链接二进制文件所依赖的库文件
在OS X上,使用 otool -L yourlib.dylib 可实现类似的功能

4  objdump
Linux 自带, Mac OS 上类似为otool.
objdump -D   lib.so    #display assembler contents for all sections
objdump -tT /lib/i386-linux-gnu/libc.so.6 | grep fopen
objdump -Dslx  lib.so
objdump -x lib.so
objdump -TC lib.so

5 readelf
readelf -Ws libc.so 显示所有symbol tables
readelf -s lib.so 显示所有symbol tables
readelf -h  lib.so     # elf header
readelf -S lib.so      #sections

6 strings yourfile 用于提取文件中的字符串
strings —readix=x  yourfile     显示字符串在文件中位置
-a    扫描整个文件  
-e   也扫描Unicode字符

7   size
size /usr/sbin/httpd
Sample outputs:
   text       data        bss        dec        hex    filename
 314213      12376      13304     339893      52fb5    /usr/sbin/httpd
Where,
text - Actual machine instructions that your CPU going to execute. Linux allows to share this data.
data - All initialized variables (declarations) declared in a program (e.g., float salary=123.45;).
bss - The BSS consists of uninitialized data such as arrays that you have not set any values to or null pointers.

8 elfedit
Linux 自带, 可修改machine type, file type, osabi信息

9 hexdump -C  /bin/ls | more
linux command, 查看二进制信息

refer to:
http://www.tenouk.com/Module000linuxnm3.html