help : C and C++ link together

help : C and C++ link together

Post by Chia-shyan Ch » Tue, 09 Sep 1997 04:00:00



sorry !!
the error is "undefined reference to sub"

 
 
 

help : C and C++ link together

Post by Chia-shyan Ch » Tue, 09 Sep 1997 04:00:00


hello,
    I got a problem when I try to compile my program.
    For example, I have two sub-program named pro1.c and pro2.cpp, the pro1
is :

extern sub(int, int);
main()
{
  sub(2,3);

Quote:}

the pro2.cpp is
int sub(int a,int b)
{
  return a-b;

Quote:}

when I complie with "cc -o proc pro1.c pro2.cpp",
an unreferenced error will occurs and it can not find the sub function.

How can I solve it ?

thankx ....

 
 
 

help : C and C++ link together

Post by Frank Mil » Tue, 09 Sep 1997 04:00:00




>hello,
>    I got a problem when I try to compile my program.
>    For example, I have two sub-program named pro1.c and pro2.cpp, the pro1
>is :

>extern sub(int, int);
>main()
>{
>  sub(2,3);
>}

>the pro2.cpp is
>int sub(int a,int b)
>{
>  return a-b;
>}

>when I complie with "cc -o proc pro1.c pro2.cpp",
>an unreferenced error will occurs and it can not find the sub function.

>How can I solve it ?

The usual way to solve this common problem is to have a header file
with a function prototype something like this:

#ifdef __cplusplus
        extern "C" {
#endif
        /* insert normal C prototypes here: */
        int sub(int var1, int var2);
#ifdef __cplusplus
        }
#endif

This works with Borland, Watcom, and GCC, or should if I got the right
underscores for the '__cplusplus'.  Just #include into both
the *.c and *.cpp source and the link should resolve correctly.

        -frank

 
 
 

help : C and C++ link together

Post by Greg Come » Tue, 09 Sep 1997 04:00:00





>>hello,
>>    I got a problem when I try to compile my program.
>>    For example, I have two sub-program named pro1.c and pro2.cpp, the pro1
>>is :

>>extern sub(int, int);
>>main()
>>{
>>  sub(2,3);
>>}

>>the pro2.cpp is
>>int sub(int a,int b)
>>{
>>  return a-b;
>>}

>>when I complie with "cc -o proc pro1.c pro2.cpp",
>>an unreferenced error will occurs and it can not find the sub function.

>>How can I solve it ?

>The usual way to solve this common problem is to have a header file
>with a function prototype something like this:

>#ifdef __cplusplus
>    extern "C" {
>#endif
>    /* insert normal C prototypes here: */
>    int sub(int var1, int var2);
>#ifdef __cplusplus
>    }
>#endif

>This works with Borland, Watcom, and GCC, or should if I got the right
>underscores for the '__cplusplus'.  Just #include into both
>the *.c and *.cpp source and the link should resolve correctly.

Yup, and too, using header files for function prototypes
is always a good idea.

Even if this were not an issue, and for
those not clear one the the issue, what this is: apprently the
original poster's C compiler, ran as C++ because the source file
was named with .cpp, and C++'s normally encode function names with
type information, hence the sub in proc was generated in the
object code perhaps as _sub whereas the sub in proc2 was ende dup
being named something such as sub__Fii, and hence the call the sub
in main was not found.

This brings up another issue BTW: for mixed C and C++ language
apps, one is always best  when main is in C++ not C
(this does not address the problem above, it's just wise with issues
about static initialation important in C++).

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
               Producers of Comeau C++ 4.0 front-end pre-release
****WEB: http://www.comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310

 
 
 

help : C and C++ link together

Post by Paul Cifarell » Thu, 11 Sep 1997 04:00:00



> This works with Borland, Watcom, and GCC, or should if I got the right

> underscores for the '__cplusplus'.

This also works for the Sun4 C++ compiler, HPUX C++ compiler, (both
cfront compilers).

I would venture to guess that it works for ALL c++ compilers, possibly
even Microsoft (they do have a tendency to re-invent, after all).

Regards,
Paul C.

 
 
 

help : C and C++ link together

Post by Greg Come » Tue, 16 Sep 1997 04:00:00




>> This works with Borland, Watcom, and GCC, or should if I got the right

>> underscores for the '__cplusplus'.

>This also works for the Sun4 C++ compiler, HPUX C++ compiler, (both
>cfront compilers).

>I would venture to guess that it works for ALL c++ compilers, possibly
>even Microsoft (they do have a tendency to re-invent, after all).

That latter is quite true often :-(  In any event, __cplusplus
is a required macro to be set by C++ translators (as per the ISO draft)
so it would be surprising to find a C++ compiler that didn't set it
(including most old ones).

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
               Producers of Comeau C++ 4.0 front-end pre-release
****WEB: http://www.comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310