如何让一个c文件 变成可以执行的文件

shankaipingo 2010-05-05 10:10:38
linux内核环境
make 不起作用 说nothing to done啥啥啥 的
make file不会写。

怎么搞使得可以在shell中使用./可以执行一个文件。
...全文
799 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
liranke 2010-05-13
  • 打赏
  • 举报
回复
楼主要做成模块还是程序啊
ronliu 2010-05-13
  • 打赏
  • 举报
回复
hello.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
MODULE_LICENSE("Dual BSD/GPL");

static char *whom = "world";
module_param(whom, charp, 0);
static int howmany = 1;
module_param(howmany, int, 0);

static int hello_init(void)
{
int i;
for( i=0; i<howmany; i++)
printk("(%d) Hello, %s!\n",i,whom);
return 0;
}

static void hello_exit(void)
{
printk("Goodbye!\n");
}

module_init(hello_init);
module_exit(hello_exit);

Makefile



obj-m += hello.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
ydy4618 2010-05-13
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 zhangxuyu1118 的回复:]

准备源文件hello.c,内容:
#include <stdio.h>
void main(void)
{
printf("hello,linux!\n");
}

在源文件同级编译
#gcc -o hello hello.c

运行:
./hello
[/Quote]
正解
zhangxuyu1118 2010-05-12
  • 打赏
  • 举报
回复
准备源文件hello.c,内容:
#include <stdio.h>
void main(void)
{
printf("hello,linux!\n");
}

在源文件同级编译
#gcc -o hello hello.c

运行:
./hello
ericming200409 2010-05-07
  • 打赏
  • 举报
回复
没有写makefile make当然不起作用
louyong0571 2010-05-07
  • 打赏
  • 举报
回复
楼上参数写错了
cancel_li 2010-05-07
  • 打赏
  • 举报
回复
加载*.ko用insmod
cancel_li 2010-05-07
  • 打赏
  • 举报
回复
倒,楼主用的是内核模式,要编成*.ko,然后在shell里install *.ko
如果不是内核模式就直接按上面编译成.o就行,那就不能用printk.
lishaman 2010-05-06
  • 打赏
  • 举报
回复
gcc hello -o hello.c
lishaman 2010-05-06
  • 打赏
  • 举报
回复
我的空间有自己动手写makefile额
yaoike 2010-05-06
  • 打赏
  • 举报
回复
楼主的意思好像是要做成 .ko的模块吗?那个printk也是内核的函数呀?楼主的写法不对吧,要做成模块的话,你看一下ldd3吧,把ldd3的源码下下来看一下里面的 hello.c 就清楚了。
Wenxy1 2010-05-05
  • 打赏
  • 举报
回复
先make clean,然后再make.
怎么都喜欢发到内核版,我一天都要移好多贴子。
shankaipingo 2010-05-05
  • 打赏
  • 举报
回复
老说io.h找不到。全代码如下
#include <asm/io.h>
int main(int argc, char **argv)
{
printk("Main Begin\n");
return 0;
}

include咱写。路径明显不对啊。ko的makefile咱就找到了,单独的C就找不到.h拉???
cj83111 2010-05-05
  • 打赏
  • 举报
回复
gcc test.c -o hello(任意名)
./hello
FoxMessire 2010-05-05
  • 打赏
  • 举报
回复
gcc -o test test.c //把test.c生产test执行文件。
shell中./test可以运行程序了

makefile你还是先看相关的规则,再自己试试
wibnmo 2010-05-05
  • 打赏
  • 举报
回复
要学习下GCC工具的作用。。
spfbc 2010-05-05
  • 打赏
  • 举报
回复
顶ls
steptodream 2010-05-05
  • 打赏
  • 举报
回复
在windows下 你也是编译c文件 然后生成可执行文件吧
linux下也一样 用gcc编译即可
steptodream 2010-05-05
  • 打赏
  • 举报
回复
比如你的c文件是test.c
#gcc -o exefile test.c
#./exefile
shankaipingo 2010-05-05
  • 打赏
  • 举报
回复
怎么搞使得可以在shell中使用./可以执行这个c文件。编译成.o吗?咱编译。
加载更多回复(2)

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