Archiv für Computer

Zeige Ergebnisse 1 - 15 von 164

Jahre

Monate (2012)

Meaningful C++ error message

During my time as a student at the University of Stuttgart we had to learn the programming language Ada. Back then I was swearing about the compiler error message because they were totally meaningless (at least to me).

I am currently working on a C++ project and I have to say that C++ isn’t an inch better than Ada. Consider the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <Windows.h>
 
namespace Geometry {
  class Polyline { };
}
 
using Geometry::Polyline;
 
namespace OtherNS {
  Polyline* get() {
    return NULL;
  }
}

Trying to compile this code gives you the following error message (on Visual C++ 2010):

using_test.cpp(10): error C2143: syntax error : missing ';' before '*'
using_test.cpp(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
using_test.cpp(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

What? Syntax error? And then you start looking where you missed a ;.

The problem, however, is completely different. It’s because there is a function called Polyline() defined somewhere in Windows.h and now the compiler tries to use this function as return type instead of the class Polyline (but doesn’t say anything about that). <irony>This, of course, becomes totally clear just by reading this extremely meaningful error message.</irony> *sigh*

GCC isn’t better here (in case you were blindly blaming Microsoft for writing bad error messages):

error: ‘Polyline’ does not name a type

By the way, the problem can be solved by placing the using statement inside the OtherNS namespace.

1.Februar 2012

C++ references and inheritance

The last few days I’ve been hunting a bug in a C++ project I’ve been working on. This hunt again showed me how easily you can break C++ programs by accident (something that isn’t possibly in Java or C#). You need to completely understand the inner workings of C++ to avoid such pitfalls.

The whole problem was a result of me thinking that C++ references (&) are just pointers (*) that have some restrictions (e.g. they can’t be NULL). Wrong! What’s even worse: They’re sometimes just pointers with some restriction. This makes them work in some cases but fail in others.

Let me take you on my journey and you’ll hopefully avoid this (very subtle) mistake in your work.

16.Januar 2012

IDisposable, Finalizer, and SuppressFinalize in C# and C++/CLI

The .NET framework features an interface called IDisposable. It basically exists to allow freeing unmanaged resources (think: C++ pointers). In most cases, you won’t need IDisposable when writing C# code. There are some exceptions though, and it becomes more important when writing C++/CLI code.

The help page for IDisposable provides the code for IDisposable‘s default implementation pattern in C#. This article will explain each part of it step by step and also provide the equivalent C++/CLI code in each step.

15.Januar 2012

Facebook and You

Sehr böse ;)

Facebook and You

BTW, ich mag Facebook.

(via Geek & Poke)

11.Januar 2012

Sharing project properties in Visual C++

Everyone who has ever created and managed a C++ project in Visual Studio knows that there are hundreds of compiler switches and options to choose from. While setting the desired values for one project may be ok, it’s quite time-consuming and error-prone to do this for multiple projects. I’m currently working with a solution containing about 30 or so projects that share most of their project settings. I always wished there was a way to sync or share these common settings among the projects in the solution. Fortunately, there is: property sheets. They’re a bit hidden though, so I’ll explain how to use them in this article.

Note: This only applies to C++ and C++/CLI projects. .NET projects (C# and Visual Basic) don’t have that many options to be tweaked and (therefore?) can’t have shared settings.

Note 2: This article describes property sheets as they appear in Visual Studio 2010. They may work slightly different in other versions of Visual Studio.

9.Januar 2012

Passing native pointers across C++/CLI assembly boundaries

C++/CLI allows you to mix native C++ code with managed .NET code (which is extremly nice). Mixing such code also allows you to create methods in .NET class that take or return pointers to native (C++) classes. Unfortunately, this doesn’t work out of the box across assemblies (read: DLLs). If you define a .NET class in one assembly and this class has a method that returns a pointer, you may not be able to use this method from within another C++/CLI assembly.

This article describes the problem and shows solutions.

30.Dezember 2011

Eclipse and the JDK under Mac OS X Lion

Java is open-source, fortunately. Accessing its source code is, unfortunately, not so intuitive on Mac OS X Lion when using Eclipse. This article will guide you through the steps required to be able to view Java’s source code in Eclipse.

28.Dezember 2011

Assign DNS name to virtual machine (here: Parallels Desktop 7)

Assigning a DNS name to a virtual machine can be a convenient thing. Do this is not very complicated, it requires some technical skill though. This tutorial shows how to do this with a Ubuntu 11.10 Linux server running under Parallels Desktop 7 on Mac OS X Lion. The basic principals described in this article work as well for any other combination, but that’s beyond this article (and that’s where your technical skill comes into play).

Editing hidden and locked files on Mac OS X with TextWrangler

Sometimes one needs to edit a system configuration file on Mac OS X. While these files are usually text files, you can’t used Mac OS’ “TextEdit” tool to do this. Fortunately, there is a free alternative to “TextEdit” called TextWrangler.

Important: Make sure that you don’t use the AppStore version as this version doesn’t allow editing locked files. (Apple’s store guidelines prohibit this.)

First, select File –> Open... from the menu. Then, since system configuration files are often hidden or in hidden folders, select Show hidden items at the bottom of the “Open” dialog.

Open Dialog in TextWrangler

Then open the file you want to edit. You may notice that the file is in read-only mode (represented by the icon in the upper left corner).

TextWrangler in read-only mode

To switch to write mode, either click this icon or start editing the file’s content. In both cases you’ll be asked to unlock the file. After that, you can edit the file.

When you’re done, simply save the file. You’ll be asked for your password since you’re editing a system file (that doesn’t belong to you). And that’s it.

Note: The AppStore version doesn’t ask you for your password but instead asks you to save the file under a different name.

22.Dezember 2011

Users vs. Programmers

So true – oh, did I say that out loud? ;)

Programmers vs. Users
6.Dezember 2011

Musik von Old School Elektronik

YouTube Preview Image

14.November 2011

Android Source Code in Eclipse

Google made developing an Android app fairly simple. Everything you need can be downloaded for free from Android’s development site. This includes the Android API, an Android emulator (for running Android apps directly on your computer), and an Eclipse plugin called ADT (Android Developer Tools). However, there’s one thing missing: the Java source code.

The Problem

Despite the fact the Android is (supposed to be) an open source project, the downloads don’t include the source code of Android. So, when opening an Android class in Eclipse, you may see something like this:

"Source not found" error in Eclipse

To solve this problem you had to manually download the whole Android source code (which contains much more than just the Java source code files) and then manually assemble a source files directory from the Java files scattered throughout the Android source tree. Very tedious and certainly not very simple.

The Solution

Fortunately, there is a Google Code project named “adt-addons” aiming to solve this problem. The project actually consists of several Eclipse plugins, but the one you want is called “Android Sources”. You can either search the instructions on the project page, or follow them (nicely formatted) here.

Basically what you need is to install the “Android Sources” plugin (about 170 MB). Here’s the update URL:

http://adt-addons.googlecode.com/svn/trunk/source/com.android.ide.eclipse.source.update/

After installing the plugin, simply restart Eclipse and the Android source code is already attached to the Android classes.

Sources found for Android classes
11.November 2011

Eclipse Plugins

This is just a note to my self of what Eclipse plugins I’ll use. This list may, however, also be useful to you.

  • Subclipse: Subversion support
  • MercurialEclipse: Mercurial support
  • ExploreFS: This plugin allows you to open a file in Windows Explorer/Finder through the context menu.
  • CheckStyle: This plugins allows you define more precise rules for your code style. If a rule is violated, a warning is issued.
  • TestNG: Unit testing framework (much like JUnit)

To see what plugins have already been installed, go to Help –> Install new software… and click on What is already installed? at the bottom of the dialog.

9.November 2011

Projects in Visual C++ 2010 – Part 3: Precompiled Headers

In this part of the “Projects in Visual C++ 2010″ mini series another important aspect of C++ programming is explain: precompiled headers. Precompiled headers (or precompiled header files) in many cases significantly reduce the time needed to compile a project.

Here at work I have a C++ project with about 50 .cpp files in it. The project uses the Qt library and all files only include the absolute minimum of header files required. Without precompiled headers, compiling the project takes about 56 seconds. With precompiled headers, the compile time goes down to about 7 seconds. That’s eight times faster.

Related Articles:

Projects in Visual C++ 2010 – Part 2: Project Dependencies

This article is the second part of the subprojects mini series. The first part was about creating a DLL project. This part will show how to use a DLL library project in another project.

Referencing a library in C++ (or, more specific, with Visual C++) is somewhat cumbersome – or should I say, used to be somewhat cumbersome. Fortunately, with the release of Visual C++ 2010 this has been greatly simplified. This article first shows the old way and then describes the new (simple) way.

Related Articles: