Showing posts with label software development. Show all posts
Showing posts with label software development. Show all posts

Saturday, 5 June 2010

Profiling

http://www.garethstevens.com/products/CSS_Profiling.jpg

To profile your C++ code
  1. Add -pg to your compiling step and your linking step

  2. Run your program

  3. Convert profile information to a text file

    gprof executableFilename > textFilename

    For example,

    gprof Polyworld > gmon.txt

Monday, 17 May 2010

How to Back Trace an Error Caught by MSVCRT


How to Back Trace an Error Caught by MSVCRT
  1. Run your program in a debugger such as GDB
  2. Select Retry
  3. GDB will catch the error message.
  4. Now you can bring up a back trace.


Notes
  • This took me ages. I kept pressing Abort and then getting frustrated when there was no back trace to help me debug
References
  • http://www.codeguru.com/forum/showthread.php?t=315371

How to Build Polyworld for Windows


How to Build Polyworld for Windows
  1. Get Qt and MinGW

    Download qt-sdk-win-opensource-2010.02.1.exe
    Install to d:\work\tools\qt

  2. Get GSL for Windows

    Download gsl-1.8-bin.zip and gsl-1.8-lib.zip files
    Extract to d:\work\tools\gnu32

  3. Get CVS

    Download cvs-1-11-22.zip
    Extract to d:\work\tools\cvs

  4. Get Boost

    Download boost_1_43_0.zip
    Extract to d:\work\tools\boost

  5. Start the Qt Command Prompt

    start > all programs > qt sdk by nokia v2010.02.1 (open source) > qt command prompt

  6. Set up your path

    path=%path%;d:\work\tools\gnuwin32\bin;d:\work\tools\cvs

  7. Download polyworld

    cvs -d:pserver:anonymous@polyworld.cvs.sf.net:/cvsroot/polyworld checkout -P polyworld
    Copy to d:\work\polyworld

  8. Get a Project File and a World File

    Dowload polyworld-modified.zip
    Extract to d:\work\polyworld-modified
    Copy d:\work\polyworld-modified\polyworld.pro to d:\work\polyworld
    Copy d:\work\polyworld-modified\debug\worldfile to d:\work\polyworld\debug

  9. Genrate a Makefile

    Go to d:\work\polyworld
    Double check that all the directories in polyworld.pro are correct for you.
    qmake

  10. Edit the souce code

    See instructions below

  11. Build

    Go to d:\work\polyworld
    mingw32-make
Edit the source code
  1. Some header files are in a different place for MinGW

    Look for the files in d:\work\tools\qt\2010.02.1\mingw

  2. Some header files are not available for MinGW

    Put a #ifdef linux and #endif around them.

  3. Some functions are not available for MinGW

    MinGW does not have drand48().
    Instead use double( rand() ) / double( RAND_MAX )

    MinGW does not have alloca().
    Instead use boost shared arrays

    For the rest, put a #ifdef linux and #endif around them.
    There are some big chunks where you'll have to do this but you won't affect the simulation too much.

    There are some functions for file writing that won't work because Polyworld uses a few commands only available on Linux and Mac. Use the worldfile in polyworld-modified.zip to disable most of them.

  4. Some functions will cause a crash

    There are a few lines in the genome::crossover() that will cause a crash.

  5. There are a few more small changes you'll need to make but I hope this guide has helped you with most of the big ones.
Notes
  • If you have trouble modifying the code, I've included the modifications in polyworld-modified.zip

  • If you have trouble building the code, I've included polyworld.exe and all of the dependencies in polyworld-modified.zip

  • Use the dynamic link libraries in d:\work\tools\qt\2010.02.1\qt\bin and not d:\work\tools\qt\2010.02.1\bin
References
  • http://www.beanblossom.in.us/larryy/BuildingPolyworld.html
  • http://wiki.yak.net/634
  • http://wiki.yak.net/630

How to Build Qt 4.6.2 on Windows XP with MinGW

How to Build Qt on Windows with MinGW:
  1. Download qt-sdk-win-opensource-2010.02.1.exe
  2. Extract to c:\qt
  3. start > all programs > qt sdk by nokia v2010.02.1 (open source) > qt command prompt
  4. mingw32-make confclean
  5. configure
  6. mingw32-make

I use the following command to configure:
  • configure -platform win32-g++ -no-phonon -no-phonon-backend -no-webkit -fast -debug -opensource -shared -no-qt3support -no-sql-sqlite -no-openvg -no-gif -no-libpng -no-libmng -no-libtiff -no-libjpeg -no-script -no-scripttools -nomake examples -nomake demos -no-xmlpatterns

Explanation:
  • -platform win32-g++ (specify your platform and your compiler)
  • -no-phonon (often crashes here)
  • -no-phonon-backend (often crashes here)
  • -no-webkit (often crashes here)
  • -fast (generate Makefiles during the compiling step instead of during the configuring step)
  • -debug
  • -opensource
  • -shared
  • -no-qt3support
  • -no-sql-sqlite
  • -no-openvg (no vector graphics)
  • -no-gif
  • -no-libpng
  • -no-libmng
  • -no-libtiff
  • -no-libjpeg
  • -no-scripttools (often crashes here)
  • -nomake examples (these take ages)
  • -nomake demos (these take ages)
  • -no-xmlpatterns (often crashes here)

Useful commands
  • To bring up the help, pick one of the following switches:

    -h -help -?

  • For example:

    configure -h

References

Related Links

Saturday, 15 May 2010

Polyworld on Windows

After a fair bit of work, I modified Polyworld to work on Windows. I'll post up how I did it soon.

Tuesday, 11 May 2010

How to Run Emacs Windows in a Console


From the command line
  • emacs -nw

How to Fix Emacs Windows Error - Spawning Child Process - Invalid Argument

Emacs Error Message - spawning child process: invalid argument
  • When you call the shell
  • When you call the compiler, such as mingw32-make
Details
  • Emacs Version 23.1 for Windows
  • Windows XP Professional Service Pack 3
  • Powershell Version 1
Solution
  1. Go to the windows console
  2. Enter the command "set" to bring up your system variables
  3. I had shell=c:\progra~1\rational\ration~1\nutcroot\mksnt\sh.exe
  4. Enter "set shell=" to remove the variable
  5. Try Emacs again
Tips
  • Anonymous commentator (18/6/11) found the cause of the error to be a different environmental variable, MKS. I suggest that you examine your other environmental variables if changing SHELL or MKS does not work for you.
Notes
  • This took me a long time to fix and a caused me a lot of stress
  • Please leave a comment if you found this useful

Sunday, 9 May 2010

How to Install Eclipse C++ and MinGW

Step 1 - Install MinGW

Step 2 - Install Eclipse C++
Step 3- Install Java Portable
Step 4 - Start a console
  • start > run > cmd > enter
Step 5 - Set up your path
  • path=c:\java;c:\eclipse;c:\mingw\bin;%path%
Step 6 - Start eclipse from your command line
  • eclipse > enter
Step 7 - Specify your work directory
  • The "workspace launcher" dialog will appear
  • Set workspace to "c:\work"
Step 8 - Create a new project
  • file > new > c++ project
  • set the project name to "hello-c++"
  • set the project type to "makefile project > hello world c++ project"
  • select "finish"
Step 9 - Select the console
  • In there bottom pane there are tabs for "problems", "tasks", "console" and "properties"
  • Select "console"
Step 10 - Change build command from "make" to "mingw32-make"
  • properties > c/c++ build
  • deselect "use default build command"
  • change "make" to "mingw32-make"
Step 11 - Run
  • Click "run hello-c++.exe"
  • The "launch debug configuration selection" dialog will appear
  • Select "mingw gdb debugger"
  • Select "ok"
References

Friday, 7 May 2010

How to Install MinGW

Step 1 - Create directory c:\mingw

