ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.