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 .
- add a line to user/Makefile , dir_$(CONFIG_USER_HELLO_HELLO) += hello
- add a line to misc section of config/config.in, bool 'hello' CONFIG_USER_HELLO_HELLO
- mkdir ~/uClinux-dist/user/hello
- put hello.c in user/hello dir,
- 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
http://blog.eccn.com/u/20/cmd.htm?do=blogs&id=29&uid=20