OpenWrt Forum Archive

Topic: A question of cross-compile and

The content of this topic has been archived on 29 Mar 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I've got two files named "hello.c" and "hello1.c"
In hello.c I have:
#include <stdio.h>
main()
{
  printf("Hello World!\n");
}

In hello1.c I have:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/sysctl.h>
#include <asm/io.h>
#include <typedefs.h>
#include <bcm4710.h>
#include <sbutils.h>


static int __init diag_init()
{
                printk("Hello World!\n");
    return 0;
}

static void __exit diag_exit()
{
    printk("Byebye!\n");
}

module_init(diag_init);
module_exit(diag_exit);

I can use mipsel-uclibc-gcc to compile hello.c to a binary file hello and then use wget to download it to WRT54G and then execute it. It works well.

However, I can only use mipsel-uclibc-gcc to compile hello1.c to hello1.o
and then use
    insmod helloworld.o
    rmmod helloworld
    dmesg
to get the result.

So what's the difference between two files?
How can mipsel-uclibc-gcc tell the difference between them?

hello.c is a userspace program to be compiled using the following command :

mipsel-linux-uclibc-gcc hello.c -o hello

hello1.c is a kernel module that needs to be compiled with various CFLAGS, such as -D__KERNEL__

If you use the file command to determine what kind of file they are. hello is a MIPS executable, and hello1.o is a MIPS relocatable object.

The discussion might have continued from here.