Posts RSS Comments RSS Del.icio.us Digg Technorati Blinklist Furl reddit 125 Posts and 30 Comments till now
This wordpress theme is downloaded from wordpress themes website.

Archive for the 'Software Development' Category

gcc optimization flags – debug vs release

gcc (or g++ for C++ code) has varying optimization levels.  Specific types of code or target platforms (such as embedded) might benefit from an alternate gcc flag.  But for most situations, you can use –O0 for debug build and –O2 for release build.  The default is –O0.  An old friend (who is an Economics specialist) ran into this because he wanted to write a fairly simple C++ application for Economics data simulation, that would be cross-platform.

His XCode build ran noticeably faster (such as 2 sec vs. 54 sec) than when he used g++ directly on Mac OS X, or g++ in cygwin on Windows, or g++ on Linux.  So I took his initial code and got it to build using Qt Creator 2.0.0 on Windows 7.  I noticed the g++ optimization flags when looking at Qt Creator’s make files (which use qmake and gcc / g++) (notice the -O2 part) (this is from Makefile.Release):

CC            = gcc
CXX           = g++
DEFINES       = -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_THREAD_SUPPORT
CFLAGS        = -O2 -Wall $(DEFINES)
CXXFLAGS      = -O2 -frtti -fexceptions -mthreads -Wall $(DEFINES)

I’m sure I used gcc directly like this at some point (at least in college).  However, if you use a tool that abstracts this (such as Qt Creator, or Linux make files that someone else on the team wrote 5 to 15 years ago) or an alternative (such as Visual Studio C++, C#, Python, Java), then it’s pretty easy to not be aware of this very basic gcc detail.  One of my long-term goals is to continue to be a fairly cross-platform OS user and developer (Windows, Mac, Linux / *nix).

I liked this story since it’s a simple example of how spending just a little bit of time (helping a fellow coder – even if he/she is not a hardcore full-time software developer) or (doing a hobby project) put me just a little outside the daily experience of my day job, and enabled me to learn something useful.  This was also a great excuse to play some more with Qt Creator.

Begin http://www.network-theory.co.uk/docs/gccintro/gccintro_49.html

6.4 Optimization levels

In order to control compilation-time and compiler memory usage, and the trade-offs between speed and space for the resulting executable, GCC provides a range of general optimization levels, numbered from 0–3, as well as individual options for specific types of optimization.

An optimization level is chosen with the command line option -OLEVEL, where LEVEL is a number from 0 to 3. The effects of the different optimization levels are described below:

-O0 or no -O option (default)
At this optimization level GCC does not perform any optimization and compiles the source code in the most straightforward way possible. Each command in the source code is converted directly to the corresponding instructions in the executable file, without rearrangement. This is the best option to use when debugging a program and is the default if no optimization level option is specified.
-O1 or -O
This level turns on the most common forms of optimization that do not require any speed-space tradeoffs. With this option the resulting executables should be smaller and faster than with -O0. The more expensive optimizations, such as instruction scheduling, are not used at this level. Compiling with the option -O1 can often take less time than compiling with -O0, due to the reduced amounts of data that need to be processed after simple optimizations.
-O2
This option turns on further optimizations, in addition to those used by -O1. These additional optimizations include instruction scheduling. Only optimizations that do not require any speed-space tradeoffs are used, so the executable should not increase in size. The compiler will take longer to compile programs and require more memory than with -O1. This option is generally the best choice for deployment of a program, because it provides maximum optimization without increasing the executable size. It is the default optimization level for releases of GNU packages.
-O3
This option turns on more expensive optimizations, such as function inlining, in addition to all the optimizations of the lower levels -O2 and -O1. The -O3 optimization level may increase the speed of the resulting executable, but can also increase its size. Under some circumstances where these optimizations are not favorable, this option might actually make a program slower.
-funroll-loops
This option turns on loop-unrolling, and is independent of the other optimization options. It will increase the size of an executable. Whether or not this option produces a beneficial result has to be examined on a case-by-case basis.
-Os
This option selects optimizations which reduce the size of an executable. The aim of this option is to produce the smallest possible executable, for systems constrained by memory or disk space. In some cases a smaller executable will also run faster, due to better cache usage.

It is important to remember that the benefit of optimization at the highest levels must be weighed against the cost. The cost of optimization includes greater complexity in debugging, and increased time and memory requirements during compilation. For most purposes it is satisfactory to use -O0 for debugging, and -O2 for development and deployment.

End http://www.network-theory.co.uk/docs/gccintro/gccintro_49.html

Begin http://stackoverflow.com/questions/796162/g-compiler-flags-optimization-and-flags-for-making-a-static-library-c

The rule of thumb:

When you need to debug, use -O0 (and -g to generate debugging symbols.)

When you are preparing to ship it, use -O2.

When you use gentoo, use -O3…!

When you need to put it on an embedded system, use -Os (optimize for size, not for efficiency.)

End http://stackoverflow.com/questions/796162/g-compiler-flags-optimization-and-flags-for-making-a-static-library-c

Qt Creator, Promote to

Since I’m new to Qt Creator, I wasn’t sure how to put a custom widget in the designer (outside of manually editing the .ui xml file, which seemed wrong).

Then I found that you can right click a widget (in the UI designer of Qt Creator) and choose "Promote to".

For example, you can promote a QGraphicsView to your custom child class of QGraphicsView (such as MyGraphicsView from mygraphicsview.h).

qt_promote_01

Qt stdout qDebug()

#include <QDebug>
qDebug(“qDebug, printf style, %i\n”, 5);
qDebug() << “qDebug cout style, ” << 5;

I created a new Qt4 Gui Application, and did a printf().  The printf() worked (ie, goes to the Application Output) when I run via “Start Debugging F5″.  However, it didn’t print when I run via “Run, Ctrl+R”.  But qDebug() worked in both cases.  Interesting how qDebug() lets you do both printf() style and cout style.

This got me thinking about my plan for this hobby project application.  For my day job, I use Qt just for the GUI, in a Windows / Linux C++ application (that uses Visual Studio and gcc, STL).  However, for this hobby project, I think I will lean towards using more of the pure Qt stuff (rather than STL).  Partly just to try something different.  Partly because I’m giving this new project a Qt focus, and using Qt Creator.  It will be fun to see how this goes :-)

