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

  • SEARCH
  • Home
  • 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 743889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:54:55+00:00 2026-05-14T08:54:55+00:00

Recently I found a C library that I want to use in my C++

  • 0

Recently I found a C library that I want to use in my C++ project.
This code is configured with global variables and writes it’s output to memory pointed by static pointers.
When I execute my project I would like 2 instances of the C program to run: one with configuration A and one with configuration B. I can’t afford to run my program twice, so I think there are 2 options:

  • Make a C++ wrapper: The problem here is that the wrapper-class should contain all global/static variables the C library has. Since the functions in the C library use those variables I will have to create very big argument-lists for those functions.
  • Copy-paste the C library: Here I’ll have to adapt the name of every function and every variable inside the C library.

Which one is the fastest solution?
Are there other possibilities to run 2 instances of the same C source?

Thanks,

Max

  • 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-14T08:54:55+00:00Added an answer on May 14, 2026 at 8:54 am

    C++ -Wrapper
    You get away easier by pasting “the entire library” – only slightly modfied – into a class.

    // C
    static char resultBuffer[42];
    void ToResult(int x) { ... }
    char const * GetResult() { return resultBuffer; }
    

    becomes

    // C++
    class CMyImportantCLib
    {
      private:
        char resultBuffer[42];
        void ToResult(int x) { ... } // likely, no code changes at all
        char const * GetResult() { return resultBuffer; }
    } ;
    

    There are mostly declarative changes (such as “killing” static and extern declarations). You would need to hunt down static variables inside the methods, though, and turn them into members as well

    Separate Namespaces
    That is an ugly solution, but might be enough for you:

    // impMyLib.h
    namespace A 
    {
      #include "c-lib.h"
    }
    namespace B
    {
      #include "c-lib.h"
    }
    
    // impMyLib.cpp
    namespace A 
    {
      #include "c-lib.c"
    }
    namespace B
    {
      #include "c-lib.c"
    }
    

    If you are lucky, the optimizer/linker succeeds in folding the identical code. However, types in A:: and B:: are unrelated.

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

Sidebar

Related Questions

I recently found myself writing a piece of code that executed a Core Data
I recently found an article online that told me about this: RewriteRule ^mock-up/([^/]+)/([^/]+) /mock-up/index.php?page=$1&section=$2
I recently found the need to learn a new library that didn't come with
I was attempting to use a new library today that I found on the
I recently split some code out into an Android library project. I added references
I recently found that dynamically creating object and methods in Ruby is quite a
I recently found javafx 2.1 very useful for my project of making a video
I recently found that one of my services was taking a large amount of
I recently found a code to send emails using python. It was only for
I know that the library required can be loaded with LIBS += -L/path/to/lib recently

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.