You are not logged in.

#1 2009-07-21 08:36:50

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

C++ - advise on making code modular (mix-and-match)

Hi all,

I've been using C++ for some research-related programming, basically computer vision algorithms. My algorithms are modular in that they can be mixed and matched, for example in a 4-stage algorithm, stage A could be skipped entirely, or done using method 1, or method 2, or both sequentially. Currently, I take in command-line options indicating which methods should be used at which stage, with some simple string parsing and 'if' structures to check whether the options match a known method (ie. already coded into my program).

I'd like to make the design more modular, in such a way that external contributors are able to write their own algorithms with a certain API (nothing fancy, just a function which accepts the input images and an options structure and another function which returns the result), compile it to some sort of library, and my code should be able to use the external function without recompiling.

I'm thinking shared libraries would be able to achieve this, but I'm not sure how within C++ to 'select' a library to load based on name-matching from the command-line. My C++ formal education is pretty much non-existant, sort of a 'learn-as-you-go', so I know much more about image manipulation and efficiency concerns than procedural things such as this.

I know I'm not being too clear here, but am not sure how to improve on that. If anyone can help, please point me in the right direction. Thanks.


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#2 2009-07-21 12:10:12

gnud
Member
Registered: 2005-11-27
Posts: 182

Re: C++ - advise on making code modular (mix-and-match)

For starters, have a look at this article : http://www.faqs.org/docs/Linux-mini/C++-dlopen.html

Offline

#3 2009-07-21 23:21:01

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

Re: C++ - advise on making code modular (mix-and-match)

Thank you, gnud, I now know a new term, dynamic loading. Reading up on it (via google et. al.) it seems reasonably kludgy, which seems unavoidable given that it refers back to C instead of C++. However, for my purposes the big issue is that it is platform specific (dlopen for UNIX base and some windows-specific stuff for Windows), which would be quite an issue for getting contributions from external sources.

I'm wondering whether a simple wrapper class (called, for want of a better name, loadgenericlib) could be used with #if statements to automatically open/close using dlopen/dlclose on linux systems and the windows equivalent on windows systems. I already do something of the sort with my main.cpp, which can be compiled as a Matlab (linux) shared lib or as a general executable, depending on some #define value. This would allow me to minimize the platform-specific code (and associated bugs) to just one class, and all other programming can be done in a platform-agnostic manner, presumably.

Thoughts?


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#4 2009-07-22 07:35:59

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: C++ - advise on making code modular (mix-and-match)

ngoonee wrote:

I'm wondering whether a simple wrapper class (called, for want of a better name, loadgenericlib) could be used with #if statements to automatically open/close using dlopen/dlclose on linux systems and the windows equivalent on windows systems. I already do something of the sort with my main.cpp, which can be compiled as a Matlab (linux) shared lib or as a general executable, depending on some #define value. This would allow me to minimize the platform-specific code (and associated bugs) to just one class, and all other programming can be done in a platform-agnostic manner, presumably.

I think that's the usual way to go about it. Just wrap it around your method definitions in the implementation file.

Offline

#5 2009-07-22 09:46:31

kakTuZ
Member
From: Hannover, Germany
Registered: 2007-10-20
Posts: 86

Re: C++ - advise on making code modular (mix-and-match)

Informative article about library programming: http://tldp.org/HOWTO/Program-Library-HOWTO/index.html.
It recommends using the "Dynamic Loading of Modules" part of the glib library for abstracting from the underlying operation system.

Offline

#6 2009-07-22 12:20:10

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: C++ - advise on making code modular (mix-and-match)

If you are willing to pull in Qt as a dependency, you can use: http://doc.trolltech.com/4.5.1/plugins-howto.html which is a cross platform abstracted way to load libraries at run time.  You would only need to use QtCore I believe, so you don't need to worry about GUI interaction.

Edit: Also, you could probably use QtMod or wait for Qt4.3 to leave testing so you could use one of the split packages to further reduce build machine packages.

Last edited by scio (2009-07-22 12:21:43)

Offline

#7 2009-07-22 15:12:45

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

Re: C++ - advise on making code modular (mix-and-match)

Thanks for all your input. Concerning QT, I don't really want to introduce it as a dependency since that may be a sticking point to other researchers in the event they wish to contribute. A simple wrapper class downloadable from my website would be preferable to having to setup Qt on their machines, as it is OpenCV is already a dependency, but this is more defensible because my algorithms are image-related...


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#8 2009-07-22 15:57:59

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: C++ - advise on making code modular (mix-and-match)

Well, my point with QtMod or the new split Qt package would be that you could add the code into a sub directory of your project and compile it into a static library.  This way, it will not rely on them having Qt setup, but would allow you to use the functionality.  This would obviously significantly increase the size of the project but would allow you to ensure that they have the correct files.

However, I understand the Qt issue.  The only other solution I could give you would be to write platform-specific code to load the libraries, and then use something like CMake to only compile the correct files for the others.

Offline

#9 2009-07-22 22:30:30

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

Re: C++ - advise on making code modular (mix-and-match)

Ah thanks, did not think of doing that. The size is quite prohibitive though, for now, if just a simple #ifdef works as its supposed to.


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

Board footer

Powered by FluxBB