Step 2 - Download the following files into c:\mingw
  1. binutils-2.20.1-2-mingw32-bin.tar.gz
  2. gcc-c++-4.5.0-1-mingw32-bin.tar.lzma
  3. gcc-core-4.5.0-1-mingw32-bin.tar.lzma
  4. gdb-7.1-2-mingw32-bin.tar.gz
  5. libexpat-2.0.1-1-mingw32-dll-1.tar.gz
  6. libgcc-4.5.0-1-mingw32-dll-1.tar.lzma
  7. libgmp-5.0.1-1-mingw32-dll-10.tar.lzma
  8. libmpc-0.8.1-1-mingw32-dll-2.tar.lzma
  9. libmpfr-2.4.1-1-mingw32-dll-1.tar.lzma
  10. libstdc++-4.5.0-1-mingw32-dll-6.tar.lzma
  11. make-3.81-20090914-mingw32-bin.tar.gz
  12. mingwrt-3.18-mingw32-dev.tar.gz
  13. mingwrt-3.18-mingw32-dll.tar.gz
  14. w32api-3.14-mingw32-dev.tar.gz

  15. grep-2.5.4-bin.zip
  16. grep-2.5.4-dep.zip
Step 3 - Extract the files you downloaded with 7-Zip 4.65 into c:\mingw

Step 4 - Check that you have the following directories
  1. c:\mingw\bin
  2. c:\mingw\doc
  3. c:\mingw\include
  4. c:\mingw\lib
  5. c:\mingw\libexec
  6. c:\mingw\mingw
  7. c:\mingw\share
Step 5 - Start a console
  • start > run > cmd > enter
Step 6 - Set the system path
  • path=c:\mingw\;%path%
Step 7 - Test
  • Go to directory c:\
  • Enter "mingw32-make --version"
  • You should see, "GNU Make 3.81"

Tuesday, 4 May 2010

I'm currently working on porting the simulation to Windows.

How do I get MinGW and Qt ready?

Thankfully, Peter Dimov pointed out that Qt comes with MinGW.

I downloaded and installed Qt 4.6.2 (qt-sdk-win-opensource-2010.02.1.exe).

Now Firefox Portable does not work. I downloaded and installed Firefox 3.6.3 (Firefox Setup 3.6.3.exe).

I opened the Qt Command Prompt (start->All Programs->Qt SDK by Nokia v2010.02.1 (open source)->Qt Command Prompt).

I created hello-c++.cpp
//c
#include <cstdlib>

// c++
#include <iostream>

using namespace
std;
int
main( int argc, char *argv[] )
{

cout << "Hello World!" << endl;
return
EXIT_SUCCESS;
}
I created a Makefile
all:
mingw32-g++ hello-c++.cpp
I compiled hello-c++

mingw32-make

Success!

I ran hello-c++.exe

Success!

I created hello-qt.cpp
#include <QtGui>

int
main(int argc, char *argv[])
{

QApplication app(argc, argv);
QWidget window;
window.resize(320, 240);
window.show();
window.setWindowTitle(
QApplication::translate("toplevel", "Top-level widget"));
return
app.exec();
}
I compiled a Makefile

qmake -project
qmake

I compiled hello-qt.cpp

mingw32-make release

I compiled the release version because I only had the release version of Qt.

Success!

I ran hello-qt.exe

Error: The application has failed to start because mingwm10.dll was not found.

I copied Qt\2010.02.1\mingw\bin\mingwm10.dll

Error: This application has failed to start because libgcc_s_dw2-1.dll was not found.

I copied Qt\2010.02.1\mingw\bin\libgcc_s_dw2-1.dll

Error: This application has failed to start because QtCore4.dll was not found.

I copied Qt\2010.02.1\bin\QtCore4.dll

Error: The procedure entry point _Z5qFreePv could not be located in the dynamic link library QtCore4.dll.

Thankfully, Tymek pointed out that I needed to use a different QtCore4.dll file

I copied Qt\2010.02.1\qt\bin\QtCore4.dll

Error: This application has failed to start because QtGui4.dll was not found.

I copied Qt\2010.02.1\qt\bin\QtGui4.dll

Success!

Friday, 26 February 2010

Increasing Code Compiling and Linking Speed

(http://imgs.xkcd.com/comics/compiling.png)

Over the last few days I've been working on increasing my code compiling and linking speed. A few of my files were taking about three minutes to compile. In addition, they depended on each other so any changes I made led to long, long periods of waiting.

Total compiling time was about fifteen minutes with about a minute or two of linking time. This was ridiculous but I had been putting off doing anything because the code started off being quite quick to compile and had only been increasing in small increments.

The first thing I did was look for a way to time how long everything was taking and seeing whether I could figure out where to focus my efforts.

I found an article that talked about using the Linux command `time'. This was quite handy because my Makefile compiles each file individually, all I had to do was change from my normal command to:

time g++ -c filename.cpp
time g++ -l filename.o


That gave me individual compilining and linking times. To get the total building time, I changed from my normal command to:

time make

I then went looking for ways to increase my compiling speed. I had a look at an article from EventHelix.

  • The main tip was to move header file inclusion code from header files to implementation files wherever possible.

I started with the file that took the longest to compile and proceeded to move all the header file inclusion code to the implementation files. Instead of having object member variables, I changed to having pointer member variables. This has made a huge difference. I went from a three minute compile time down to about six seconds. My overall compile time went from about fifteen minutes to about one minute.

Another source of long compile times was having lots of template code in header files (due to using Boost Serialization) and having lots of header file inclusion code in header files.

Moving header file inclusion code to header files was my idea and so was having template serialization code in header files. I've had to spend a lot of time fixing this up. Now I'm hoping that going to the other extreme won't come back to bite me.

Friday, 24 July 2009

Examing variables with the print command in gdb

I've been working on some code to allow me to save the state of my simulation to a file. Along the way I needed to check the value of a variable. The strange thing is that I was not able to examine it directly in gdb. However, I could examine it by calling a function which returned the value of the variable. I suspect that I am not understanding the print command in gdb properly.

(gdb) print fMaxCritters
$12 = 0
(gdb) print this->GetMaxCritters()
$13 = 8

Thursday, 5 March 2009

How to Fix undefined reference to vtable

I was working on reorganising some code when I came across the following error which stumped me for ages. I was trying to move a class out of "Simulation.h/Simulation.cp" and into "SceneWindow.h/SceneWindow.cp".

make -k -s
Simulation.o: In function `~TSceneWindow':
/home/binh/Documents/polyworld/app/Simulation.cp:6392: undefined reference to `vtable for TSceneWindow'
/home/binh/Documents/polyworld/app/Simulation.cp:6392: undefined reference to `vtable for TSceneWindow'
/home/binh/Documents/polyworld/app/Simulation.cp:6392: undefined reference to `vtable for TSceneWindow'
/home/binh/Documents/polyworld/app/Simulation.cp:6392: undefined reference to `vtable for TSceneWindow'
/home/binh/Documents/polyworld/app/Simulation.cp:6392: undefined reference to `vtable for TSceneWindow'
Simulation.o:/home/binh/Documents/polyworld/app/Simulation.cp:6392: more undefined references to `vtable for TSceneWindow' follow
collect2: ld returned 1 exit status
make: *** [Polyworld] Error 1
make: Target `first' not remade because of errors.

Compilation exited abnormally with code 2 at Thu Mar  5 02:51:00

The solution was to ensure that I placed a reference to "SceneWindow.h" in my "Makefile". In my case I needed to place the reference in my ".pro" file and then use "qmake" to generate a "Makefile".

This took me hours to figure out. Grr.



9/6/2011

A few people have kindly shared their tips.

Anonymous suggested
  • running qmake and then re-building

miformaprohibidaenlibertad suggested
  • This problem is also related with virtual functions. A common reason of the "vtable" message is not declaring a virtual function equal to zero, for example virtual foo(); instead of virtual foo() = 0;

I suggest that you first make sure that you are including all the necessary header files in your Makefile. I had to do this because I was writing my Makefiles manually.

If you are using Qt, you can rely on qmake to include all the necessary header files in your Makefile as long as you double check that you have included all the necessary header files in your ".pro" file.

Finally, try making your virtual functions into pure virtual functions by setting them equal to zero. I'm not sure whether this will work but it's another good option that you can try.


Friday, 20 February 2009

Todd Howard on iteration


Todd Howard, executive producer and game director of Bethesda Softworks:
"... When you have a big new technology jump and a new machine, you spend a lot of time and money figuring it out and getting to work, and then you're happy. We'd rather spend our time iterating. You don't want to be spending all your time just getting something to work."

Thanks to Will Tuttle & Miguel Lopez at GameSpy.

Wednesday, 10 December 2008

Unit Testing with CppUnit

For the past few days I've been reading the code for Polyworld and figuring out where to make my code additions. Today, I'm at a stage where I know where to make my code additions but I'd like to have a unit testing framework in place.

I reread Exploring the C++ Unit Testing Framework Jungle by Noel Llopis who suggested the following frameworks:
  • CppUnit
  • Boost.Test
  • CppUnitLite
  • CxxTest
I've used CppUnitLite and CxxTest previously but it's been a while and I've forgotten how to use them. I decided not to use CxxTest because I don't want to use Perl for the moment. I decided not to CppUnitLite because I don't want to work on extending the framework for the moment. I decided not to use Boost.Test because I don't want to use Boost for the moment.

That left me with CppUnit

As I was looking around, I also had a look at:
I downloaded and installed CppUnit 1.12.1 from sourceforge and had a look at the documentation. Things were a bit scary so I had a look around for some tutorials and found these:
Müller's example program did not work so I tried Hosken's example program which also did not work. I then removed CppUnit and installed an Ubuntu CppUnit package. Once I did that, both Müller's and Hosken's example programs worked.

Here a few other links I found interesting as I was looking around:
Now all I have to do is figure out some simple ways to avoid the overhead of creating objects that I want to test.

Tuesday, 9 December 2008

Saving Emacs Sessions


There is something very cool about just having a screen of code.

As I've been working with Emacs I'm discovering more and more features. Today it occurred to me that it would be useful to be able to exit Emacs and having everything as it was upon return.

The following is from the GNU Emacs Manual:

51 Saving Emacs Sessions

Use the desktop library to save the state of Emacs from one session to another. Once you save the Emacs desktop—the buffers, their file names, major modes, buffer positions, and so on—then subsequent Emacs sessions reload the saved desktop.


You can save the desktop manually with the command M-x desktop-save. You can also enable automatic saving of the desktop when you exit Emacs, and automatic restoration of the last saved desktop when Emacs starts: use the Customization buffer (see Easy Customization) to set desktop-save-mode to t for future sessions, or add this line in your ~/.emacs file:


(desktop-save-mode 1)

If you turn on desktop-save-mode in your ~/.emacs, then when Emacs starts, it looks for a saved desktop in the current directory. Thus, you can have separate saved desktops in different directories, and the starting directory determines which one Emacs reloads. You can save the current desktop and reload one saved in another directory by typing M-x desktop-change-dir. Typing M-x desktop-revert reverts to the desktop previously reloaded.


Specify the option `--no-desktop' on the command line when you don't want it to reload any saved desktop. This turns off desktop-save-mode for the current session. Starting Emacs with the `--no-init-file' option also disables desktop reloading, since it bypasses the .emacs init file, where desktop-save-mode is usually turned on.


By default, all the buffers in the desktop are restored at one go. However, this may be slow if there are a lot of buffers in the desktop. You can specify the maximum number of buffers to restore immediately with the variable desktop-restore-eager; the remaining buffers are restored “lazily,” when Emacs is idle.


Type M-x desktop-clear to empty the Emacs desktop. This kills all buffers except for internal ones, and clears the global variables listed in desktop-globals-to-clear. If you want this to preserve certain buffers, customize the variable desktop-clear-preserve-buffers-regexp, whose value is a regular expression matching the names of buffers not to kill.


If you want to save minibuffer history from one session to another, use the savehist library.

Saturday, 29 November 2008

To search for a specific string in a specific file type across sub directories

Let's say I want to find the string "invalid" in files of type *.cp across subdirectories.

grep -r --include=*.cp "invalid" .

"-r" means look recursively

"." means look in this directory

You can also add other file types at the same time

grep -r --include=*.cp --include=*.h "invalid" .

To get console emacs

Make sure you have removed emacs.

apt-get install emacs22-nox

Friday, 28 November 2008

Useful Linux Info

Useful commands:
  • To install stuff

    gunzip filename
    tar xvf filename
    ./configure
    make
    sudo make install

  • To uninstall stuff

    sudo make uninstall
Useful bash commands
  • find: Search for files that meet a desired criteria
Useful Links:
  • bash commands - Linux MAN Pages

  • Find Anything From the Terminal

    Oldie but goodie terminal command, find, can be used to locate literally any file on your system. Tech weblog dmiessler.com introduces find and runs through its functionality starting with simple searches and covering very complicated criteria. Find can be used to locate files based on a plethora of criteria that includes name, directory, date and/or time modified, file size, file type, file owner and permissions. What separates find from other searching applications is that any number of arguments can be combined to create very refined search parameters. Find is installed on practically all *nix systems by default.

Useful directories:
  • /media

    Your usb's

  • ~

    Your home directory

Useful Emacs Info

Useful Emacs Links:
  • Vi and Emacs

    NOT "Vi vs. Emacs" -- Despite twenty or thirty years of abuse thrown at each other by adherents of the Church of Vi vs the Church of Emacs, I feel the two editors are complementary, rather than antagonistic. They have a very different "look and feel", but that's not a real reason for choosing one over the other. They were designed for different jobs, they are better at different things, and I use both of them, depending on the job.

  • Is there any way to run console emacs inside KDE?

    You may not have emacs installed. When I run emacs in the console, I get emacs. I do not have xemacs installed, just emacs.

    You might check and see if you actually have emacs on the machine, To check, try "ls -l /usr/bin/emacs. If you have emacs installed you will find that "/usr/bin/emacs" is a link to "emacs-nox. It could also be that /usr/bin/emacs is linked to xemacs on your machine.

  • CS51 Guide to Emacs

    Emacs also integrates with GDB to allow you to look at your code while stepping through your program. To run GDB in Emacs, type M-X gdb , where is the name of your program. If you have a core file, you can open it with M-X gdb where is the name of your core file.

  • Common requests

  • Emacs Quick Reference

  • Modes R' Us

    Telling Emacs about Modes and Files

    To tell Emacs to go into a major mode based on a file's extension, place this in your "~/.emacs" file:

    (setq auto-mode-alist (cons '("\\.java$" . java-mode) auto-mode-alist))
    (setq auto-mode-alist (cons '("\\.cxx$" . c++-mode) auto-mode-alist))
    (setq auto-mode-alist (cons '("\\.hxx$" . c++-mode) auto-mode-alist))
    (setq auto-mode-alist (cons '("\\.c$" . c-mode) auto-mode-alist))
    (setq auto-mode-alist (cons '("\\.pl$" . perl-mode) auto-mode-alist))
    (setq auto-mode-alist (cons '("\\.pm$" . perl-mode) auto-mode-alist))
    (setq auto-mode-alist (cons '("\\.txt$" . text-mode) auto-mode-alist))

  • Emacs Commands List
Useful Emacs Commands
  • To open a file:
    control-x, control-f

  • To close a file:
    control-x, k

  • To quit Emacs:
    control-x, control-c

  • To find out what colours Emacs can display:
    M-x list-colors-display

  • To enter the shell:
    M-x shell

  • To save a file:
    control-x, control-s

  • To compile:
    M-x compile

  • To run a program:
How do I quickly switch between files?

Use Control-x b, filename ENTER. You can also use tab completion.

How do I find a string in the current file?

Use Control-s, filename

How do I find a string across files?