LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 04-17-2004, 04:16 AM   #1
kris273
LQ Newbie
 
Registered: Mar 2004
Posts: 19

Rep: Reputation: 0
a simple dynamic linking program


currently I'm trying to get a Dynamic link and load progam working

I have 2 programs, one given by the man pages of dlopen, which works fine

The other I try to get something more usable but I keep getting an "invalid elf header. I'm not really sure what that is, or why I'm getting it.

i'm trying to just make it call a "printf("hello world");" type function that's stored in a seperate file, the instructions i have talk about compiling it with gcc -fPIC -g....and some others i'm not to familar with, the end product would be hello.so....

anyone have a good idea on this? or a good source for reading up on where to find out more about this?

Kris
 
Old 04-17-2004, 06:41 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
with seeing the code i cant do much but heres a simple example

dlload.c
Code:
#include <dlfcn.h>

int main(int argc, char **argv)
{
        void *lib;
        void (*fn)();

        if(argc != 2) return -1;

        lib=dlopen(argv[1], RTLD_LAZY);
        fn=dlsym(lib, "hello");

        fn();

        dlclose(lib);
        return 0;
}
hello.c
Code:
#include <stdio.h>

void hello()
{
        printf("Hello\n");
}
[kev82@tifa:kev82](1050)$ gcc -o dlload dlload.c -ldl
[kev82@tifa:kev82](1051)$ gcc -shared -fPIC -o hello.so hello.c
[kev82@tifa:kev82](1052)$ ./dlload ./hello.so
Hello
[kev82@tifa:kev82](1053)$
 
Old 04-17-2004, 12:18 PM   #3
kris273
LQ Newbie
 
Registered: Mar 2004
Posts: 19

Original Poster
Rep: Reputation: 0
thank you, using

the compile lines you gave me seemed to work

I just had one final question, any good resourced for learning more about dynamic linking and loading?

Kris

Last edited by kris273; 04-17-2004 at 12:22 PM.
 
Old 04-18-2004, 06:26 AM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
http://www.linuxquestions.org/questi...hreadid=169427

the article is specifically about writing plugins but contains some basic information, theres also a few good links in my public bookmarks that cover this
 
  


Reply



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
dynamic linking joshknape Linux - Software 1 09-19-2005 09:43 PM
simple bash scripts and linking to them kylenewton Programming 4 07-21-2005 10:15 PM
Simple Linking Question inescapeableus Linux - Software 5 10-12-2004 12:07 AM
dynamic linking and g++ kris273 Programming 10 08-16-2004 02:16 PM
Static/dynamic linking ugenn Linux - Software 0 12-15-2002 11:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:27 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration