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.
1) Make sure the dynamic lib (.so) is in a directory listed in /etc/ld.so.conf. Either add the directory to this file or copy the .so to a directory that is already listed in /etc/ld.so.conf.
//dynamic.c
#include <stdio.h> /*for printf() */
#include <stdlib.h> /* for exit() */
#include <dlfcn.h>
typedef void (*pf)();
int main()
{
void *lib;
pf greet;
const char * err;
lib=dlopen("./hello.so", RTLD_NOW);
if (!lib)
{
printf("failed to open hello.so: %s \n", dlerror());
exit(1);
}
dlerror(); /*first clear any previous error; redundant
in this case but a useful habit*/
greet= (pf) dlsym(lib, "hello");/*locate hello() */
err=dlerror();/*check for errors and copy error message*/
if (err)
{
printf("failed to locate hello(): %s \n", err);
exit(1);
}
greet(); /*call hello() */
dlclose(lib);
return 0;
}
//hello.c
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
//using namespace STD;
//gcc -shared -fPIC hello.c -o hello.so
void hello()
{
printf("have a nice day.\n");
printf("or a bad day what ever you choose\n");
}
here's what i typed:
$ g++ -o test dynamic.c -ldl
$ gcc -shared -fPIC hello.c -o hello.so
$ ./test
have a nice day.
or a bad day what ever you choose
$ g++ -shared -fPIC hello.c -o hello.so
$ ./test
failed to locate hello(): ./test: undefined symbol: hello
$
yeah, in that sample I have nothing C, but I want to use strings and cout in the shared libraries (and a few other c++ functions), which as far as I know gcc won't allow it.
//dynamic.c
#include <stdio.h> /*for printf() */
#include <stdlib.h> /* for exit() */
#include <dlfcn.h>
typedef void (*pf)();
int main()
{
void *lib;
pf greet;
const char * err;
lib=dlopen("./hello.so", RTLD_NOW);
if (!lib)
{
printf("failed to open hello.so: %s \n", dlerror());
exit(1);
}
dlerror(); /*first clear any previous error; redundant
in this case but a useful habit*/
greet= (pf) dlsym(lib, "hello");/*locate hello() */
err=dlerror();/*check for errors and copy error message*/
if (err)
{
printf("failed to locate hello(): %s \n", err);
exit(1);
}
greet(); /*call hello() */
dlclose(lib);
return 0;
}
//hello.c
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
//using namespace STD;
//gcc -shared -fPIC hello.c -o hello.so
void hello()
{
printf("have a nice day.\n");
printf("or a bad day what ever you choose\n");
}
here's what i typed:
$ g++ -o test dynamic.c -ldl
$ gcc -shared -fPIC hello.c -o hello.so
$ ./test
have a nice day.
or a bad day what ever you choose
$ g++ -shared -fPIC hello.c -o hello.so
$ ./test
failed to locate hello(): ./test: undefined symbol: hello
$
This due to g++ had renamed your function, try with:
extern "C" void hello()
{
....
}
This due to g++ had renamed your function, try with:
extern "C" void hello()
{
....
}
that's exactly what i was trying to say when i said you hadn't written it in c++
c++ compiler encodes extra information about the function into the function name
thanks to aiza for clarifying
No, the extern "C" part just makes the exported symbol use the C naming rules. C++ naming rules mangle the names based on parameters, etc. This is how C++ allows for function overloading. There is nothing about extern "C" that stops you from using C++ code.
Edit: I didn't see before that you also have a string class as a parameter... that MIGHT cause problems trying to export it as extern "C" since it is part of the function prototype. Change that to a char* and it should work, though. Then if you really want a string instead, you can set an instance of the string class to that in the body of the function.
Edit2: Thought I'd clarify a bit more about what your problem is...
When you compile your .so with g++, and your function doesn't have extern "C", the C++ naming rules are used to mangle the name. Then in your executable, you are using dlsym to try and load an UNmangled name for the function, which it won't find because the name was mangled. If you can find out what the resulting mangled name is, you could theoretically pass that as the name to dlsym instead. Since that is kind of an ugly solution, it is generally just better to use extern "C" to prevent the name mangling in the first place... Hope that helps explain things a bit.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.