Archive for the ‘Programming’ Category

Shared post: Using SCons effectively

June 1, 2016

An overall interesting read to understand core concepts of SCons:

http://blog.bfitz.us/?p=1679

 

Advertisements

C: Unit test of static functions (module local)

July 10, 2015

When you want to unit test the functions you do not export, i.e., you have defined them with the “static” keyword, there are three ways to accomplish this:

  1. Add the unit tests at the end of the file containing the static functions.
  2. Expose the function declarations in the header (probably guarded by some #ifdef UNIT_IMPL), and instead of writing static, use a preprocessor definition like VISIBILITY, which you redefine to an empty string if you want to build your unit test suite. Include the header in your separate unit test file.
  3. Include the file under test in the unit test file:

    // content of module_test.c:
    #include "module.c"
    // ...
  4. I find the 3rd way to be the most elegant.

Repost: Einf├╝hrung in Makefiles

October 23, 2014

I just found an old article I wrote (in German) to introduce people to make files:

Einfuehrung_in_Makefiles

Treat specific warnings as error in Visual Studio

March 27, 2014

Some warnings are so severe that they should be treated as errors at compilation, others are not.

There’s the option to treat all warnings as errors in the Visual Studio IDE or at the command line (/WX). In Visual studio 2013, and also in Visual Studio 2005, there’s an option to treat only specific warnings as errors, with the command line option /we.

Example: To treat the warning that a function is being used without a previous declaration into an error, add /we4013 to the compiler’s command line.

To read more on compiler warning options, see the Micrsosoft Page.

Eclipse: Fixing the “Program is not a Recognized Executable” Error

January 2, 2011

This has been taken from a FAQ at http://wiki.wxwidgets.org

When you compile & link a program, the Eclipse run feature will try to recognize the executable, using one of several parsers. If the wrong binary parser (or none at all) is selected for the project, Eclipse will show the above error in the “Run…” dialog when you select the executable file for your project. Follow those steps to add a binary parser:

In the Eclipse main menu

  • select “Project” -> “Properties”
  • navigate to “C/C++ Build” -> “Settings” -> (on the right) “Binary Parsers”

In the “Binary parser:” list, check the “PE Windows Parser” and click “OK” (lower right)

C++ Style Int or Float to String conversion

December 20, 2010

If you want to convert an int or float/double value into a string representation without using snprintf or itoa, here’s the C++ way:

#include <sstream>

template <typename T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}

Of course you can also use the stringstream variable the same way as you would cout.
(found on the net)

Invaluable SVN properties for source files

September 2, 2010

If you add a source file to your repository, think about adding those SVN properties:


svn:eol-style = native
svn:mime-type = text/plain
svn:keywords = Date Revision HeadURL Author

The first line checks a file for consistent line endings before it can be commited.
The last line enables the magic tags like $Date: $ (see previous post).

You can also make TortoiseSVN auto-add properties by setting it up in the [auto-props] section of your SVN config file (%APPDATA%/Subversion/config). Here are some useful lines:


[auto-props]
*.cpp = svn:mime-type=text/plain;svn:eol-style=native
*.hpp = svn:mime-type=text/plain;svn:eol-style=native
*.h = svn:mime-type=text/plain;svn:eol-style=native
*.c = svn:mime-type=text/plain;svn:eol-style=native
*.dbc = svn:mime-type=text/plain;svn:eol-style=CRLF
*.doc = svn:needs-lock=true;svn:mime-type=application/msword

C Tricks: Defining a String Table (Array)

August 2, 2010

Sometimes you need a String Table – an array of predefined character arrays – in C. There is the simple way to do this, by defining a fixed string size and table size:

#define TABLESIZE 3
#define STRINGSIZE 16
char stringTable[TABLESIZE][STRINGSIZE] =
  { "Entry1", "Entry2", "Entry3" };

However, there is a far more elegant way:

char * stringTable[] =
 { "Entry1", "Entry2", "Entry3", "verylongEntryonlytotestifithasanyinfluenceonsizeof"};
printf("String table size: %d \n", sizeof(stringTable)/sizeof(char*) );

This will output “String table size: 4” just as you intended. You do not have to worry about adjusting your defines whenever you make changes to the string table. Also, you do not waste memory because every character array only takes up as much space as needed for the particular string.

p.s.: I hope this actually works in C, I only tested it with a C++ compiler.

SVN Batch Update with TortoiseSVN

June 10, 2010

This one-liner within a .bat file will update all subversion copies in the directory of the .bat file:

for /d %%d in (*. ) do call c:\programme\tortoisesvn\bin\tortoiseproc /command:update /path:%%d /notempfile /closeonend:3

Fixing CANoe remote control via COM

June 4, 2010

If you need to control replay of a CANoe/CANalyzer logfile from an outside application (e.g. a script), you can do so via the COM interface CANoe/CANalyzer provides. When you create an instance of the CANoe “Application” object, Windows COM service retrieves the CANoe executable path etc. from the registry and starts the application. If that information does not work for you, e.g. because you installed several versions of CANoe on your machine and want the older version to handle the COM requests, you can repair the registry entries by typing:


canoe32 -regserver

You can find the changes in the registry key
[HKEY_CLASSES_ROOT\CLSID\{7F31DEB1-5BCC-11d3-8562-00105A3E017B}].

Not all functions listed in the CANoe COM help have to be available in every version. To be sure, you can inspect the exported object interface withe the Visual Studio helper application “oleview.exe”:

  1. Open a Visual Studio 2005 command prompt and enter “oleview”
  2. In the Tree view part, open “Type Libraries” and select CANoe Type Library
  3. Click on the “View” button view button in the tool bar to open the interface/class viewer for CANoe