1. Compaq Visual Fortran 6.6B에서 새 Fortran Static Library 프로젝트를 만들고 파일을 프로젝트에 추가합니다.
2. Build 메뉴에서 Set Active Configuration을 클릭하고 Release를 선택합니다.
3. 컴파일하고 빌드합니다. 이제 Fortran 프로젝트의 Release 하위 폴더에서 [myfortranlib].lib를 찾을 수 있습니다.
사용자 정의 함수의 C 코드 수정
Fortran 라이브러리를 준비했으면 C 코드에 라이브러리를 추가해야 합니다.
extern void __stdcall FORTRANFUNCTION(const double *array1, const double *scalar1 [, etc.]); // Since FORTRAN typically expects arguments by reference, arguments are passed // as pointers. To pass values, the FORTRAN code must contain a compiler directive // telling the function to expect a value rather than an address. LRESULT mcadfunction(LPCOMPLEXARRAY array1, LPCCOMPLEXSCALAR scalar1, etc.) // this defines the function before FUNCTIONINFO, using the same variable names // called by the external FORTRAN function. { // some error checking goes here, followed by the // actual call to the FORTRAN function. For example, FORTRANFUNCTION(&array1->hReal[0][0], &scalar1->real [, etc.]); // Either the function call must be in UPPERCASE, or you will have to set // Settings->FORTRAN->External procedures->External name implementation // to "Upper case" in your FORTRAN compiler. Any other C functionality follows... return 0; }
라이브러리 링크
1. MS Visual C++ 또는 Visual Studio의 기존 C 프로젝트에서 Linker 탭을 선택한 다음 Category 메뉴에서 Input을 선택합니다. 목록 끝에 있는 Object/library modules 텍스트 상자에 [myfortranlib].lib를 입력합니다.
2. Ignore libraries 텍스트 상자에 libc.lib를 입력합니다.
3. Additional Library Path 텍스트 상자에 [myfortranlib].lib 파일의 경로를 입력합니다(예: C:\temp).
4. DLL을 컴파일하고 빌드합니다.
추가 정보
• Fortran 코드는 직접 함수로 실행하는 것이 아니라 별도의 스레드로 실행해야 합니다. 이렇게 하면 프로세스를 종료하는 경우(예: Esc 누르기) 현재 활성화된 Fortran 서브루틴을 확인할 필요 없이 전체 스레드를 종료할 수 있습니다.
• Fortran 코드에서 모든 STOP 문을 호출한 C에 오류 코드를 반환하는 특수한 함수로 바꾸는 것이 좋습니다. 이렇게 해야 호출한 C에서 스레드를 안전하게 종료할 수 있습니다.
• Fortran 코드에 콘솔 PRINT 문이 있는 경우 콘솔을 파일에 재지정하여 출력이 화면 대신 파일에 기록되도록 변환할 수 있습니다. Fortran 루틴이 종료되고 오류가 발생하는 경우 C++ 프런트 엔드가 이 로그 파일을 읽고 창에 표시하도록 프로그래밍할 수 있습니다. 예를 들면 다음과 같습니다.
SUBROUTINE INIT_STDOUT () c this subro which redirects FORTRAN output to file use dfwin integer res c CALL close_stdout res=SETENVQQ("FOR_PRINT=C:\FORT_OUT.TXT") c PRINT *, 'Print a character to initialize ' PRINT *,' ' RETURN END
• Fortran 기반 DLL을 배포하고 싶다면 해당 DLL을 실행할 컴퓨터에 적절한 Fortran 런타임 라이브러리가 설치되어 있는지 확인해야 합니다. 다음과 같은 Fortran 런타임 라이브러리를 사용할 수 있습니다.
◦ DFORRT.DLL
◦ DFORRTD.DLL
◦ DFORMD.DLL
◦ DFORMDD.DLL
◦ DFDLG100.DLL
◦ MSVCRT.DLL
이러한 DLL이 누락된 경우 오류가 발생합니다. DLL은 사용 중인 Fortran 컴파일러에 패키지로 포함됩니다. Windows/system32/ 또는 WINNT/system32/ 디렉토리에 DLL을 설치해야 합니다.