LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-13-2012, 10:26 AM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Rep: Reputation: 83
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.
 
Old 08-13-2012, 10:37 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,599

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
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)
 
Old 08-13-2012, 10:48 AM   #3
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Original Poster
Rep: Reputation: 83
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.
 
Old 08-13-2012, 10:50 AM   #4
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Original Poster
Rep: Reputation: 83
I think I'll be fine without the structure.
 
Old 08-13-2012, 10:52 AM   #5
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,839

Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
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
 
Old 08-13-2012, 10:58 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,599

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


Reply

Tags
dynamic linking, gcc, linux, shared object


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Compiling code - linking to libraries using different versions of c++ runtime? Dweeb2010 Linux - Software 2 05-21-2011 11:38 AM
[SOLVED] SDL + Eclipse + CDT hello world compiling but not linking with g++ on arch linux fedoraDrew Programming 3 01-22-2011 06:25 PM
Linking (ld) syntax and Floating Point crosscompile question Caesurus Linux - Software 0 06-08-2009 08:36 AM
Problem in using both load time linking and runtime linking durgaprasad_j Linux - General 0 08-01-2006 03:49 AM
Hello World linking Errors shotokan Programming 4 03-24-2006 04:16 AM


All times are GMT -5. The time now is 07:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration