Thread: Compilation

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Compilation


    Hi,
    I am facing problem in Compilation,I don't know how to compile properly in Linux

    This is my .c file which will create a.out file

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <dlfcn.h>
    int main()
    {
    void* hdll;
    typedef int (*Hello)();
    Hello fnpHello;


    hdll = dlopen("/home/local/Desktop/Simulation/TCP/Sample.so",RTLD_NOW);

    if(hdll != NULL)
    {
    printf("Sample.so is Loaded ya\n");
    fnpHello = dlsym(hdll, "Hello");
    fnpHello();
    }
    else

    printf("TCP DLL is not Loaded ya\n");

    return 1;
    }


    The above program will call the Sample.c


    # include <stdio.h>

    # include <stdlib.h>

    # include <string.h>

    #include <dlfcn.h>



    /* for windows */

    typedef int (*fnSimulateTCP)();

    fnSimulateTCP fnpointer;

    int Hello()

    {

    void *hdll = NULL;

    hdll = dlopen("/home/local/Desktop/Simulation/TCP/Sample1.so",RTLD_NOW);

    if (hdll != NULL)

    {

    printf("Sample 1 is Loaded Successfully\n");

    fnpointer = dlsym(hdll, "fnSimulateTCP");

    fnpointer();

    }

    return 0;



    }


    The above program will call further Sample1.c

    # include <stdio.h>
    # include <stdlib.h>
    # include <string.h>

    /* for windows */

    int fnSimulateTCP()
    {

    printf("hello world\n");
    return 0;

    }





    I am not getting the Correct output.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2007
    Posts
    1,513
    Rep Power
    1423
    First of all, please use code or highlight tags, when posting source code in this forum.

    Furthermore, please post your previous attempts and describe your results. Just posting your source code will typically not yield your intended results. With a "question" like yours, I could reply with a "works flawlessly here" and you would not be any whiser.

    That said, it does work flawlessly here:

    Code:
    user@host ~/path $ gcc -c -fPIC Sample.c -o Sample.o
    user@host ~/path $ gcc -c -fPIC Sample1.c -o Sample1.o
    user@host ~/path $ gcc -shared -Wl,-soname,Sample.so -o Sample.so Sample.o 
    user@host ~/path $ gcc -shared -Wl,-soname,Sample1.so -o Sample1.so Sample1.o 
    user@host ~/path $ gcc -Wall -rdynamic main.c -ldl -o main
    user@host ~/path $ ./main 
    Sample.so is Loaded ya
    Sample 1 is Loaded Successfully
    hello world
    Regards, Jens

IMN logo majestic logo threadwatch logo seochat tools logo