Re: (newbie) can compile with gcc but not with g++

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Kamaraju Kusumanchi <kk288@xxxxxxxxxxx> 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]

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux