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 08-06-2004, 03:15 AM   #1
kris273
LQ Newbie
 
Registered: Mar 2004
Posts: 19

Rep: Reputation: 0
dynamic linking and g++


my problem is when doing dynamic linking and loading, it seems I HAVE to use the gcc command.

My question is:
  • Is this a bug
    some compability issue I don't know about g++/gcc
    is there something different I should be doing.
Quote:
code:
dlload.c

#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");
}


gcc -o dlload dlload.c -ldl
gcc -shared -fPIC -o hello.so hello.c
how ever, this does not seem to work if I use, is there a way to fix this?

g++ -shared -fPIC -o hello.so hello.c (yes it does complie)
run the program and i get "undefined symbol: hello "

Last edited by kris273; 08-06-2004 at 03:48 AM.
 
Old 08-06-2004, 04:16 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quick guess:

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.

2) Then run "ldconfig" (as root).

3) Then try again to run your program.

Last edited by Hko; 08-06-2004 at 04:18 AM.
 
Old 08-06-2004, 05:04 PM   #3
kris273
LQ Newbie
 
Registered: Mar 2004
Posts: 19

Original Poster
Rep: Reputation: 0
here's the other test

Code:
//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
$
 
Old 08-06-2004, 05:53 PM   #4
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
i think one obvious thing is that nothing you have there is in c++
it is all in c.
check this out.
 
Old 08-06-2004, 08:56 PM   #5
kris273
LQ Newbie
 
Registered: Mar 2004
Posts: 19

Original Poster
Rep: Reputation: 0
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.

Last edited by kris273; 08-06-2004 at 08:58 PM.
 
Old 08-06-2004, 10:09 PM   #6
aiza
LQ Newbie
 
Registered: Jul 2004
Posts: 18

Rep: Reputation: 0
Re: here's the other test

Quote:
Originally posted by kris273
Code:
//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()
{
....
}
 
Old 08-07-2004, 12:12 PM   #7
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
Quote:
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
 
Old 08-08-2004, 01:35 AM   #8
kris273
LQ Newbie
 
Registered: Mar 2004
Posts: 19

Original Poster
Rep: Reputation: 0
I didn't write it in c++?

so if i did everything the same way, and instead of using printf I used cout, and it'll compile it differently thus allowing it to work?
 
Old 08-15-2004, 01:48 AM   #9
kris273
LQ Newbie
 
Registered: Mar 2004
Posts: 19

Original Poster
Rep: Reputation: 0
This is the type of function I want.

extern "C" void hello(string input)
{
cout << "hello " << input << endl;
}

how ever the extern C would make it a C only function which makes me unable to use C++ functions. so is there some other way to do it?
 
Old 08-15-2004, 10:17 AM   #10
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.

Last edited by deiussum; 08-15-2004 at 10:25 AM.
 
Old 08-16-2004, 02:16 PM   #11
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Both the .so (library) and your program must be compiled with either C or C++, not both.
 
  


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
Dynamic name resolution for dynamic IP merlin740 Linux - Software 2 10-04-2004 05:56 PM
a simple dynamic linking program kris273 Programming 3 04-18-2004 06:26 AM
Static/dynamic linking ugenn Linux - Software 0 12-15-2002 11:01 PM
C++ linking? ugenn Programming 5 05-14-2002 01:33 AM

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

All times are GMT -5. The time now is 08:24 AM.

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