LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-17-2004, 09:57 PM   #1
BuzzLightyear
LQ Newbie
 
Registered: Aug 2004
Posts: 2

Rep: Reputation: 0
Sharing, importing and exporting finctions


I'm trying to build an app that exposes certain functions for external apps to access. I can't use a shared library as my program needs to be running regardless of whether an external app wants to use it.

Here's an example of what I would like to achieve, but just can't figure out how to go about it in linux

/* MainProg.cpp */

// This is the main app that does stuff, and exposes functions

int main(int argc, char *argv[])
{
usleep(50000);
}

extern "C" void BeUsed(int n)
{
printf("An external app reckons the answer is %d", n);
}


/* ExternalApp.cpp */

// This could be some third party app that hooks into my service and needs to pass information in

extern "C" void BeUsed(int n);

int main(int argc, char *argv[])
{
BeUsed(42);
}


Now here's some questions:

In 'ExternalApp' how do I define 'BeUsed' as being a public symbol?

Also, in addition to that, is there a function that I can use to dynamically import the symbol for the purpose of detecting that the host is ready for use?

What are the requirements in MainProg to expose it's functions as public symbols?

I'm making the assumption (maybe wrongly) that I can't use dlsym to get handles to the functions, because it requires a handle to a SO, which can't be an executable.

Thanks and look forward to any assistance.
 
Old 08-18-2004, 12:45 AM   #2
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
I didn't understand your question completely, I think you want to say that you want your program in running state and other programs may use the functions of your running program also you talk about exporting variables.
I think you have to learn about deamon threads. In linux demon threads are like services in windows 2000 the are running as backgound processes.
Also i think you learn Envoirnment variables to see if your program is available to be used by other programs. Like your program will set some envoirnment variable and others can check these.
 
Old 08-18-2004, 01:22 AM   #3
ADS
LQ Newbie
 
Registered: Aug 2004
Posts: 14

Rep: Reputation: 0
Quote:
Originally posted by cppkid
I didn't understand your question completely, I think you want to say that you want your program in running state and other programs may use the functions of your running program also you talk about exporting variables.
I think you have to learn about deamon threads. In linux demon threads are like services in windows 2000 the are running as backgound processes.
Also i think you learn Envoirnment variables to see if your program is available to be used by other programs. Like your program will set some envoirnment variable and others can check these.
That actually has nothing to do with his question. He's asking how to import a function referenced by an external symbol from a binary.

Code:
// Declare printf with C linkage.
extern "C" int printf( const char *fmt, ... );

//  Cause everything in the header file "cinclude.h"
//   to have C linkage.
extern "C"
{
#include <cinclude.h>
}

//  Declare the two functions ShowChar and GetChar
//   with C linkage.
extern "C"
{
    char ShowChar( char ch );
    char GetChar( void );
}

//  Define the two functions ShowChar and GetChar
//   with C linkage.
extern "C" char ShowChar( char ch )
{
    putchar( ch );
    return ch;
}

extern "C" char GetChar( void )
{
    char ch;

    ch = getchar();
    return ch;
}

// Declare a global variable, errno, with C linkage.
extern "C" int errno;
There's some ways to export functions/variables (although it seems you already knew about these). I'm not sure how to import symbols from a seperate binary into another program. You may want to read the manpage for ld.
 
Old 08-18-2004, 01:25 AM   #4
BuzzLightyear
LQ Newbie
 
Registered: Aug 2004
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by cppkid
I didn't understand your question completely, I think you want to say that you want your program in running state and other programs may use the functions of your running program also you talk about exporting variables.
Exactly right on the first part. And the second part, no, I was referring to functions when I said 'symbols'. I don't want to share variables - well I do, but by passing them through functions.

Quote:

I think you have to learn about deamon threads. In linux demon threads are like services in windows 2000 the are running as backgound processes.
Also i think you learn Envoirnment variables to see if your program is available to be used by other programs. Like your program will set some envoirnment variable and others can check these.
This is a separate issue that I think I have sorted. My biggest problem at this point is getting access into my running program.
 
  


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
Evolution exporting snoopyswiss SUSE / openSUSE 0 07-19-2005 05:22 AM
exporting one NFS folder to another NFS and exporting it.... stellarmarine1 Linux - Networking 3 09-20-2004 06:54 AM
Exporting X Display belorion Linux - General 2 02-25-2004 04:16 AM
Exporting Display podollb Linux - Newbie 8 10-26-2003 09:21 PM
Problem exporting X. nidua18 Linux - Software 0 10-21-2003 10:42 AM

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

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