Qt, More Native, video notes

This is just a dump of the notes I took for this Qt Video:

2009/03/04 video, More Native Look & Feel
* http://qt.nokia.com/developer/learning/online/talks/developerdays2009/tech-talks/how-to-make-your-qt-application-look-native
* Intro, Styles, Style Sheets, Dialogs, Cross platform tips, Native platform tips
* More tips at labs.trolltech.com

The 4 UI guidelines:
* Windows User Experience Interaction Guidelines
* Apple HIG
* KDE UIG
* GNOME HIG

Feel aspects:
* GNOME has (Cancel, OK)
* Windows has (OK, Cancel)

Use Styles:
* style()->drawPrimitive(QStyle:PE_IndicatorBranch, &opt, painter, this);
* QProxyStyle, such as for (Windows Vista) vs. (KDE Oxygen), example:
class MyProxyStyle : public QProxyStyle {
  int styleHint(StyleHint hint, …) const {
    if( hint == QSTyle::SH_UnderlineShortcut )
      return 0;
    return QProxyStyle::styleHint(hint, …);
}

Style Sheets:
* careful – they can break look and feel
* try to blend with system palette
  QString css = "QLabel{ color:palette(highlight); }";
* if you need custom colors
  QColor color(255, 0, 0);
  QString css = QString("QLabel { color:%1;}").arg(color.name())

Dialogs, QDialogButtonBox
* manages button order, layout, icon, text
  QDialogButtonBox box(QDialogButtonBox::Save | QDialogButtonBox::Discard | QDialogButtonBox::Cancel);
* can use custom buttons as well
  QDialogButtonBox box;
  box.addButon(myCustomButton, QDialogButtonBox::AcceptRole);

Dialogs, QFormLayout
* such as for config dialogs
  * Mac labels aligned right, Windows labels aligned left
* manages label alignment
* row wrapping (on embedded devices, it will split the lines)
* implicit buddy assignment

Dialogs, Dialog modality
* he said they are against the user guidelines
* on Snow Leopard (Mac OS X 10.6), they don’t ask you to confirm changes
* Mac apps tend to be a single doc interface
* QDialog methods – use open(), so that on mac os x it will be a sheet
  QDialog::exec() // modal
  QDialog::show() // non modal
  QDialog::open() // window modal
if(!messagebox) {
  messagebox = new QMessageBox(…);
  connect(messagebox, SIGNAL(finished()), this, SLOT(dialogClose(int));
}
messagebox.open()
* if we want a dialog attached to a specific window, use QDialog::open()
* a window is not a sheet, if it’s covering other windows

Cross platform tips, when showing file location in title bar
* Do this:
  setWindowModified(true);
  setWindowFilePath("untitled.txt");
* because
  Windows uses a *
  Mac puts a small dot in the close circle button X
  differences in path in title bar

Cross platform tips, ask for attention
* QApplication::alert(widget, msec = 0);
* makes the windows taskbar flash / mac dock bounce

Cross platform tips, QDesktopServices
* storageLocation(), gives access to default directories, documents, temp, music
* openUrl(), launches URL in default system application
* QUrl from PDF file will open with default app
* openUrl("mailto:myself@gmail.com")
* openUrl("http://qt.nokia.com")
* openUrl(QUrl::fromLocalFile(…))

Cross platform tips, shortcuts
* Qt has QAction::setShortcut() and QAction::setShortcuts(); use setShortcuts()
* QKeySequence::keyBindings(QKeySequence::NextChild)
  Use this to check for shortcut key conflicts
  Test it on all platforms

Cross platform tips, icons
* on windows you’re not supposed to use icons for anything anyway?  what?
* use consistent icon theme (not random conglamoration of icons)
  such as Oxygen (KDE 4), or Tango (free project, sort of GNOME related?)
* QIcon::fromTheme("documents-edit");
  QIcon::fromTheme("documents-edit", QIcon("document.png"));
  new in Qt 4.6
  lets you load an icon from an icon theme
  this is great for KDE and GNOME
  for Windows and Mac, bundle icon theme as a resource
  for KDE, the idea is to use KIcon
  test changing the theme on (such as on GNOME / KDE etc)
* QAction::setIconVisibleInMenu(bool)
  QApplication::Attribute(Qt::AA_DontShowIconsInMenu)
  Mac default is false
  But next version of GTK for GNOME is disabling these – so I should leave it alone
  Windows – he thinks people overuse icons, so I could set it to false, specifically for Windows

Cross platform tips, QAction Priority
* new Qt 4.6
* QAction::setPriority(QAction::Priority)
* allows Qt to collapse layouts intelligently (such as shrink icons, remove icon text, etc)
* decides, for which icons, the label is collapsed
* they plan to add more support for this in the future

Cross platform tips, MDI interfaces
* multiple document interfaces
* don’t use them (for example, use tabbed views instead)
* Mac doesn’t support, GTK+ doesn’t support, Microsoft discourages it (says use MDI)

Native platform tips, use native code
* ifdefs: Q_WS_WIN, W_WS_MAC, Q_WS_X11
* QWidget::winid(), gives window handle (HWND, NSVIEW, X handle)

Native platform tips, Mac MainWindow Demo
* Qt’s main window demo is not very good?
* 29 min 50 sec:
  it’s almost impossible to use the same layout on the mac and say with it straight face that it "looks native"
  Mac tends to require modifications to your interface if you truly want it to look native

Native platform tips, Mac icons
* blowing up a 32×32 icon looks terrible
  he said to use a gray icon for tray – but that’s no longer true?

Native platform tips, Mac QMenuBar
* create a QMenuBar without a parent
* first menu bar created will be the default menu bar
* "Qt Creator doesn’t do this, but it should – we’ll fix that"; is it already fixed?

Native platform tips, Mac unified tool bar
* QMainWindow::setUnifiedTitleAndToolBarOnMac()
* do enable it, but you may have to test / tweak it
* he said he hopes next version of Qt will handle the default better?
* you don’t have room for more than 10 Mac icons

Native platform tips, Mac custom doc menu (sp, dock?)
* QMenu *menu = new QMenu;
  // Add actions to the menu
  extern void qt_mac_set_dock_menu(QMenu *);
  qt_mac_set_dock_menu(menu);

Native platform tips, Mac menu placement
* Qt automatically rearranges menu entries
  based on name: about, settings, preferences, quit, exit
  just use the name to call it "settings" etc, and it will go to Application::preferences on Mac
* override with QAction::menuRole, AboutRole, PreferencesRole, NoRole (means don’t move)

Native platform tips, X11
* Qt 4.6 has KDE improvements
* see freedesktop.org, ?
* make a .desktop file
  handles application icon, menu entry, mimetype
* use standard icons
* test on both GNOME and KDE (and possibly others, such as XFCE)
  different themes, shortcuts, window behavior
* use DESKTOP_SESSION environment variable, returns GNOME or KDE or XFCE
  if we want to do different things for GNOME vs. KDE etc?

Native platform tips, Windows registry via QSettings
* QSettings reads the Windows registry, by default
  QSettings settings("HKEY_CURRENT_USER\\ …", QSettings::NativeFormat);
  bool result = settings.value("EnableBalloonTips", true).toBool();

Native platform tips, Windows QCommandLinkButton
* introduced with Windows Vista, but works just as well on other platforms (Windows 95, Mac, X11)
* great for wizards: instead of having the user do (click radio button, then click next),
  instead just do (click your choice – click just once)

Native platform tips, Windows explorer style look and feel
* Windows native – this guy says it’s lost some of it’s meaning
  he says apps tend to only use the look and feel for certain parts of the GUI
* http://labs.trolltech.com/blogs/2007/06/08/explorer-style-toolbars/
* QtDotNetStyle, Office 2003 style – although it’s going out of fashion

Native platform tips, Windows enable blur behind (Windows Vista, Windows 7)
* not supported in Qt as of 4.6
* but you can use the native Windows API, if you set
  WA_TranslucentBackground and WA_NoSystemBackground, on the widget
* he wrote an example enable blur function, EnableBlur() (sp?)

Screen Shots:

OK and Cancel Buttons
qt_video_notes_01

QDialogButtonBox
qt_video_notes_02

QFormLayout
qt_video_notes_03

Icons
qt_video_notes_04

Cross Platform GUI for Mac

I did a quick prototype using C# and .NET with the Visual Studio 2008 GUI designer.  Then I compiled and ran this on Mac OS X using MonoDevelop.

I had a very positive impression of MonoDevelop.  Though one quirk I ran into was having to do a separate post-build step path for Windows vs. Mac.  Once I got that working, here’s how it looked on Mac OS X:

cross_platform_gui_01

Mac is the most worrisome of the four (Windows 7, Mac OS X, KDE, GNOME) in terms of having a good look and feel for the GUI.  This one didn’t have a good Mac look and feel, because it used Windows Forms (WinForms).  For Mono, you’re encouraged to use GTK#.

I also tried a small Java Swing application.  I created it in NetBeans, then just copied the binaries into Mac OS X.  This had a more Cocoa / Aqua feel to it.  The Menu bar is wrong, but it at least looks a lot better than my WinForms example!

cross_platform_gui_02

Java Swing has some official doc from Apple.  On the other hand, there are also arguments in favor of Java SWT being more native.

Next, I looked at Qt.  At this point, I think I’m most likely to end up using (Qt, Qt Creator, C++).  Some are claiming that a cross platform desktop GUI will be wrong (somehow), and you should just do 4 separate native GUIs, or at least 2 separate native GUIs (one for Windows / Linux, and one for Mac OS).  However, I think this really depends on your goals, and on the specific application you are developing.  Additionally, I’m confident that in many situations, either path can yield good results (or bad results).  For my current particular hobby project, I am pretty optimistic about Qt.

Someone on the qt-creator list said he heard there are font rendering problems, so I tested this (on Snow Leopard), and I didn’t see anything wrong with it: XCode, Text Wrangler, Qt sample app (I ran it from Qt Creator):

cross_platform_gui_03

cross_platform_gui_04

cross_platform_gui_05

There’s just a little info / summary about my investigation.  Some links to check out:

* Qt is Mac OS X Native: http://doc.trolltech.com/4.6/qtmac-as-native.html

* Qt, 2009/03/04 video, More Native Look & Feel:

http://qt.nokia.com/developer/learning/online/talks/developerdays2009/tech-talks/how-to-make-your-qt-application-look-native

* The HIG’s: Windows User Experience Interaction Guidelines, Apple HIG, GNOME HIG, KDE HIG

Cross Platform GUI toolkit choices

I know they aren’t the only choices, but here’s the ones I looked at.

My top 3 ideas are:
1) Qt, C++, Qt Creator (or Visual Studio / gcc / etc)
2) Swing, Java, Eclipse (or NetBeans, or IntelliJ)
3) SWT, Java, Eclipse (or NetBeans, or IntelliJ) (JFace rather than SWT?)

