Linking C code with GCJ

Kresten Krab Thorup krab@daimi.au.dk
Tue Oct 19 03:23:00 GMT 1999


"Daniel P. Zepeda" <dpz@pobox.com> writes:

> Hi,
> 	Is there a way to link object code created with a C compiler in
> with the rest of my C++/Java code that is called from the C++ side 
> using CNI? 
> 
> 	The reason I ask is I have this set of C libraries that I want to
> use with my Java program.  They are written in straight C. I figured all I
> need to do is change the compiler from gcc to g++ and compile them to use
> with CNI and Java. I know the programs are correct because I can compile
> and run them with gcc in C mode. The problem comes when I try to compile
> them with the C++ compiler. I get a lot of:
> 
> assignment to `unsigned char *' from `char *' changes signedness 
> 

C is not really a subset of C++, as you might think.  C++ is more
strict in several ways.  

Therefore, it is easier to compile C files with a C compiler.  

Thereafter, you can write declarations for your C functions in a C++
module, as in:

  extern "C" my_c_function (char *foo);

Or, if the declarations are in a header file, you may have suceess
with:

  extern "C" {
  #include "c_header_file.h"
  }

but then again, maybe not.

Given such a declaration, you can write a CNI stub, that calls your
favourite C function.  Compile this with the c++ compiler.

Link everything together using the gcj command.

-- Kresten

 Kresten Krab Thorup           "I like my eggs ploded"
 Department of Computer Science, University of Aarhus
 Aabogade 34, DK-8200 Aarhus N, Denmark
 +45 8942 5665 (office), +45 2343 4626 (mobile)


More information about the Java mailing list