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 10-22-2010, 12:41 PM   #1
subham
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
dlsym() problem


dlopen() is working correctly but dlsym() says cannot find symbol.

Here are my C (not C++) codes:
------
f1.c
------
#include<dlfcn.h>

int main()
{
char *str="name";
void *handle=dlopen("libsharedtest.so",RTLD_LAZY);
void (*p)(char *);
p = (void (*)(char *))dlsym(handle,"printer");
char* lError = dlerror();
if (lError)
{

printf("Error: %s\n", lError);
}
(*p)(str);
dlclose(handle);
return 0;
}



-----
f2.c
-----

#include<stdio.h>

void printer(char *str)
{
printf("%s",str);
}
 
Old 10-22-2010, 05:59 PM   #2
codewad
LQ Newbie
 
Registered: Oct 2010
Posts: 1

Rep: Reputation: 0
If you're using gcc, there's a good chance that it's running your symbols through it's C++ name mangling stuff, even though you're coding in C.

The way I know of to fix this is to use extern "C" in your dynamic lib code, something like this:

Code:
#include<stdio.h>

extern "C" {

  void printer(char *str)
  {
    printf("%s",str);
  }

}
This will tell the compiler not to mangle the symbol names. There are also a couple other ways to use extern "C", I think. This is just the only way I remember off the top of my head.
 
  


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
undefined reference to `dlsym' darleys Programming 9 08-13-2008 12:25 AM
undefined reference to `dlsym' darleys Linux - Server 1 08-01-2008 08:40 AM
using dlsym() go939 Linux - Software 2 11-05-2007 11:20 PM
undefined reference to `dlsym' bahadur Programming 10 05-12-2007 04:23 PM
dlsym returning NULL tim_l Programming 2 02-02-2004 08:27 AM

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

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