Topic: HELP using C library in C++ program


Author: akv3389@ucs.usl.edu (Anil Vijendran)
Date: 13 Nov 1994 07:32:07 GMT
Raw View
>>>>> On 7 Nov 1994 23:33:34 -0500, vickroy@cis.ohio-state.edu (james michael vickroy) said:


    james>     extern TYPE *foo(char *a,int b,int c,int d);

Use this instead:

extern "C"
{
 TYPE* foo(all_your_stuff);
};

The reason is in C++, fn names are encoded. You have to tell the
compiler explicitly not to encode the function name which is what
extern "C" does.
--

Anil
  --------------------------------------
  USL Box 43007, Lafayette LA 70504-3007
        anil@usl.edu
   (318) 234 1142 [Home]
          ---------------------




Author: smorrish@bleakhouse.win-uk.net (Simon D. Morrish)
Date: Mon, 14 Nov 1994 20:36:41 GMT
Raw View
James Michael Vickroy (vickroy@cis.ohio-state.edu) writes:

>I trying to use a library (archive) function presumeably generated by a
>C language compiler with the following prototype:
>
>                 TYPE *foo(char *a,int b,int c,int d);
>
>and is actually prototyped in a header file as:
>
>                 extern TYPE *foo();
>
>When I compile this into my program (using SUN c-front) I get this error:
>
>                 "error: unexpected  1 argument for foo()"
>
>I also tried prototyping the function myself like this:
>
>                 extern TYPE *foo(char *a,int b,int c,int d);
>
>and get an unresolved external.
>

In C++, the function prototype 'TYPE *foo()' means a function taking no
parameters, explaining the first error. If you wish to declare a function taking
an unspecified number and type of parameters, you use the declaration 'TYPE
*foo(...)'. This is usually regarded as bad practice unless absolutely necessary.

The second error is, I believe, because C++ 'name mangles' functions when it
compiles them. This means that information about the type of parameters the
function takes is included in the mangled name.

Thus, your source is expecting to link to a function called something like
'_foo$qcpqiqiqi', whereas the C library is presenting it a function called
'_foo'. This, of course, causes an unresolved external.

A solution is to tell your C++ compiler that foo is a C function by declaring it
as:

        extern "C" TYPE *foo(char *, int, int, int);

Hope this works!




...Simon





Author: andersb@lnis88.is.morgan.com (Bruce Anderson)
Date: Fri, 11 Nov 1994 13:11:39 GMT
Raw View
In article <39mv2uINN6do@turtle.cis.ohio-state.edu> vickroy@cis.ohio-state.edu (james michael vickroy) writes:

>   Xref: is1.is.morgan.com comp.lang.c++:93077 comp.std.c++:10765
>   Path: is1.is.morgan.com!s5!uunet!usc!math.ohio-state.edu!magnus.acs.ohio-state.edu!cis.ohio-state.edu!bounce-bounce
>   From: vickroy@cis.ohio-state.edu (james michael vickroy)
>   Newsgroups: comp.lang.c++,cis.lang.c++,comp.std.c++
>   Date: 7 Nov 1994 23:33:34 -0500
>   Organization: The Ohio State University Dept. of Computer and Info. Science
>   Lines: 33
>   Distribution: world
>   NNTP-Posting-Host: turtle.cis.ohio-state.edu
>
>   I trying to use a library (archive) function presumeably generated by a
>   C language compiler with the following prototype:
>
>       TYPE *foo(char *a,int b,int c,int d);
>
>   and is actually prototyped in a header file as:
>
>       extern TYPE *foo();
>

You need :-
extern "C" TYPE *foo(char *a,int b,int c,int d);

you can group together several with

extern "C" {
your prototypes
}

I believe you can do something similar for Pascal libraries

>   When I compile this into my program (using SUN c-front) I get this error:
>
>    oooo  o   ooo ooo oo        "Baseball is life."
                                  ~~~~~~~~~~~~~~~~~

Then life is not worth living ;-)
--
              www
             (o o)
 +-------ooO--(_)--Ooo-------------+----------------------------+
 | Bruce Anderson                  | Email : andersb@morgan.com |
 | Morgan Stanley                  |                            |
 | 25, Cabot Square, Canary Wharf, | Tel   : +44 (0)71 425 8167 |
 | London. E14 4QA                 | Fax   : +44 (0)71 425 8969 |
 +---------------------------------+----------------------------+
            (_) (_)





Author: baker-j@cis.ohio-state.edu (john t baker)
Date: 7 Nov 1994 23:48:22 -0500
Raw View
You have been a victim of "name-mangling".  I will describe what this is
if you request, but the short answer to your question is to enclose the
declaration thus:

extern "C"
 {
 // place all external C-library function call prototypes here
 }
--
John Baker
"It ain't an easy life being a self-parody."
 - John Baker




Author: vickroy@cis.ohio-state.edu (james michael vickroy)
Date: 7 Nov 1994 23:33:34 -0500
Raw View
I trying to use a library (archive) function presumeably generated by a
C language compiler with the following prototype:

    TYPE *foo(char *a,int b,int c,int d);

and is actually prototyped in a header file as:

    extern TYPE *foo();

When I compile this into my program (using SUN c-front) I get this error:

    "error: unexpected  1 argument for foo()"


I also tried prototyping the function myself like this:

    extern TYPE *foo(char *a,int b,int c,int d);

and get an unresolved external.

I don't have access to the source for the library functions, so I can't
see the original function declaration. Does anyone have any thoughts
on this?

thanks in advance,
jim vickroy
--
o ooo  oo                    Jim Vickroy
o o  o o o    Q              College Of Business
o  oooooooo   o    oo        The Ohio State University
o    o oo  o  o   o  o       vickroy@cis.ohio-state.edu
 oooo  o   ooo ooo oo        "Baseball is life."