Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15494352
  • 博文数量: 112
  • 博客积分: 11195
  • 博客等级: 上将
  • 技术积分: 1989
  • 用 户 组: 普通用户
  • 注册时间: 2005-06-20 11:04
文章分类

全部博文(112)

文章存档

2013年(2)

2012年(27)

2011年(6)

2010年(11)

2009年(6)

2007年(7)

2006年(23)

2005年(30)

分类: C/C++

2009-08-20 16:01:14

 

很有趣,在一些场合也很有用的东西,昨天从老张那里淘来了。

test.c

#include <stdio.h>
#include <stdlib.h>

extern char * _binary_test_c_start;
int main()
{
        printf("%s", (char *)&_binary_test_c_start);
}

 

Makefile

 

SRC = test.c
TAR = t
ALL:test.c
        objcopy -I binary -O elf32-i386 -B i386 test.c test.bin
        gcc -o t test.c test.bin

 

objcopy很方便的可以把文本文件做成可以连接器可以连接的对象,进而链进可执行程序里面。

阅读(179280) | 评论(14) | 转发(30) |
给主人留下些什么吧!~~

vivieu2013-09-29 13:57:01

关于“编译通不过”的问题:
1、编译出错如下,在 _binary_test_c_star 前面还有一个 _ 呢
$make
objcopy -I binary -O elf32-i386 -B i386 test.c test.bin
gcc -o t test.c test.bin
/tmp/ccSHEOn3.o:test.c:(.text+0xf): undefined reference to `__binary_test_c_star
t'
collect2: ld 返回 1
make: *** [ALL] 错误 1

2、查看test.bin中的符号,只要一个 _ 前缀
$readelf.exe  -s test.bin

Symbol table '.symtab' contains 5&

lhk_pf2013-04-12 15:59:14

china_ssl:这个怎么用,我试了一下编译都不能通过
用于 80x86 的 Microsoft (R) 32 位 C/C++ 优化编译器 16.00.30319.01 版
版权所有(C) Microsoft Corporation。保留所有权利。

for.c
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:for.exe
for.obj
for.obj : error LNK2019: 无法解析的外部符号 __binary_test_c_start,该符号在函数
_main 中被引用
for.exe : fatal error LNK1120: 1 个无法解析的外部命令

__binary_test_c_start 这个变量要看你的 C文件名字来定 比如你的是 hello.c 的话,变量名字应该是
__binary_hello_c_start 这样的!

回复 | 举报

playmud2011-08-22 15:33:51

lthyxy: BT........

lthyxy2011-08-20 20:41:18

BT...

china_ssl2011-07-15 14:32:35

这个怎么用,我试了一下编译都不能通过
用于 80x86 的 Microsoft (R) 32 位 C/C++ 优化编译器 16.00.30319.01 版
版权所有(C) Microsoft Corporation。保留所有权利。

for.c
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:for.exe
for.obj
for.obj : error LNK2019: 无法解析的外部符号 __binary_test_c_start,该符号在函数
_main 中被引用
for.exe : fatal error LNK1120: 1 个无法解析的外部命令