LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   runtime linking - can anybody point me to a hello world example? (http://www.linuxquestions.org/questions/programming-9/runtime-linking-can-anybody-point-me-to-a-hello-world-example-4175421864/)

eantoranz 08-13-2012 10:26 AM

runtime linking - can anybody point me to a hello world example?
 
Hi!

I'm trying to find a simple straight up example of runtime linking in C. I want to be able to use some functions from an so that I don't want to actually link at build time. Is there such an example out there? A hello world kind of example would be very handy.

Thanks in advance.

NevemTeve 08-13-2012 10:37 AM

Shared libs are resolved at program loading. Try these commands:

Code:

ldd MyHelloWorldProgram
readelf -d MyHelloWorldProgram

they should prove that your program already uses shared libs (libc.so for example)

eantoranz 08-13-2012 10:48 AM

dlopen for starters. http://www.yolinux.com/TUTORIALS/Lib...ndDynamic.html

How can I use one struct defined in the so? dlsym apparently can't find it.

eantoranz 08-13-2012 10:50 AM

I think I'll be fine without the structure.

schneidz 08-13-2012 10:52 AM

this simple example should help illustrate:
Code:

[schneidz@hyper bak]$ ll
total 8
-rw-rw-r--. 1 schneidz schneidz 87 Aug 13 11:50 main.c
-rw-rw-r--. 1 schneidz schneidz 66 Aug 13 11:48 schneidz.c
[schneidz@hyper bak]$ cat main.c
#include<stdio.h>

main()
{
    printf(" schneidz of 5 x 5 = %d\n", schneidz(5,5));
}

[schneidz@hyper bak]$ cat schneidz.c
#include <stdio.h>

int schneidz(int x, int y)
{
 return x * y;
}
[schneidz@hyper bak]$ gcc -c schneidz.c
[schneidz@hyper bak]$ ll
total 12
-rw-rw-r--. 1 schneidz schneidz  87 Aug 13 11:50 main.c
-rw-rw-r--. 1 schneidz schneidz  66 Aug 13 11:48 schneidz.c
-rw-rw-r--. 1 schneidz schneidz 1248 Aug 13 11:50 schneidz.o
[schneidz@hyper bak]$ gcc main.c schneidz.o -o schneidz.x
[schneidz@hyper bak]$ ll
total 20
-rw-rw-r--. 1 schneidz schneidz  87 Aug 13 11:50 main.c
-rw-rw-r--. 1 schneidz schneidz  66 Aug 13 11:48 schneidz.c
-rw-rw-r--. 1 schneidz schneidz 1248 Aug 13 11:50 schneidz.o
-rwxrwxr-x. 1 schneidz schneidz 6598 Aug 13 11:51 schneidz.x
[schneidz@hyper bak]$ ./schneidz.x
 schneidz of 5 x 5 = 25


NevemTeve 08-13-2012 10:58 AM

struct, enums, unions, typedef and #defines exists in compile time; in run-time you can use dlsym to access functions and global variables


All times are GMT -5. The time now is 12:59 PM.