public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* shared libraray
@ 2000-11-24  9:22 Kevin Su
  2000-11-24 12:57 ` Alexandre Oliva
  0 siblings, 1 reply; 5+ messages in thread
From: Kevin Su @ 2000-11-24  9:22 UTC (permalink / raw)
  To: gcc-help

Hi,

    I recently tried to create a shared library.  I found that if  I
compile the source with  g++, instead of gcc, symbol of functions in the
shared library can not be found with dlsym(), it returns NULL with an
error from dlerror().  With gcc, everything is working fine.

To test it, I wrote two sample file, xxx.c and test.c,  shown below.
Compiling these two files using gcc like the following will work:

$ gcc -fpic -c xxx.c
$ ld -shared xxx.o -o xxx.so
$ gcc test.c -ldl
$ a.out
xxxxxxxx

Compiling these two files using g++ like the following will NOT work:

$ g++ -fpic -c xxx.c
$ ld -shared xxx.o -o xxx.so
$ g++ test.c -ldl
$ a.out
dlsym:/tmp/xxx.so: undefined symbol: xxx


Any help or hint on this problem is much appreciated.

                Thanks,

                                        Kevin


file "xxx.c" ( the file to be built into a shared library):
====================================================

#include <stdio.h>
void
xxx(void)
{
         fprintf(stderr, "xxxxxxxx\n");
}


file "test.c" (the file that loads the shared library) :
=====================================================

#include <dlfcn.h>
#include <stdio.h>
typedef void (*func)(void);

int main()
{
        void* ptr;
        func f;

        void* handle;

        if((handle = dlopen("/tmp/xxx.so", RTLD_LAZY)) == 0){
            fprintf(stderr, "dlopen:%s\n", dlerror());
            return 0;

        if((ptr = dlsym(handle, "xxx")) == 0){
            fprintf(stderr,"dlsym%s\n", dlerror());
            return 0;
        }

        f = (func)ptr;
        f();
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: shared libraray
  2000-11-24  9:22 shared libraray Kevin Su
@ 2000-11-24 12:57 ` Alexandre Oliva
  2000-12-18 11:19   ` problem compiling gethostbyname_r with g++ Kevin Su
  0 siblings, 1 reply; 5+ messages in thread
From: Alexandre Oliva @ 2000-11-24 12:57 UTC (permalink / raw)
  To: Kevin Su; +Cc: gcc-help

On Nov 24, 2000, Kevin Su <ksu@kinaare.net> wrote:

>     I recently tried to create a shared library.  I found that if  I
> compile the source with  g++, instead of gcc, symbol of functions in the
> shared library can not be found with dlsym(), it returns NULL with an
> error from dlerror().  With gcc, everything is working fine.

Declare functions you want to dlsym() as extern "C", so that their
names don't get mangled.

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me

^ permalink raw reply	[flat|nested] 5+ messages in thread

* problem compiling gethostbyname_r with g++
  2000-11-24 12:57 ` Alexandre Oliva
@ 2000-12-18 11:19   ` Kevin Su
  2000-12-18 11:30     ` Kevin Su
  2000-12-19 18:37     ` Alexandre Oliva
  0 siblings, 2 replies; 5+ messages in thread
From: Kevin Su @ 2000-12-18 11:19 UTC (permalink / raw)
  To: egcs; +Cc: gcc-help

>

Hi,

When I compile the re-entrant version of gethostbyname, gethostbyname_r(),
with gcc, it is successfully compiled and linked.
But if I used g++ to compile it, I got errors from undefined symbol
__h_errno_location().

The simple test program test.c and the compilation output is shown bleow.

Anybody know what might be the problem here?

        Any help or hint is greatly appreciated.


                                              Kevin


test.c
========================================

#include <stdio.h>
#include <netdb.h>

int main(int argc, char** argv)
{
    int h_errno;
    struct hostent *result, result_buf;
    char buf[1024];

    if(gethostbyname_r(argv[1], &result_buf, buf, 1024, &result, &h_errno) <
0){
           return -1;
    }

}

======================================

$ gcc test.c
$ g++ test.c
/tmp/ccBqm5JX.o: In function `main':
/tmp/ccBqm5JX.o(.text+0xa): undefined reference to `__h_errno_location(void)'

collect2: ld returned 1 exit status


^ permalink raw reply	[flat|nested] 5+ messages in thread

* problem compiling gethostbyname_r with g++
  2000-12-18 11:19   ` problem compiling gethostbyname_r with g++ Kevin Su
@ 2000-12-18 11:30     ` Kevin Su
  2000-12-19 18:37     ` Alexandre Oliva
  1 sibling, 0 replies; 5+ messages in thread
From: Kevin Su @ 2000-12-18 11:30 UTC (permalink / raw)
  To: gcc-help

Kevin Su wrote:

> >
>
> Hi,
>
> When I compile the re-entrant version of gethostbyname, gethostbyname_r(),
> with gcc, it is successfully compiled and linked.
> But if I used g++ to compile it, I got errors from undefined symbol
> __h_errno_location().
>
> The simple test program test.c and the compilation output is shown bleow.
>
> Anybody know what might be the problem here?
>
>         Any help or hint is greatly appreciated.
>
>                                               Kevin
>
> test.c
> ========================================
>
> #include <stdio.h>
> #include <netdb.h>
>
> int main(int argc, char** argv)
> {
>     int h_errno;
>     struct hostent *result, result_buf;
>     char buf[1024];
>
>     if(gethostbyname_r(argv[1], &result_buf, buf, 1024, &result, &h_errno) <
> 0){
>            return -1;
>     }
>
> }
>
> ======================================
>
> $ gcc test.c
> $ g++ test.c
> /tmp/ccBqm5JX.o: In function `main':
> /tmp/ccBqm5JX.o(.text+0xa): undefined reference to `__h_errno_location(void)'
>
> collect2: ld returned 1 exit status

Sorry, I forgot to mention the platform on which the problem happened:

Linux on Intel

gcc version (gcc -v):
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/specs

gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)

                                                  Kevin

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: problem compiling gethostbyname_r with g++
  2000-12-18 11:19   ` problem compiling gethostbyname_r with g++ Kevin Su
  2000-12-18 11:30     ` Kevin Su
@ 2000-12-19 18:37     ` Alexandre Oliva
  1 sibling, 0 replies; 5+ messages in thread
From: Alexandre Oliva @ 2000-12-19 18:37 UTC (permalink / raw)
  To: Kevin Su; +Cc: gcc-help

On Dec 18, 2000, Kevin Su <ksu@kinaare.net> wrote:

> /tmp/ccBqm5JX.o: In function `main':
> /tmp/ccBqm5JX.o(.text+0xa): undefined reference to `__h_errno_location(void)'

Looks like this is the result of inappropriate name mangling.  Compile
with -save-temps and look for occurrences of this symbol in the
preprocessed source (*.ii).  Then, complain to the maintainer of the
file that contains that declaration (look for a line starting with `#'
before that declaration, in the preprocessed file) that the
declaration should declare this symbol as extern "C".  And note that
the C library is not part of GCC, so the maintainers of the culprit
header file are not the maintainers of GCC.

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2000-12-19 18:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-11-24  9:22 shared libraray Kevin Su
2000-11-24 12:57 ` Alexandre Oliva
2000-12-18 11:19   ` problem compiling gethostbyname_r with g++ Kevin Su
2000-12-18 11:30     ` Kevin Su
2000-12-19 18:37     ` Alexandre Oliva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).