This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: (newbie) can compile with gcc but not with g++
- From: llewelly at xmission dot com
- To: Kamaraju Kusumanchi <kk288 at cornell dot edu>
- Cc: gcc-help at gcc dot gnu dot org
- Date: 09 Apr 2004 20:38:32 -0600
- Subject: Re: (newbie) can compile with gcc but not with g++
- References: <40772CD2.3040706@cornell.edu>
Kamaraju Kusumanchi <kk288@cornell.edu> writes:
> Hi all
> I am a newbie regarding c/c++ programming. From what I read, If I
> write a C program, I can compile with both gcc and g++.
Sometimes. C++ is not 100% backward compatible with C - more like 95%.
Stroustrup covers this in TC++PL3 (a book every C++ programmer should
own), section B.2 . However, your program is outside the scope of
compatiblity between C and C++.
Your issue is with C++ compatiblity with Fortran, *not* C compatiblity
with C++.
> I'm not able
> to do the later. Here are the details.
>
> I compiled the clapack package (downloaded from netlib) on intel p4
> 2.8 using gcc (3.3.3). I am using debian testing. Consider the
> following program
>
> #include <stdio.h>
I think this program needs something like this:
extern "C" void dgesv_(int*, int*, double*, int*, int*, double*, int*, int*);
I'm guessing that your fortran library uses C calling conventions. If
it doesn't, I don't know what the answer is.
>
> int main()
> {
> double a[16] = {11,14,15,16,7,10,13,12,5,6,8,9,1,2,3,4};
> /* Actual matrix is 11 7 5 1
> * 14 10 6 2
> * 15 13 8 3
> * 16 12 9 4
> */
> double b[4] = {1.2,3.2,6.2,2.4};
> int M=4,N=1,lda=4,ldb=4,ipiv[4],info, ret;
> int i,j;
>
> for(i=0; i<4; i++) {
> for(j=0; j<4; j++) {
> printf("%f ", a[i*4+j]);
> }
> printf("\n");
> }
> for(i=0; i<4; i++) printf("%f\n", b[i]);
> dgesv_(&M, &N, a, &lda, ipiv, b, &ldb, &info);
C++ requires all functions to have appropriate declarations.
> for(i=0; i<4; i++) printf("%f\n", b[i]);
> // Correct answer is -0.76, 1.83, -0.51, -0.70
> printf("%d\n",info);
> return 0;
> }
>
> I am able to run this program with gcc using the following commands.
>
> gcc -c -O2 ludecomp.c
> gcc ludecomp.o libtmglib.a liblapack.a libblas.a libF77.a libI77.a -lm -lc
> ./a.out
>
> But when I use g++, I am getting the following error
>
> mv ludecomp.c ludecomp.cpp
> g++ -c ludecomp.cpp
> ludecomp.cpp: In function `int main()':
> ludecomp.cpp:22: error: `dgesv_' undeclared (first use this function)
> ludecomp.cpp:22: error: (Each undeclared identifier is reported only
> once for
> each function it appears in.)
[snip]