Some ideas that get honorable mention:
A) multiple native GUI toolkits (Windows, WPF, Expression Blend) (Mac OS X, Cocoa, Interface Builder) (GNOME, GTK+) (KDE, Qt)
B) REAL Studio (formerly REALBasic)
C) GTK#, C# and .NET, Mono

An absolute minimum is to have very correct native visual style for the widgets (Mac OS X – Cocoa Aqua ?) (Windows – not sure if it’s Windows Forms or WPF ?) (GNOME – GTK+ ?) (KDE – Qt ?).  Beyond that, it should do things like dialogs where the position of the (OK, Cancel) buttons is correct (for Windows 7, Mac OS X, GNOME, KDE), which I know Qt at least tries to do.  Probably a native app should use different defaults shortcuts for each OS.

I saw that Apple supports Swing, and that SWT has a native widget focus.  However, I don’t know how well they compare to Qt (especially on Mac OS X) for these things.

I may have ruled out REAL Studio too soon, but I’m not enthused about that path unless it’s really a lot better than the other choices (such as Qt, Swing, SWT).

My first cross-platform GUI desktop application is just a simple specialized object based image drawing utility (you can open a png image and draw objects on it, like rectangles or polygons, and resize / move / delete / copy etc these objects).

The places I got the most information on this were:
1) doc from Apple, Qt, etc (Qt had some good videos)
2) trying it out myself (and looking at screen shots)
3) the official mailing lists, such as qt-interest and mono-osx

