LinuxQuestions.org
Help answer threads with 0 replies.
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 02-19-2008, 12:46 PM   #1
EnTe
LQ Newbie
 
Registered: Jan 2006
Location: pamplona (navarra)
Distribution: Kubuntu 7.10 x64
Posts: 23

Rep: Reputation: 0
Exclamation call a function from library


Hello guys!.

Well, my problem is:

I have a function on a self library and I want to call the function when I write the function name with the keyboard.

example:

----------- Function library -----------
void functionA(char c)
{
if(c='a')
printf("Hello World!");
else
printf("Goodbye!");
}

----------- Main program -----------
void main(void)
{
char fname[20];

printf(">> ");
scanf("%s",fname);
????????????????????????
????????????????????????
}

I need to know the code from "??????????" to do this:

Simulating...(On terminal)

>> functionA a (pushing key enter)
Hello World!

>> functionA (pushing key enter)
Goodbye!

Thanks you.

PD: Sorry for my bad english.
 
Old 02-19-2008, 01:05 PM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
I think that ‘‘man dlopen’’ could enlighten you. dlopen opens .so library, dlsym takes result of dlopen and a function name and returns function address.
 
Old 02-19-2008, 01:42 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by raskin View Post
I think that ‘‘man dlopen’’ could enlighten you. dlopen opens .so library, dlsym takes result of dlopen and a function name and returns function address.
A simpler alternative to dlopen is linking to the library and using the function directly (the symbol resolution is performed automatically instead of manually).

So something like this:
Code:
extern void functionA(char c);

int main(void)
{
	char fname[20];

	printf(">> ");
	scanf("%s",fname);
	functionA(fname[0]);

	return 0;
}
And then, during linking, you would specify that your executable be linked with the library (either static or shared).
 
Old 02-22-2008, 05:59 AM   #4
EnTe
LQ Newbie
 
Registered: Jan 2006
Location: pamplona (navarra)
Distribution: Kubuntu 7.10 x64
Posts: 23

Original Poster
Rep: Reputation: 0
thanks guys!.

But I use static libraries...
 
Old 02-22-2008, 06:15 AM   #5
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
osor's advice works in this case, too, but you will also have to manually select function by name.
You will still need
Code:
 extern void functionA(char c);
and when you compile something like
Code:
 gcc -c main.c 
gcc main.o lib.o -o program
will do.
 
  


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
list library function of a shared library .so powah Linux - General 7 10-25-2011 04:47 AM
Call function in another process Gabbiadini Programming 1 10-27-2007 08:17 AM
How to call another function from a function? geminigal Programming 4 04-21-2005 10:41 PM
what is the function library of the basic graphics library in rethat9.0? zerwolve Red Hat 0 04-29-2004 09:18 PM
how to call function?? harpal Programming 3 04-29-2003 05:59 AM

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

All times are GMT -5. The time now is 01: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