sorry !!
the error is "undefined reference to sub"
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 isQuote:}
when I complie with "cc -o proc pro1.c pro2.cpp",Quote:}
How can I solve it ?
thankx ....
>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 ?
#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
>>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.
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
> underscores for the '__cplusplus'.
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.
>> 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).
- 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
Hi,
Any comments on the setup of the Matrox Mystique, with Xfree86 would be
appreciated.
Cheers,
David Pearson
2. non-blocking accept on sockets
4. New account doesn't load to X-Windows
5. Using cc and c++ plus together
7. compile and build c++ and c files together
8. mounting cd-rom, relative newbie
9. compile and build c++ and c file together
10. Help - want C++ generic linked-list class code
12. Help! Linking to user library with C++
13. Using C99 standard and C++ together