[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Apr 2 08:31:13 2002 
Subject:Re: external C call 
From:Jonathan L Cunningham 
Volume-ID:1020402.02 

On Mon, 1 Apr 2002 17:14:57 +0000 (UTC), Aaron.Sloman.XX@cs.bham.ac.uk
(Aaron Sloman See text for reply address) said:

>[To reply replace "Aaron.Sloman.XX" with "A.Sloman"]
>
>mhl@Cs.Nott.AC.UK writes:
>
>> Date: Mon, 1 Apr 2002 16:30:22 +0000 (UTC)
 
>> Here's the c function I'm using to test...  (It's in a file called test.cc)
>>
>> int foo(int x)
>> {

(snip)

>> I compile the test.cc file into a shared object library with the following commands...
>>
>> g++ -c -fpic test.cc
>> g++ -shared -o libtest.so test.o
>
>You have used the Gnu C++ compiler, not the C compiler which is gcc.
>The man gcc file on my linux machine states:
>
>       gcc    assumes preprocessed (.i) files are C and assumes C
>              style linking.
>
>       g++    assumes preprocessed (.i) files are C++ and assumes
>              C++ style linking.
>
>
>       Suffixes of source file names indicate  the  language  and
>       kind of processing to be done:
>
>       .c    C source; preprocess, compile, assemble
>       .C    C++ source; preprocess, compile, assemble
>       .cc   C++ source; preprocess, compile, assemble
>       .cxx  C++ source; preprocess, compile, assemble
>
>So you should use .c as the file suffix if you intend the program to
>compiled and linked as if it were a C program.

Obviously this is best if you are writing a C program, but if you
really do need C++, you can tell the compiler to use C style linking
for the functions you wish to import into poplog. For example, you
can declare foo in a header file thus:

extern "C" int foo(int x);

and then you can compile the .cc file as C++.


-- 
Jonathan L Cunningham