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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:52:47+00:00 2026-05-27T20:52:47+00:00

I am doing some research into platform independent code and found mention of the

  • 0

I am doing some research into platform independent code and found mention of the dlfcn API. It was the first time I came across mention of it and did further research into it. Now hopefully my lack of experience/understanding of platform independent code as well as compiling/linking isn’t going to show in this post but to me the dlfcn API just lets us do the same dynamic linking programmatically that the ld utility does. If I have misconceptions please correct me as I would like to know. Regarding what I think I know about the ld utility and the dlfcn API I have some questions.

What are the advantages of using either the ld utility vs. dlfcn API to dynamically link?

My first thought was that the dlfcn API seems like a waste of my time since I need to request pointers to the functions vs. having ld examine a symbol table for undefined symbols and then linking them. Similarly ld does everything for me while I have to do everything by hand with the dlfcn API (i.e. open/load the library, get a function pointer, close the library, etc.). But on second glance I thought that there may be some advantages. One being that we can load a library out of memory after we are done using it.

In this way memory could be saved if we knew we didn’t need to utilize a library the whole time. I am unsure if there is any “memory/library” management for libraries dynamically linked by ld? Similarly I am unsure of what scenarios/environments would we be interested in using the dlfcn API to save said memory as it seems this wouldn’t be a problem in modern day systems. I presume one would be the usage of the library on a system with very very very limited resources (maybe some embedded system?).

What other advantages or disadvantages may there be?

What “coding pattern” is used for platform independent code in regards to dynamic linking?

If I was making platform independent code that depended on system calls I could see myself achieving platform independent code by coding in one of three styles:

  1. Logical branching directly in my libraries code via macros. Something like:
  2. void myAwesomeFunction()
    {
         ...
    #if defined(_MSC_VER)
         // Call some Windows system call
    #elif defined(__GNUC__)
         // Call some Unix system call
         ...
    }
  3. Create generic system call functions and use those in my libraries code. Something like:
  4. OS_Calls.h
    
    void OS_openFile(string myFile)
    {
         ...
    #if defined(_MSC_VER)
         // Call Windows system call to open file
    #elif defined(__GNUC__)
         // Call Unix system call to open file
         ...
    }
    
    MyAwesomeFunctions.cpp
    
    #include "OS_Calls.h"
    
    void myAwesomeFunction()
    {
         ...
         OS_openFile("my awesome file");
         ...
    }
  5. Similar to one but add a layer of abstraction by using the dlfcn API
  6. MyLibraryLoader.h
    
    void* GetLibraryFunction(void* lib, char* funcName)
    {
        ...
        return dlsym(lib, funcName);
    }
    
    MyAwesomeFunctions.cpp
    
    #include "MyLibraryLoader.h"
    
    void myAwesomeFunction()
    {
        Result result = GetLibraryFunction(someLib, someFunc)(arguments...);
    }

What ones are typically used and why? And if there are any others that aren’t listed and preferred to mine please let me know.

Thanks for reading this post. I will keep it updated so that it may serve as a future informative reference.

  • 1 1 Answer
  • 1 View
  • 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-27T20:52:47+00:00Added an answer on May 27, 2026 at 8:52 pm

    dlfcn and ld does not solve the same problem: in fact you can use both in your project.

    The dlfcn API is meant to support plugin architectures, in which you define an interface which modules should implement. An application can then load different implementations of that interface, for various reasons (extensibility, customization, etc.).

    ld, well, links the libraries your application request, but does that at compile time, not at runtime time. It doesn’t support in any way plugin architectures, since ld links objects specified in the command line.

    Of course you can only use the dlfcn API, but it is not meant to be used in that way and, of course, using it in that way would be a huge pain in your rectum.

    For your second question, I think the best pattern is the second one.
    Branching “directly in the code” can be confusing, because it’s not immediately obvious what the two branches accomplish, something which is well-defined if you define a proper abstraction and you implement it using multiple branches for each supported architecture.
    Using the dlfcn API is pretty pointless, because you don’t have a uniform interface to call (that’s exactly the argument that supports the second pattern), so it just adds bloats in your code.

    HTH

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

Sidebar

Related Questions

I was doing some research into general topics of software engineering and I came
I am doing some research into methods of comparing time series data. One of
I'm doing some research into using log4net, and I found the IObjectRenderer interface interesting.
I've just been doing some research into sql data services, and found out that
I am doing some research into geolocation in web pages. I have set up
I’m doing some research into the capabilities of Windows Azure for an European company.
So I have been doing some research into getting my UTF8 to print correctly.
I'm doing some personal research into text analysis, and have come up with close
I am just doing some research into image processing and would appreciate it if
I am busy doing some research into using REST services with mobile applications and

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.