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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:26:14+00:00 2026-05-25T23:26:14+00:00

I need to write a plugin system which works with statically linked modules on

  • 0

I need to write a plugin system which works with statically linked modules on Linux.
I do not want the core (main function) to explicitly call the init function for the module.

The closest analogy which I can think of, for what I want to accomplish, is the Linux kernel.
There it is possible to have a unknown number of modules/plugins compiled and linked statically, but the modules are initiated as would be if they were loaded dynamically.

I have this

core/main.c:

 int main(void) { return 0; }

core/pluginregistrar.h:

#ifndef PLUGIN_REGISTRAR_H
#define PLUGIN_REGISTRAR_H

#include <stdio.h>

#define module_init(pInitor) \
    static inline funInitor __inittest(void) \
        { fprintf(stdout, "test\n"); return pInitor; }
    int init_module(void) __attribute__((alias(#pInitor)));

typedef void (*funInitor)(void);

#endif

adap1/main.c:

#include "pluginregistrar.h"

void adap1Init(void) { fprintf(stdout, "test1\n"); }

module_init(adap1Init);

adap2/main.c:

#include "pluginregistrar.h"

void adap2Init(void) { fprintf(stdout, "test2\n"); }

module_init(adap2Init);

so far, but I have no idea on how to get the core to actually initiate the modules which have done a module_init.

Can anyone here give me a pointer? (no pun intended)

EDIT:
I changed core/main.c to

extern int init_module(void);
int main(void) {
  init_module();
  return 0;
}

and it not shows the call of the “adaptation” which was first in the library list given to the linker.

  • 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-25T23:26:15+00:00Added an answer on May 25, 2026 at 11:26 pm

    If you’re using GCC, you can use the __attribute__((constructor)) specifier to have one piece of code run at startup (before main) for each of your "modules". (Also works with clang and ICC.)

    For example:

    $ cat t.c
    #include <stdio.h>
    #ifdef AAA
    static void  __attribute__((constructor)) myinit()
    {
        printf("%s\n", AAA);
    }
    #else
    int main()
    {
        printf("bye\n");
        return 0;
    }
    #endif
    $ gcc -DAAA=\"hello\" -o  m.o -c t.c 
    $ gcc -DAAA=\"there\" -o  n.o -c t.c 
    $ gcc -o t.o -c t.c
    $ gcc -o foo m.o n.o t.o
    $ ./foo
    there
    hello
    bye
    

    (Code provided for illustration purposes only.)

    Once you have that, you’re pretty much good to go. Have that "constructor" function do whatever the module needs to do to initialize itself, and "register" into your plugin framework. (A structure with a bunch of function pointers, added to a linked list or something like that would work.)

    Note that link order will determine your plugin initialization order, and that’s a can of worms – if your modules depend on each other, things get really tricky. Make sure you have as few globals as possible.

    Update:

    If you need to use static libraries rather than plain .o files, you need a bit of extra linker magic.

    Assuming the above has already run:

    $ ar cru libm.a m.o
    $ ar cru libn.a n.o
    $ gcc -o foo t.c -Wl,-whole-archive libn.a libm.a -Wl,-no-whole-archive
    $ ./foo
    hello
    there
    bye
    

    I’m not entirely certain of whether you can rely on (reverse) link order in this case.

    Or:

    $ ar cru libmn.a m.o n.o
    $ gcc -o foo t.c -Wl,-whole-archive libmn.a -Wl,-no-whole-archive
    $ ./foo 
    there
    hello
    bye
    

    (And here I have no idea of what contructor order you’ll get.)

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

Sidebar

Related Questions

I need to write a plugin for an application which is extensible using Python
I need to write a plugin for Dynamics CRM 4.0 that executes when a
I need to write, in JavaScript (I am using jQuery), a function that is
I need to write a little ruby function that does word wrapping. I have
I am trying to write a plugin which will update a user field. I
I need to write a page which allows a user to select price packages,
My plugin needs to be able to write to the file system. Is it
Is there any function in wordpress for fileupload? I need this for the plugin
I learning how to write a WordPress plugin. I need some help writing some
I need to write a small console app (patch) that turns off the print

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.