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 6191933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:51:13+00:00 2026-05-24T02:51:13+00:00

My situation is the following: I am writing a toolbox that generates two libraries.

  • 0

My situation is the following:

I am writing a toolbox that generates two libraries. The first (A) has all the functions and data-types and can be used in a pure C++ application; the second (B) is an interface to MATLAB.
A pure C++ program would be compiled with

g++ $(FLAGS) C.cpp $(MOREFLAGS) -lA

while a MATLAB program would be compiled with B linked after A, i.e.

mex $(FLAGS) C.cpp $(MOREFLAGS) -lA -lB

Now, I would like use a function pointer (myexit) to call std::exit() for pure C++ applications and mex_exit() (which calls mexErrMsgTxt()) for MATLAB applications. Following a tutorial on function pointers, I’ve written something like the snippets below (everything is actually within a namespace, but I have suppressed this for brevity).

//A.hpp
#ifndef __A
#define __A
extern void (*myexit)(int);
...
#ifdef mex_h /* defined in mex.h */
#include "B.hpp"
#endif /* mex_h */
#endif /* __A */

//A.cpp
#include "A.hpp"
void (*myexit)(int) = &std::exit;

//B.hpp
#ifndef __B
#define __B
#include <mex.h>
#include "A.hpp"
...
void mex_exit(int);
#endif /* __B */

//B.cpp
#include <mex.h>
#include "A.hpp"
void mex_exit(int err) {mexPrintf("Error code %d\n",err); mexErrMsgTxt("...");}
//void (*myexit)(int) = &mex_exit; // <- I want a line like this to override the line in A.cpp

What I have above seems to work for pure C++ code and I find that I can get the right behaviour for MATLAB code if I include the line myexit = &mex_exit; in C.cpp, however, I want this behaviour simply from having #include <mex.h> and #include "A.hpp" in C.cpp and linking to B (i.e. it shouldn’t be the user’s responsibility to include this line).

Is this possible? If so, then how?

  • 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-24T02:51:16+00:00Added an answer on May 24, 2026 at 2:51 am

    In library B, add a static constructor to set myexit:

    // Consider putting this into a namespace
    extern void (*myexit)(int);
    
    // anonymous namespace to avoid name collisions
    namespace {
    class StaticInit {
      StaticInit() {
        myexit = mex_exit;
      }
    } static_init_obj;
    }
    

    Then link library B to depend on library A:

    gcc -o libB.so -shared -lA -lmatlab b.o
    

    Because libB depends on libA, libA static constructors will be invoked before libB static constructors, so ordering isn’t a problem. The StaticInit constructor will be invoked during program startup (before main()), and set myexit for you.

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

Sidebar

Related Questions

I am in the following situation: I am writing code for a kernel that
The application I'm writing has a need for a solution to the following situation:
Here's the situation: I'm writing a simple AJAX application that performs CRUD functions. When
Given the following situation (UML below), If Y has the method: public void PrintWs();
How should I handle the following situation : I am writing my own 2D
I have the following situation: There is a tool that gets an XSLT from
I'm writing my first script that will connect via SSL to a MySQL database.
I'm writing a custom structure manipulation program, and I've got the following types: type
Consider the situation. I am writing a statistical analysis app. The app has multiple
When I am writing my code, I find the following situation many times: def

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.