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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:55:05+00:00 2026-05-17T06:55:05+00:00

I have written a code where in it would take in a executable file

  • 0

I have written a code where in it would take in a executable file and the [lib*.so] library as my arguments and link @ Run-time.

I want to also take in the function in the (*.o) file @ run-time and link it.
But I don’t know how to go about it.

EDIT 1:
The function I’m trying to link is a part of the .o file in the lib*.so library.
So I want to specify the library name and also the function name which is in the same library @ Run-Time.

For eg. If my library contains two functions(i.e *.o files) the linker should compile the function which I want to use @Run-Time.

I have posted the code,please help :

#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>    // use -ldl

typedef float (*DepFn)(short, short);

int main(int argc, char* argv[])
{
    void* lib;
    DepFn df;

    if(argc < 2)
        return printf("USAGE: %s lib-file\n", argv[0]);

    lib = dlopen(argv[1], RTLD_NOW);
    if(lib == NULL)
        return printf("ERROR: Cannot load library\n");

    df = dlsym(lib, "Depreciation");
    if(df)
    {
        short l, i;

        printf("Enter useful-life of asset: ");
        scanf("%hd", &l);

        for(i = 1; i <= l; i++)
        {
            float d = 100 * df(l, i);
            printf("%hd\t%.1f%%\n", i, d);
        }
    }
    else
        printf("ERROR: Invalid library\n");

    dlclose(lib);
}
  • 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-17T06:55:05+00:00Added an answer on May 17, 2026 at 6:55 am

    If you need to take the function name at runtime, you need to pass it in argv[2], and instead of hardcoding function-name in the dlsym use argv[2].

    if(argc < 3)
            return printf("USAGE: %s lib-file function-name\n", argv[0]);
    
        lib = dlopen(argv[1], RTLD_NOW);
        if(lib == NULL)
            return printf("ERROR: Cannot load library\n");
    
        df = dlsym(lib, argv[2]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code written by someone else in which some functions take arguments
Need assistance with the pexpect module I have written a simple code which would
I have written code to perform a function that could take a while to
I have written the following code to perform some simultaneous HTTP posting and file
I have all written code and i want to this code standarise by php
I have written a test program to take in values from a csv file,
I have written code that opens 16 figures at once. Currently, they all open
I have written code in Java to access web cam,and to save image... I
Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {
I have written this code inside a servlet to delete certain records from three

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.