登录站点

用户名

密码

nios中uclinux的CompileHello[ 2007-4-11 16:41:00 | By: caopengly ]

已有 486 次阅读  2009-09-24 10:30   标签CompileHello  caopengly  nios  uclinux 
 

CompileHello

Version 7, changed by hippo5329@yahoo.com.tw. 01/15/2007.   Show version history

11. To compile a simple program, just add -elf2flt to link flag

Create a file, hello.c

#i nclude <stdio.h>
int main(void)
{
printf("hello world\n");
}

nios2-linux-uclibc-gcc hello.c -o hello -elf2flt

The compiled object format is FLAT.
You may check it with,

nios2-linux-uclibc-flthdr hello

hello
Magic: bFLT
Rev: 4
Build Date: Mon Jun 5 21:49:44 2006
Entry: 0x40
Data Start: 0x4a8c
Data End: 0x5c48
BSS End: 0x7ca8
Stack Size: 0x1000
Reloc Start: 0x5c48
Reloc Count: 0x11e
Flags: 0x1 ( Load-to-Ram )

Then copy hello to the rootfs's bin dir. Rebuild the kernel image for initramfs.
cp hello ~/uClinux-dist-test/romfs/bin
cd ~/uClinux-dist-test
make linux image

Boot nios2 uClinux, and run
hello

The default stack size of application is 4KB, you can change it with -elf2flt="-s <new stack size>" option, eg ,
nios2-linux-uclibc-gcc hello.c -o hello -elf2flt="-s 16000"
will have stack size as 16KiB.

The default include dir search path is /opt/nios2/include or staging_dir/include .
The default library search path is /opt/nios2/lib or staging_dir/lib .
The default apps library is uClibc's libc, so you don't need -lc .
If you use math, you need -lm .
If you use pthread, you need -lpthread .
If you use crypt, you need -lcrypt .

You will need those include headers, too.
The order of librarys is important, the linker will search only one pass by default.

example apps to use button pio.

Add hello as an user apps to uClinux-dist

Follow uClinux-dist/Documentation/Adding-User-Apps-HOWTO .

  1. add a line to user/Makefile , dir_$(CONFIG_USER_HELLO_HELLO) += hello
  2. add a line to misc section of config/config.in, bool 'hello' CONFIG_USER_HELLO_HELLO
  3. mkdir ~/uClinux-dist/user/hello
  4. put hello.c in user/hello dir,
  5. create Makefile in user/hello dir,

EXEC = hello

OBJS = hello.o

all: $(EXEC)

$(EXEC): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)

romfs:
$(ROMFSINST) /bin/$(EXEC)

clean:
-rm -f $(EXEC) *.elf *.gdb *.o

上一篇: [FPGA与EDA]转帖:我们的CPU [ 2007-9-11 11:48:00 | By: caopengly ] 下一篇: uclinux中nios的flashprogrammer[ 2007-4-8 11:59:00 | By: caopengly ]

分享 举报