Fortran Coder

查看: 6797|回复: 1
打印 上一主题 下一主题

[混编] 混合编程中fortran调用C函数的问题?

[复制链接]

8

帖子

3

主题

0

精华

入门

F 币
30 元
贡献
16 点
跳转到指定楼层
楼主
发表于 2015-12-22 10:57:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
各位大神,我现在用fortran调用C的函数,目前的情况是这样,我将两个工程在同一个解决方案中分别建立,通过lib文件进行连接,编译通过应该可以证明没有编译错误,通过单步调试DEBUG发现二者已经链接上,但是二者之间数据似乎没有传递,感觉很奇怪,以下是代码:

fortran的部分:
[Fortran] 纯文本查看 复制代码
       SUBROUTINE ISAT
     1 (ELWRK, LRW, IELWRK, LIW, ANDTOL, EPSTOL, RNDTOL, STRIDE, YALL)
C
      INCLUDE 'CHEM.TXT'
      EXTERNAL BISRCH [C], BALNCE [C], INSRTD [C], ENLRGE [C], 
     &                FRMEM[C], STATIS[C], FUN, JAC
      DIMENSION YALL(KMAX), YTMP(KMAX), YQ(KMAX)
      DIMENSION ELWRK(LRW),IELWRK(LIW)
C
      INTEGER(4) nstep, ifind, iinteg, ienlge, irecrd
      INTEGER(4) IFLAG, NONEAR, BF, GS, JS, GSN, GSO, GROSS
      DOUBLE PRECISION ABOVE, BELOW, Y, DDY
      DIMENSION Y(KMAX),DDY(KMAX),RTOL(KMAX),ATOL(KMAX)
      DIMENSION ABOVE(KMAX+1),BELOW(KMAX+1),RPAR(KMAX),IPAR(KMAX)
c
      real*4 contm,difftm,chemtm,dintm,isatm,intgtm,srchtm,deltm,
     &       slargetm,baltm,tmchm,tmchm0
      common /stime/ difftm, contm, chemtm, dintm, isatm, intgtm,
     &               srchtm, deltm, slargetm, baltm
      common /sflag/ nstep, ifind, iinteg, ienlge, irecrd
      common /ode/ neq, mf, itol, iopt, itask, istate,rtol, atol
c
      COMMON /FIND/ Y, DDY, ABOVE, BELOW, BF, JS, GROSS
      COMMON /FLAG/ IFLAG, NONEAR, GSN, GSO
*
      DO I = 1, KK+1
         YTMP(I) = YALL(I)
         Y(I) = YTMP(I)
      END DO
*
      TMPTM0 = SECNDS(0.0)
      CALL BISRCH
      TMPTM = SECNDS(0.0)
      SRCHTM = SRCHTM + (TMPTM - TMPTM0)
*
      IF ((NONEAR.EQ.0).OR.(IFLAG.EQ.0)) THEN
         DO I = 1, KK+1
            YQ(I) = YTMP(I) + DDY(I)
         END DO
      END IF
*
      IF (IFLAG .EQ. 0) THEN
         DO I = 1, KK+1
            YALL(I) = YQ(I)
         END DO
         IFIND = IFIND + 1        
      ELSE
*
         ISTATE = 1
         TMP = 0.0
  360    CONTINUE
         TMPTM0 = SECNDS(0.0)
         CALL DVODE(FUN,NEQ,YTMP,TMP,STRIDE,ITOL,RTOL,ATOL,ITASK,ISTATE,
     &              IOPT,ELWRK,LRW,IELWRK,LIW,JAC,MF,RPAR,IPAR)
         TMPTM = SECNDS(0.0)
         INTGTM = INTGTM + (TMPTM - TMPTM0)
         IINTEG = IINTEG + 1
C
  900    CONTINUE
      END IF
C
      RETURN
      END

C的部分:
[C] 纯文本查看 复制代码
#include "stdafx.h"
#include "isatcdef.h"

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>

// specification of the structures and some variables.
struct incrmnt
{
        double y[lenth];
        double ddy[lenth];
        double above[lenth];
        double below[lenth];
        int bf;
        int js;
        int gross;
        struct incrmnt *lchild;
        struct incrmnt *rchild;
    struct incrmnt *father;
};

struct strctflag
{
        int iflag;
        int nonear;
    int gsn;
        int gso;
};
struct ssflag
{    int nstep;
     int ifind;
     int iinteg;
     int ienlge;
     int irecrd;
};

struct incrmnt FIND;
struct strctflag FLAG;
struct ssflag SFLAG;

struct incrmnt *phead=NULL,*pfind=NULL,*pfather=NULL,
*pnear=NULL,*q=NULL,*a=NULL,*f=NULL,*b=NULL,*pd=NULL;

double plus[lenth],minus[lenth];
double min=Maxmin;

// end of the specifications.

extern "C" void bisrch() 
{
        int compar(struct incrmnt *,struct incrmnt *);

        struct incrmnt *p;
        int flag,i;
              
//        initialize pfind.

//        pfind=(struct incrmnt *)malloc(sizeof(struct incrmnt));
        pfind=new incrmnt;
        for(i=0;i<=lenth-1;i++)
                pfind->y[i]=FIND.y[i];
        pfind->lchild=NULL;
        pfind->rchild=NULL;
        pfind->father=NULL;
        pfind->bf=0;
    pfind->js=0;
    pfind->gross=0;
        
        if (phead==NULL)
        {
                phead=pfind;
                FLAG.iflag=1;
                FLAG.nonear=1;
                return;
        }
        else
/*to find the location for pfind, insert it and adjust the bitree*/  
        {
                f=NULL;a=phead;p=phead;q=NULL;
/*to find the location to insert pfind*/
                while(p!=NULL)          
                {
                        if(p->bf!=0)
                        {a=p;f=q;}         /* adjustment will start from node a,*/
                                           /*  f is the parents node of a */
                        //p->father=q;
                        q=p;
                        flag=compar(pfind,p);  /*comparing the keyword*/
                    if(flag==0)
                        {
                                for(i=0;i<=lenth-1;i++)
                                {
                                        FIND.ddy[i]=p->ddy[i];
                                }
                p->js=p->js+1;
                                FIND.js=p->js;
                                min=Maxmin;
                                pnear=NULL;
                                delete pfind;
                                FLAG.iflag=0;
                                return;
                        }
                        else
                                if(flag==1)
                                        p=p->lchild;
                                else
                                        p=p->rchild;
                }
                if (fabs(min-Maxmin)<=1.e-10)
                        FLAG.nonear=1;
                else
                {
                        FLAG.nonear=0;
                        for (i=0;i<=lenth-1;i++)
                        {
                                FIND.ddy[i]=pnear->ddy[i];
                        }
                }
                FLAG.iflag=1;
                return;
        }
}

还有一个比较奇怪的地方是报错竟然是stack trace terminated abnormally,这个程序之前是有人用过的,不过是在CVF编译器上面实现的,但至少在堆栈上应该是不会存在问题的,不知道是否跟换编译器有关系,或者是我之前所说的链接问题?
说的比较杂乱,希望大神们不要怪罪!
十分感谢各位!!!
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

沙发
发表于 2015-12-22 18:47:50 | 只看该作者
楼主没有传递任何参数。此问题,已在QQ群解决。
推荐用 ISO_C_BINDING 来做 C 混编
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-7 11:27

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表