Find the answer to your Linux question:
Results 1 to 6 of 6
Hi all, I have created a library file suppose lib.o I want to create a function in this library, so that any program compiled with this library will print the ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Posts
    2

    How to get a process's executable name during execution

    Hi all,

    I have created a library file suppose lib.o
    I want to create a function in this library, so that any program compiled with this library will print the name of the executable.

    e.g.
    myprog.c

    int main(){

    function_from_liberary();
    }

    and liberary file
    lib.c contains

    void function_from_liberary() {

    printf ("\nExecutable name is %s",what_to_write_here);
    }


    i compile using

    gcc -c lib.c

    gcc -g -o myprog myprog.c lib.o

    Please help

    Thanx and regards

  2. #2
    Just Joined!
    Join Date
    Jan 2006
    Location
    India
    Posts
    52
    void function_from_liberary() {

    printf ("\nExecutable name is %s",argv[0]);
    }

  3. #3
    Linux User
    Join Date
    Oct 2004
    Location
    /dev/random
    Posts
    404
    Quote Originally Posted by rajeshk View Post
    void function_from_liberary() {

    printf ("\nExecutable name is %s",argv[0]);
    }
    No, this is not possible - argv is local to main() only - if main() is declared as
    Code:
    int main(int argc, char** argv)
    - which is the de-facto standard.

    However, you can make function_from_library() take a const char* - which would be passed from main() as argv[0].
    The Unforgiven
    Registered Linux User #358564

  4. #4
    Linux User
    Join Date
    Oct 2004
    Location
    /dev/random
    Posts
    404
    Quote Originally Posted by the_unforgiven View Post
    No, this is not possible - argv is local to main() only - if main() is declared as
    Code:
    int main(int argc, char** argv)
    - which is the de-facto standard.

    However, you can make function_from_library() take a const char* - which would be passed from main() as argv[0].
    Or, better still, you could use the file /proc/<PID>/cmdline - pid can be got with getpid().
    The Unforgiven
    Registered Linux User #358564

  5. #5
    Just Joined!
    Join Date
    Jun 2007
    Posts
    2

    error: dereferencing pointer to incomplete type

    Hi all,
    I have changed my program as follows. but it gives error:
    line 7: error: dereferencing pointer to incomplete type

    1. #include<stdio.h>
    2. #include<sys/stat.h>
    3. #include<stdlib.h>
    4. void execname() {
    5. struct task_struct *my;
    6. my = find_task_by_id(getpid());
    7. printf("&#37;s",my->comm); error: dereferencing pointer to incomplete type
    8.
    9. }
    10. int main()
    11. {
    12. execname();
    13. }


    what's wrong with it

  6. #6
    Just Joined!
    Join Date
    Jun 2008
    Posts
    3

    __progname

    __progname (and __progname_full) are possible ways to access the executable's name at runtime without having to store off values from argv**. I don't believe it's entirely standard, but it's available and is what glibc's assert uses.

    extern const char *__progname;

    void print_progname() {
    printf("Name: %s\n", __progname);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...