At this point I’m leaning towards Qt.  More on that in my next post.

Hobby Projects – Software Development

As someone who is passionate about software development, most of my life is pretty engrossed in software / computers / technology.  In addition to things like this blog, keeping up on technology news, having technology-related hobbies, trying out new software, organizing my digital life…  I often do side projects, scripting / automation projects, and learning / studying that is not necessarily for immediate direct application to my current day job.

Some recent examples include:
* My enthusiasm for AutoHotkey scripts
* file organizing / sync / backup (and lists of applications)
* notes / note taking
* this blog / website
* a game prototype with Torque 3D, and continued attention to electronic games (and to the computer / technology industry)
* increased use and awareness of mobility computing (devices / mobile OS)
* more cross platform (Windows 7, Mac OS X, GNOME, KDE) on a daily basis, and use / develop cross platform (I use a hardware KVM)
* Try out other IDE’s / languages than what I normally use.  I normally use Visual Studio with C++.  And also (C#, Python, Java) and (EditPad text editor).  I’ve also tried out, for example, (RenderMonkey, Eclipse, NetBeans, Adobe Flex, Windows Mobile in Visual Studio, XCode / Interface Builder).
* more and deeper use of subversion
* these are just a few examples that I pulled out from memory

This small list is mostly for nights / weekends, which is separate from the work / study I do for my day job.  However, there is definitely plenty of overlap, and of course all of it helps me grow as a software developer.

Sometimes I use the phrase “side projects” to include a broad range of (programming hobby projects, scripting / automation projects, trying out hardware / software, or in some way attempting to improve my current hardware / software setups).  Other times I use the phrase “hobby projects” to only mean actual software development with a significant project size.  I might not be consistent on my terminology.  Anyway…

My next few posts are going to relate to a new software development hobby project, with a significant focus on using (Qt, C++, Qt Creator) for (cross platform GUI application development).

Java with NetBeans IDE, import library JAR

I used Java a lot in college…  But it’s been a while.  I’m trying out NetBeans IDE (for both Java and Python).  Here’s some notes from writing my Java application.

System.out.println(), System.out.print()

Add a .jar library:
1. Right-click the Libraries node and choose "Add JAR/Folder".
2. Browse to and select the .jar file(s).  It didn’t work when I added it as a folder, so i strongly recommend you add each .jar file separately.
javaNetBeans01[4]

3. Right-click the Libraries node and choose Properties
4. Choose the file you added (for each file), then Edit, then specify the Javadoc and Sources
javaNetBeans02[6]
5. In your java code, import the package.  The package names should be specified in the java doc.  In my example, this would be (import net.sf.sevenzipjbinding.*;) or (import net.sf.sevenzipjbinding.SevenZip;) etc.

Shift+F11 to build main project.  F6 to run main project.

Pipe to clipboard (clip / xclip / pbcopy)

On Windows, try: echo hello | clip

On Linux, try: echo hello | xclip

On Mac OS X, try: echo hello | pbcopy

For example, you might do (cat myFile.txt | xclip).  This would basically allow you to edit the clipboard directly.

Kindle SDK (KDK) apps have 100 KB/mo max

The biggest let-down is the “use less than 100KB/user/month" part, because Kindle doesn’t have WiFi – at least not yet.  Two applications I would most imagine myself using on a Kindle are read it later (or instapaper) and EverNote (or something that can import my OneNote notes), but 100 KB/user/month wouldn’t be enough.  Hopefully the next version of the Kindle will have WiFi, and allow applications that use unlimited data over WiFi.

Since it’s short, I’ll just quote the entire thing from:

http://www.amazon.com/gp/feature.html/?ie=UTF8&docId=1000476231

KDK Limited Beta Coming Next Month

Submit your e-mail address to get notified when the limited beta starts next month. Participants in the limited beta will be able to download the Kindle Development Kit, access developer support, test content on Kindle, and submit finished content. Those wait-listed will be invited to participate at a later date as space becomes available. The Kindle Development Kit includes sample code, documentation, and the Kindle Simulator, which helps developers build and test their content by simulating the 6-inch Kindle and 9.7-inch Kindle DX on Mac, PC, and Linux desktops. We are excited to see what you invent for Kindle.

Revenue Share

User revenue will be split 70% to the developer and 30% to Amazon net of delivery fees of $0.15 / MB. Remember that unlike smart phones, the Kindle user does not pay a monthly wireless fee or enter into an annual wireless contract. Kindle active content must be priced to cover the costs of downloads and on-going usage.

Pricing Options

Active content will be available to customers in the Kindle Store later this year. Your active content can be priced three ways:

  • Free – Active content applications that are smaller than 1MB and use less than 100KB/user/month of wireless data may be offered at no charge to customers. Amazon will pay the wireless costs associated with delivery and maintenance.
  • One-time Purchase – Customers will be charged once when purchasing active content. Content must have nominal (less than 100KB/user/month) ongoing wireless usage.
  • Monthly Subscription – Customers will be charged once per month for active content.

Active content applications have an upper size limit of 100MB. Applications larger than 10MB will not be delivered wirelessly but can be downloaded from the Kindle Store to a computer and transferred to the user’s Kindle via USB.

Developer Guidelines

Voice over IP functionality, advertising, offensive materials, collection of customer information without express customer knowledge and consent, or usage of the Amazon or Kindle brand in any way are not allowed. In addition, active content must meet all Amazon technical requirements, not be a generic reader, and not contain malicious code.

We will work to refine the above guidelines throughout the beta.

Next »