Thread: Calling a function from a library by command line arguments

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    3

    Calling a function from a library by command line arguments

    Hello everyone,

    I want to do the following in C Language:
    - Program takes 2 arguments by command line arguments: first argument is library name, and second is function name from that library. (I know how to do that)
    -The program then includes the library as
    Code:
    #include <library_name>
    and also calls the function which has a function signature as
    Code:
    double function(double argument)
    . What is a way that could do this task? How to get the library and function to work by getting them from argv[]?
    - Read all double values from input and apply the function to all the values. E.g: sin(0). (Can do that if I know how to get the function by using argv[])


    What I have done is that I tried getting the library include tag as a string and convert it to executable code but apparently it is impossible in C to do such thing.
    Any idea will be highly appreciated! Thanks!
    Last edited by Potato571; 05-12-2020 at 05:12 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,298
    Which OS and compiler are you using?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    3
    I am using Ubuntu, running gcc compiler

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,298
    dlopen(3) - Linux man page

    void *dlopen(const char *filename, int flag);
    Open your library

    void *dlsym(void *handle, const char *symbol);
    Look up a symbol, cast it to a suitable function pointer

    int dlclose(void *handle);
    Close when you're done

    Er

    That's it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2020
    Posts
    3
    That's a great idea! But just a silly question, how would I use the function with some values after getting it by *dlsym?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,298
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-05-2020, 12:21 AM
  2. Function to store command line arguments in array
    By Lanuk in forum C Programming
    Replies: 2
    Last Post: 02-02-2015, 12:56 PM
  3. command line arguments
    By tempster09 in forum C Programming
    Replies: 3
    Last Post: 12-06-2009, 06:27 PM
  4. Command line arguments
    By Jasper in forum C Programming
    Replies: 8
    Last Post: 08-07-2009, 10:24 AM
  5. command line arguments
    By belusha in forum C Programming
    Replies: 4
    Last Post: 01-27-2003, 09:35 PM

Tags for this Thread