* How can you call C++ code from C code, using "DEC C" & "DEC C++ V1.3B"
@ 1995-08-10 4:00 Joe
1995-08-10 4:00 ` Michael Spratte
0 siblings, 1 reply; 2+ messages in thread
From: Joe @ 1995-08-10 4:00 UTC (permalink / raw)
Hi,
I have OSF/1 V3.0 using "DEC C" & "DEC C++ V1.3B". The problem I am having is
calling a archive compiled with 'C++' from with C code witch is compiled with
'C'.
The problem lies with linking the archive library to the generated C code. The
C++ compiler produces object code with a 'name' reference to functions
differently than does the C compiler. So in the end the linker can not make
the connection between the functions.
Could anyone give pointers on how to get around this, I am fluent in C but just
started in C++. I am trying to use a library written in C++ within software
written only in C.
An example of what I mean is shown below:
The C++ code:
--------------------->>>> function.cc <<<----------------------
#include <stdio.h>
void
swapme(int *p, int *q)
{
printf("in swapme: %d %d\n", *p, *q);
int t = *p;
*p = *q;
*q = t;
}
-----------------------------------------------------------------
The C main code:
-------------------->>>> testfunction.c <<<<----------------------
#include <stdio.h>
extern void swapme(int *, int *);
void
swapmelocal(int *p, int *q)
{
int t;
printf("in swapmelocal: %d %d\n", *p, *q);
t = *p;
*p = *q;
*q = t;
}
main()
{
int x = 10;
int y = 20;
printf("in main 1: x=%d x=%d\n", x, y);
swapmelocal(&x, &y);
printf("in main 2: x=%d x=%d\n", x, y);
swapme(&x, &y);
printf("in main 3: x=%d x=%d\n", x, y);
}
---------------------------------------------------------------------------
Doing the following compilations I get:
cxx -c function.cc
ar rc libfun.a function.o
cc -c testfunction.c
cc -o test testfunction.o libfun.a
ld:
Unresolved:
swapme
nm libfun.a
libfun.a[function.o]:
Name Value Type Size
_fpdata | 0000000000000000 | U | 0000000000000000
printf | 0000000000000000 | U | 0000000000000008
swapme(int*, int*) | 0000000000000000 | T | 0000000000000008
nm testfunction.o
Name Value Type Size
_fpdata | 0000000000000000 | U | 0000000000000000
main | 0000000000000112 | T | 0000000000000008
printf | 0000000000000000 | U | 0000000000000008
swapme | 0000000000000000 | U | 0000000000000008
swapmelocal | 0000000000000000 | T | 0000000000000008
As you can see between the nm output the linker can not match the function
'swapme' and 'swapme(int*, int*)' as the same.
I believe there is a solution on how to get around this, but in all the books
and refernece manuals I got for DEC C++ & C there is no mention of this
though it talks about calling C functions from C++ code. I am doing the opposite
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: How can you call C++ code from C code, using "DEC C" & "DEC C++ V1.3B"
1995-08-10 4:00 How can you call C++ code from C code, using "DEC C" & "DEC C++ V1.3B" Joe
@ 1995-08-10 4:00 ` Michael Spratte
0 siblings, 0 replies; 2+ messages in thread
From: Michael Spratte @ 1995-08-10 4:00 UTC (permalink / raw)
In article <40br6e$knf@merlin.resmel.bhp.com.au>, Joe Spanicek,BHP-Research PKL,, <joe@holly.resptk.bhp.com.au> writes:
|>Hi,
|>
|>I have OSF/1 V3.0 using "DEC C" & "DEC C++ V1.3B". The problem I am having is
|>calling a archive compiled with 'C++' from with C code witch is compiled with
|>'C'.
|>
|>The problem lies with linking the archive library to the generated C code. The
|>C++ compiler produces object code with a 'name' reference to functions
|>differently than does the C compiler. So in the end the linker can not make
|>the connection between the functions.
|>
|>Could anyone give pointers on how to get around this, I am fluent in C but just
|>started in C++. I am trying to use a library written in C++ within software
|>written only in C.
|>
Declare swapme in function.cc as `extern "C"':
#include <stdio.h>
extern "C" void // <<----------------------------------------------
swapme(int *p, int *q)
{
printf("in swapme: %d %d\n", *p, *q);
int t = *p;
*p = *q;
*q = t;
}
Michael Spratte
DSNlink Engineering
Digital Equipment Corporation
Colorado Springs, Colorado
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1995-08-10 4:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1995-08-10 4:00 How can you call C++ code from C code, using "DEC C" & "DEC C++ V1.3B" Joe
1995-08-10 4:00 ` Michael Spratte
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox