Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 206951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:41:32+00:00 2026-05-11T17:41:32+00:00

i’d like to write a wrapper for a C++ framework. this framework is kinda

  • 0

i’d like to write a wrapper for a C++ framework. this framework is kinda buggy and not really nice and in C++. so i’d like to be able to call their methods from outside (via good old C file) of their framework by using just one shared lib. this sounds like the need for a wrapper that encapsulates the wanted framework methods for usage with C instead of C++.

So far so good…. here is what i already did:

interface aldebaran.h
(this is in my include folder, the ultrasound methods should be called from outside of the framework):

#ifndef _ALDEBARAN_H
#define _ALDEBARAN_H

#ifdef __cplusplus
 extern "C" {
#endif

void subscribe_ultrasound();
void unsubscribe_ultrasound();
float read_ultrasound();

#ifdef __cplusplus
}
#endif

#endif

now the wrapper:

cpp file aldebaran.cpp:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "aldebaran.h"
#include "alproxy.h"
#include "../../include/aldebaran.h"

/*
 * Ultrasound defines
 */
#define ULTRASOUND_RESERVATION_MAGIC  "magic_foobar"

#define ULTRASOUND_POLL_TIME          250
#define ULTRASOUND_READ_ATTEMPTS      50
#define ULTRASOUND_SLEEP_TIME         20

using namespace std;
using namespace AL;

/*
 * Framework proxies
 */
ALPtr<ALProxy> al_tts;
ALPtr<ALProxy> al_led;
ALPtr<ALProxy> al_motion;
ALPtr<ALProxy> al_mem;
ALPtr<ALProxy> al_us;
ALPtr<ALProxy> al_cam;
ALPtr<ALProxy> al_dcm;

/*
 * Constructor
 */
Aldebaran::Aldebaran(ALPtr<ALBroker> pBroker, std::string pName): ALModule(pBroker, pName)
{
    try {
        al_tts    = this->getParentBroker()->getProxy("ALTextToSpeech");
        al_led    = this->getParentBroker()->getProxy("ALLeds");
        al_motion = this->getParentBroker()->getProxy("ALMotion");

        al_mem    = this->getParentBroker()->getProxy("ALMemory");
        al_us     = this->getParentBroker()->getProxy("ALUltraSound");
        al_cam    = this->getParentBroker()->getProxy("NaoCam");
        al_dcm    = this->getParentBroker()->getProxy("DCM");
    }catch(ALError& err){
        std::cout << "XXX: ERROR: " << err.toString() << std::endl;
        return 1;
    }

    printf("XXX: module aldebaran initiated\n");
    fflush(0);
}

/*
 * Destructor
 */
Aldebaran::~Aldebaran()
{
    printf("XXX: module aldebaran destructed\n");
    fflush(0);
}

/*
 * Subscribe to ultrasound module
 */
void subscribe_ultrasound()
{
    ALValue param;

    param.arrayPush(ULTRASOUND_POLL_TIME);
    al_us->callVoid("subscribe", string(ULTRASOUND_RESERVATION_MAGIC), param);

    printf("XXX: ultrasound subscribed: %s\n", ULTRASOUND_RESERVATION_MAGIC);
    fflush(0);
}

/*
 * Unsubscribe to ultrasound module
 */
void unsubscribe_ultrasound()
{
    al_us->callVoid("unsubscribe", string(ULTRASOUND_RESERVATION_MAGIC));

    printf("XXX: ultrasound unsubscribed: %s\n", ULTRASOUND_RESERVATION_MAGIC);
    fflush(0);
}

/*
 * Read from ultrasound module
 */
float read_ultrasound()
{
    int i;
    float val1, val2;
    float val_sum;
    ALValue distance;

    val_sum = .0f;

    for(i = 0; i < ULTRASOUND_READ_ATTEMPTS; ++i){
        SleepMs(ULTRASOUND_SLEEP_TIME);

        distance = al_mem->call<ALValue>("getData", string("extractors/alultrasound/distances"));
        sscanf(distance.toString(AL::VerbosityMini).c_str(),"[%f, %f, \"object\"]", &val1, &val2);

        val_sum += val1;
    }

    return val_sum / (1.f * ULTRASOUND_READ_ATTEMPTS);
}

definition file for aldebaran.cpp:

#ifndef ALDEBARAN_API_H
#define ALDEBARAN_API_H

#include <string>

#include "al_starter.h"
#include "alptr.h"

using namespace AL;

class Aldebaran : public AL::ALModule
{
    public:
        Aldebaran(ALPtr<ALBroker> pBroker, std::string pName);

        virtual ~Aldebaran();

        std::string version(){ return ALTOOLS_VERSION( ALDEBARAN ); };

