public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* C++/C Linkage problems
@ 2002-07-22 23:25 손석배
  2002-07-23  0:34 ` Claudio Bley
  0 siblings, 1 reply; 4+ messages in thread
From: 손석배 @ 2002-07-22 23:25 UTC (permalink / raw)
  To: gcc-help

I made a library written in C++.
Instead of offering library classes, the library offers C type library functions to the outside.
(This library is compiled by g++)

Now, I write a program written in C and this program use functions from the library that I mentioned above.

I wanted to compile and link this C code with gcc(c complier), not g++(c++ compiler).
Is it possible? If so, what option should I use?
If it's not possible, what is the solution for this case?

Restrication : 
1. Library should be written in C++ 
2. The code which use library should be written and complied by c complier
3. I want to use gcc to link this c code.

* P.S. I don't know if "Restrication 3" is the strict one. But 1, 2 should be kept strictly.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C++/C Linkage problems
  2002-07-22 23:25 C++/C Linkage problems 손석배
@ 2002-07-23  0:34 ` Claudio Bley
  0 siblings, 0 replies; 4+ messages in thread
From: Claudio Bley @ 2002-07-23  0:34 UTC (permalink / raw)
  To: sb.son; +Cc: gcc-help

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

>>>>> "sb" == ^[$(C<U<.9h^[(B  <sb.son@partner.samsung.com> writes:

    sb> I made a library written in C++.  Instead of offering library
    sb> classes, the library offers C type library functions to the
    sb> outside.  (This library is compiled by g++)

    sb> Now, I write a program written in C and this program use
    sb> functions from the library that I mentioned above.

    sb> I wanted to compile and link this C code with gcc(c complier),
    sb> not g++(c++ compiler).  Is it possible? If so, what option
    sb> should I use?  If it's not possible, what is the solution for
    sb> this case?

    sb> Restrication : 1. Library should be written in C++ 2. The code
    sb> which use library should be written and complied by c complier
    sb> 3. I want to use gcc to link this c code.

    sb> * P.S. I don't know if "Restrication 3" is the strict one. But
    sb> 1, 2 should be kept strictly.

Yes, it is possible. Look at this:

,----[ test.cc ]
| #include <iostream>
| 
| using namespace std;
| 
| extern "C" {
|   bool prInt (int& x) {
|     cout << x << endl;
|     return cout;
|   }
| }
`----

,----[ main.c ]
| #include <stdio.h>
| 
| extern int prInt (int *x); /* [1] */
| 
| int main (int argc, char *argv[])
| {
|   printf ("%s\n", prInt (&argc) ? "good" : "failed");
| 
|   return 0;
| }
`----

Basically, you need to declare the functions you want to call from C
in your library as extern "C" to prevent the C++ name mangling scheme
being applied to them. You may just define wrapper functions that just
call a work function of your library internally:

bool cpp_prInt (int &i) { std::cout << i << std::endl; return std::cout; }
extern "C" int prInt (int i) { return cpp_prInt (i); }

* [1] of course, you need to map the C++ types to the appropriate C
  types here.

$ g++ -c -fPIC test.cc
$ g++ -shared -o libtest.so test.o
$ gcc -c main.c
$ gcc -L. main.o -ltest

$ LD_LIBRARY_PATH=. ./a.out 1 2 3 4
5
good


HTH
Claudio
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>

iD8DBQE9PQb6TpSishmp0ioRAgWsAJ0a4K4qypCq/cxO5hVxFHHkP+R1eQCdGnvy
o1kNXUK9cmz4Nmq+dzSuWs8=
=mCr1
-----END PGP SIGNATURE-----

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C++/C Linkage problems
  2000-06-06 10:24 Reg Anderson
@ 2000-06-06 14:58 ` Martin v. Loewis
  0 siblings, 0 replies; 4+ messages in thread
From: Martin v. Loewis @ 2000-06-06 14:58 UTC (permalink / raw)
  To: reg.anderson; +Cc: help-gcc

> I'm attempting to put a "C" wrapper on a shared object file written in C++.
> Can anybody tell me the compiler and linker options I need t use to get
> another "C" program to compile and link to the shared object file?

You don't need any special options. Just link with g++.

Regards,
Martin

^ permalink raw reply	[flat|nested] 4+ messages in thread

* C++/C Linkage problems
@ 2000-06-06 10:24 Reg Anderson
  2000-06-06 14:58 ` Martin v. Loewis
  0 siblings, 1 reply; 4+ messages in thread
From: Reg Anderson @ 2000-06-06 10:24 UTC (permalink / raw)
  To: help-gcc

I'm attempting to put a "C" wrapper on a shared object file written in C++.
Can anybody tell me the compiler and linker options I need t use to get
another "C" program to compile and link to the shared object file?

Any help would be much appreciated.
Thx.

Reg Anderson
reg.anderson@epic-systems.com



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-07-23  7:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-22 23:25 C++/C Linkage problems 손석배
2002-07-23  0:34 ` Claudio Bley
  -- strict thread matches above, loose matches on Subject: below --
2000-06-06 10:24 Reg Anderson
2000-06-06 14:58 ` Martin v. Loewis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).