From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,4bac94c463f961a6,start X-Google-Attributes: gidfee84,public From: Joe Spanicek,BHP-Research PKL,, Subject: How can you call C++ code from C code, using "DEC C" & "DEC C++ V1.3B" Date: 1995/08/10 Message-ID: <40br6e$knf@merlin.resmel.bhp.com.au>#1/1 X-Deja-AN: 107833383 organization: BHP Research - Melbourne Laboratories newsgroups: comp.unix.osf.osf1 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 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 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