        bool innerTest(){ return true; };
};

#endif

So this should be a simple example for my wrapper and it compiles fine to libaldebaran.so.

now my test program in C:

… now i’d like to call the interface aldebaran.h methods from a simple c file like this:

#include <stdio.h>

/*
 * Begin your includes here...
 */

#include "../include/aldebaran.h"

/*
 * End your includes here...
 */

#define TEST_OKAY    1
#define TEST_FAILED  0

#define TEST_NAME "test_libaldebaran"

unsigned int count_all = 0;
unsigned int count_ok  = 0;

const char *__test_print(int x)
{
    count_all++;

    if(x == 1){
        count_ok++;

        return "ok";
    }

    return "failed"; 
}

/*
 * Begin tests here...
 */

int test_subscribe_ultrasound()
{
    subscribe_ultrasound();

    return TEST_OKAY;
}

int test_unsubscribe_ultrasound()
{
    unsubscribe_ultrasound();

    return TEST_OKAY;
}

int test_read_ultrasound()
{
    float i;

    i = read_ultrasound();

    return (i > .0f ? TEST_OKAY : TEST_FAILED);
}

/*
 * Execute tests here...
 */

int main(int argc, char **argv)
{
    printf("running test: %s\n\n", TEST_NAME);

    printf("test_subscribe_ultrasound:    \t %s\n", __test_print(test_subscribe_ultrasound()));
    printf("test_read_ultrasound:         \t %s\n", __test_print(test_read_ultrasound()));
    printf("test_unsubscribe_ultrasound:  \t %s\n", __test_print(test_unsubscribe_ultrasound()));

    printf("test finished: %s has %u / %u tests passed\n\n", TEST_NAME, count_ok, count_all);

    return (count_all - count_ok);
}

how can i manage to call these methods? i mean within my C file i have no possibility to create such an object-instance (that generated all the needed ALProxies), have i?

help would be really appreciated… thx


thank you very much so far!!

as xtofl said.. i’d like to keep my interface as simple as possible (without another c++ object preferably):

#ifndef _ALDEBARAN_H
#define _ALDEBARAN_H

#ifdef __cplusplus
 extern "C" {
#endif

void subscribe_ultrasound();
void unsubscribe_ultrasound();
float read_ultrasound();

#ifdef __cplusplus
}
#endif

#endif

the problem hereby is that functions like subscribe_ultrasound() cannot be called without the instanciation of all the proxies… this is our precondition:

...
        al_tts    = this->getParentBroker()->getProxy("ALTextToSpeech");
        al_led    = this->getParentBroker()->getProxy("ALLeds");
        al_motion = this->getParentBroker()->getProxy("ALMotion");

        al_mem    = this->getParentBroker()->getProxy("ALMemory");
        al_us     = this->getParentBroker()->getProxy("ALUltraSound");
        al_cam    = this->getParentBroker()->getProxy("NaoCam");
        al_dcm    = this->getParentBroker()->getProxy("DCM");
...

if i don’t have the code above called, all other will fail.

within their framework it is possible to “autoload” my libaldebaran.so via a python script like this call:

myModule = ALProxy("Aldebaran",  global_params.strRemoteIP, global_params.nRemotePort );

The framework log then says:

May 10 15:02:44 Hunter user.notice root: XXX: module aldebaran initiated
May 10 15:02:46 Hunter user.notice root: INFO: Registering module : 'Aldebaran'
May 10 15:02:46 Hunter user.notice root: ______ End of loading libraries ______

which is totally okay… it called the constructor of my module (so all other needed proxies got instanciated too).

but of course this instance does not belong to my C program…

maybe there is a possibility to share this to all other processes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:41:32+00:00Added an answer on May 11, 2026 at 5:41 pm

    You said yourself to create a C wrapper API around an OO framework.

    This means you don’t need any objects passing the wrapper API (as it appears from the decribed header). It seems all objects needed are created/destructed behind the wrapper API, out of view of your test program.

    The first seems the case. You don’t need objects to test your wrapper API. In the end, all objects are bytes (in memory) that are accessed through a fixed set of functions. It doesn’t matter much whether the functions are written as member-functions (C++) or as plain C functions, as long as they obey the intended semantics of your objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 209k
  • Answers 209k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Do you have a reason to not use a build… May 12, 2026 at 9:49 pm
  • Editorial Team
    Editorial Team added an answer Here's a discussion on the Silverlight Forums of using VS… May 12, 2026 at 9:49 pm
  • Editorial Team
    Editorial Team added an answer The solution for this problem can be found in the… May 12, 2026 at 9:49 pm

Related Questions

I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have text I am displaying in SIlverlight that is coming from a CMS
I am currently running into a problem where an element is coming back from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.