Samuel Jacob's Weblog

Just another technical blog

CodeLite – IDE for C

with 4 comments

For my C projects I was using Notepad++ as my editor/IDE for some time. Although notepad++ is good, it is lacking the integration with a code browser, source control and debugger. I came to know about a new IDE – CodeLitewhen I was searching for ctags for windows. It has integrated support for Subversion, ctags, gdb.

codelite
Since it is advertised to have multi-platform GUI, I was little reluctant to install and try(I have tried lot of multi-platform GUI applications and never satisfied with the look and feel of them). But wxWidgets makes the CodeLite to look like native windows application. CodeLite has lot of features and it looks very similar to Microsoft VC++ IDE.

Code browsing is very easy with CodeLite. It is has a very light explorer and also workspace view through which you can browse your project “easily”. You can switch between header and c file “easily” using the “Swap Header/implementation file” in the context menu. Context menus “Goto declaration” and “Goto Implementation” are handy to browse through code quickly.

If you are using cygwin and custom makefile – you can use a script as custom build command – see Notepad++ as IDE post for more details.

I use the following command to build(because I use gcc cross compiler)
c:\cygwin\bin\bash -l -c 'cd $ACE_ROOT/src;PATH=/usr/cross/i586-elf/bin/:$PATH:;make 2> err; sed -e "s@\(\/cygdrive\/\)\([a-zA-Z]\)@\2:@g" err; rm err’. 
And the following command to clean
c:\cygwin\bin\bash -l -c 'cd $ACE_ROOT/src;PATH=/usr/cross/i586-elf/bin/:$PATH:;make clean’

Codelite supports debugging through gdb; embedded and remote targets also supported. Since I am using a emulator to test my kernel, I have to use remote debugging in GDB to test my OS. Codelite integrates well with GDB remote debugging. I use the following GDB commands as startup gdb commands.

set print pretty on
directory src/kernel

symbol obj/kernel.sys
source img/gdb/general.gdb

source img/gdb/stack.gdb
source img/gdb/vm.gdb

If SVN is configured, using codelite you can see difference between two versions of a same file.
By default codelite uses the default diff viewer. 
It can be changed from Plugins menu -> Subversion->Options and setting the value appropriately. 
I use WinMerge to see difference of source files and have the following settings:
External Diff viewer: C:\Program Files\WinMerge\WinMergeU.exe
Arguments : /dl %bname /dr %mname %base %mine 

Another good thing about CodeLite is it is under development and your bug report/feature requests are solved within short time frame.

You can download CodeLite from www.codelite.org. It has installer for Windows and Linux also.

Written by samueldotj

September 21st, 2008 at 11:33 pm

Posted in C,Softwares,Windows

Source code browsing tool

with 2 comments

In UNIX environment there is the famous “cscope”, to browse the source code and make changes. Using cscope it is easy to browse/edit the source code, cross reference all the functions calling a function, including a file etc.

In windows, I was using notepad++’s find files options browse C source code. Today I decided to find an open source full fledged source navigator for windows.

I started with notepad++ plugin – “Function List”. But it is just not enough for a project source browsing. Google searches gave me cscope port of windows(http://cscope.sourceforge.net/). However I wanted more – the GUI. With few more searches I found Source Navigator – http://sourcenav.sourceforge.net/

Soucrce Navigator is very useful, I just added my project and it give me full list of symbols. Editing a function is just clicking it. Since I am using notepad++ for editing anything, I changed the editor preference to notepad++. To do go to Source Navigator->File menu->Project Preference->Edit-> External editor and give the following line
“C:\Program Files\Notepad++\notepad++.exe” -n%l

Now I can easily browse the source code in Source Navigator and edit it in Notepad++ with few keystrokes.

Written by samueldotj

July 21st, 2008 at 3:22 am

Network Booting

without comments

After creating Ace bootable CD, I decided to try to make Ace bootable from network also to completely avoid creation of disk images. Network booting also helps to boot test machine (real machine) on the same network with ease.

Earlier I had grub installed (on a disk) on a test machine (real machine) with network support. Grub can get the kernel using tftp and boots the system.

Now I wanted to avoid installing grub on a disk. So I had only one option diskless boot. So I decided to use PXE(Preboot eXecution Environment).

To make PXE work, I needed a DHCP server and TFTP server. There are lot of tools available for Linux but for Windows, very very less tools available. After some internet searches, I found and downloaded perfect tools: Dual Server http://sourceforge.net/projects/dhcp-dns-server/ and TFTP Server http://sourceforge.net/projects/tftp-server/ both projects are developed and maintained by Achal Dhir. The other tftp/dhcp servers available windows doesn’t have large set of options provided by Dual Server and TFTP server.

Since I wanted to try PXE boot on emulators, I needed to create software network tap adapter. Without software tap adapter, emulated network card cant talk to the physical network. So I downloaded OpenVPN, which has tool to create tap devices from http://openvpn.net/index.php/downloads.html

If you are using real machine, you may need to download and install PXE roms from etherboot.org http://rom-o-matic.net/

To boot from network, grub should be compiled with appropriate network driver and other flags. Since grub has some problem with compiling under Windows, I used a Linux machine to compile.
cvs -z3 -d:pserver:anonymous@cvs.savannah.gnu.org:/sources/grub co grub
cd grub
./configure –-enable-diskless –-enable-pxe –-enable-rtl8139
You might want to compile in the preset menu also using the –preset option.

Now configure Dual DHCP server to provide IP address, boot file information to qemu
#qemu-ne2k-pci
[52:54:00:12:34:56]
IP_Addr=192.168.2.200
HostName=QemuBox
DNS_Server=192.168.2.10
Router=192.168.2.10
Boot_File=pxegrub-ne2k-pci
Next_Server=192.168.2.10

Then configure TFTP server for the correct tftp home folder.

Now it is time to run, Qemu. Note – Windows port of Qemu 0.9.0 has some problem with obtaining DHCP, so install Qemu 0.9.1 and run the following command, it will boot from network.

qemu -L “$QEMU_BIOS_DIR” -M pc -m 32 -no-kqemu -hdc c.img -boot n -net nic,model=ne2k_pci,macaddr=52:54:00:12:34:56,vlan=0 -net tap,vlan=0,ifname=tap0

I am able to PXE boot qemu only with NE2000 cards, for other cards it failing. PXE boot on vmware only works if I configure grub using ifconfig command. Otherwise vmware machines get assigned with some wrong gateway address. No luck to PXE boot Ace on bochs.

For booting my test machine(real machine), I setup my router (DD-WRT box) with boot-dhcp option in DNSMasq.
http://www.dd-wrt.com/phpBB2/viewtopic.php?t=4662&highlight=pxe

Written by samueldotj

July 5th, 2008 at 2:35 am

Posted in Ace,Tools,Windows

Bootable CD with Grub

without comments

Last week, I decided to boot Ace using CD image rather than floppy image because booting was slow when floppy image is used in bochs.

To create bootable cd with grub, stage2_eltorito should compiled from the grub source. The mkisofs is also needed to create the image. http://smithii.com/cdrtools contains windows cygwin port of the cdrtools which contains mkisofs.

mkdir -p $ACE_ROOT/img/iso/boot/grub
cp $ACE_ROOT/img/boot/grub/stage2_eltorito $ACE_ROOT/img/iso/boot/grub
cp $ACE_ROOT/img/boot/grub/menu.lst $ACE_ROOT/img/iso/boot/grub
cp $ACE_ROOT/obj/kernel.sys $ACE_ROOT/img/iso/

mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o $ACE_ROOT/img/bootcd.iso $ACE_ROOT/img/iso

This image can be written to a CD using any CD burning software.

Written by samueldotj

July 5th, 2008 at 2:08 am

Posted in Ace,kernel,Tools

Bitfield inside a Union

without comments

When coding Ace physical memory manager, I came to a situation where I have to use lot of bit operations on a integer variable. The code looked little ugly because of the explicit bit operations(left shift

typedef struct struct_x
{
 union
 {
  int all;
  int x:1,y:1,z:30,
 };
};

The above code wasn’t working; debugging further I found that the values for bitfields are not calculated correctly.

After some digging I found that, the bitfield hint is ignored by the compiler since it is directly under union. So the compiler treated the fields x,y,z as separate integer instead of considering as one.

I modified the code to pack the bitfields inside a structure and it worked correctly.

typedef struct struct_x
{
 union
 {
  int all;
  struct
  {
   int x:1, y:1, z:30,
  }_;
 };
};

And yes, just an underscore(_) is a valid identifier.

Written by samueldotj

June 4th, 2008 at 11:05 pm

Posted in C,Programming

Configuring Cygwin to use proxy server

with 5 comments

Today I was trying to setup ACPI-CA source tree in my computer using the following command

git clone http://www.acpica.org/repos/acpica.git

but it gave me error when connecting to the site. I knew it was problem with my cygwin network settings. Since I have a proxy server in between, connections internet was failing.

Googleing internet for how to configure cygwin to use proxy didn’t yield any result. Two of my friends with Linux knowledge advised me to set an environment variable (http_proxy) to connect. After exporting it, everything worked fine.

export http_proxy=http://username:password@host:port/

Written by samueldotj

June 4th, 2008 at 4:29 am

Leading underscores in C

without comments

C compilers were prepending an underscore to all the symbols (function, variable names). I read from somewhere this is to avoid name collision with other language libraries. This convention was started with UNIX. When UNIX was written in C to use the existing assembly libraries, C symbols were added with a leading underscore by compilers and linkers.

Currently there is no need for this unless otherwise if you want to use old library. So most of the new C compilers are not adding leading underscores; however it also depends on the platform and object file. For example, Linux a.out GCC does such prepending, while Linux ELF GCC does not.

To explicitly tell the GCC compiler to not to use leading underscore you can use –fno-leading-underscore option.

Note: FORTRAN was adding both leading and trailing underscores to avoid name collision to avoid naming collision with C.

Written by samueldotj

April 16th, 2008 at 11:28 pm

Posted in C,Programming

Notepad++ as IDE

without comments

I have been using Noetpad++ for more than 2 years. It is a wonderful editor – it is very light and feature rich. I use it for editing any text files (txt, ini, C, C++, html, css..). I was happy with Notepad++ for editing my C project source files. Only thing I didn’t like was, to compile I have to go to command prompt and then to correct errors I have to note down the line numbers and open notepad++ modify….

Yesterday, I started searching for a new notepad replacement cum IDE for my Ace(C on cygwin) project. Dev-Cpp and PSPad were IDEs I got. I downloaded and installed both of them. Here comes the problem, both are heavier than notepad++. I might have not explored well, both don’t have option to run cygwin make and capture output and process it.

So I decided to create a notepad++ plugin for my need. But before that wanted to try existing plug-ins and found NppExec. This plug-in allows to run command inside notepad++ and captures the output. It also opens a C file and goto line numbers if it was in the error output if you double click on a line containing “error:” or “warning:” string (I found this one accidently). Downloaded NppExec and ran “make” inside it; but got what I really expected. The output of cygwin “make” has relative path.

i386/io.c:19: error: `Port’ undeclared (first use in this function)
make[1]: *** [i386/io.o] Error 1
make: *** [kernel] Error 2

Since my project make works by invoking “make” inside each directory, relative path will be little complicated – so decided to use absolute path in the output of gcc warning and errors. So modified the explicit rule like following.

$(CC) -c $(abspath $I./include $(CFLAGS)

Out of topic – good make file http://make.paulandlesley.org/autodep.html

Now the output is like the following

/cygdrive/e/sam/Ace3/src/kernel/i386/io.c:19: error: `Port’ undeclared (first use in this function) make[1]: *** [i386/io.o] Error 1 make: *** [kernel] Error 2

This output was not enough because, if I double click this line, notepad tries to open c:/ /cygdrive/e/sam/Ace3/src/kernel/i386/io.c instead of e:/sam/Ace3/src/kernel/i386/io.c.

To solve this problem the I used the following script in notepad++ run command(hotkey – F6)

c:\cygwin\bin\bash -l -c ‘cd src;make 2> err; sed -e “s@\(\/cygdrive\/\)\([a-zA-Z]\)@\2:@g” err; rm err’

Sed help: http://www.ibm.com/developerworks/linux/library/l-sed2.html

Explanation of this command:

c:\cygwin\bin\bash -l –c

Tells NppExec to execute bash and capture its ouput. –l means login and execute bash scripts and profile – I need this for setup my make environment. –c means execute the commands given as argument and exit. Each command is separated by semi-colon.

cd src; – needs no explanation – not needed may be in your case.

make 2> err; – run make and redirect errors to “err” file.

sed -e “s@\(\/cygdrive\/\)\([a-zA-Z]\)@\2:@g” err; – find the pattern /cygdrive/? And replace it with /cygdrive/?:

rm err – remove the error file.

If double clicking on the error/warning message is not opening the file, then try configuring NppExec’s console output filter by using the following %ABSFILE%:%LINE%: warning:* and %ABSFILE%:%LINE%: error:*

Now I am able to create, edit, compile and modify my project without leaving cygwin. Since I am using SVN no problem for source control also. 
Recently I switched to CodeLite and detail are here http://samueldotj.blogspot.com/2008/09/codelite-ide-for-c.html. CodeLite is lightweight IDE.

Written by samueldotj

October 23rd, 2007 at 10:30 pm

Posted in Softwares,Tools,Windows