2016-09-12 8 views
0

이 코드를 실행하고 실행하려면 어떻게해야합니까? 예를 들어 10x10 배열을 원한다면. 터미널에 gfortran hehe.f90 -o hehe을 시도했지만 오류 메시지가 나타납니다. 내 파일은 내가 할간단한 함수를 컴파일하면 정의되지 않은 기호가 발생합니다. "_main"

오류 메시지가 hehe.f90라고 :

Undefined symbols for architecture x86_64: 
    "_main", referenced from: 
    implicit entry/start for main executable 
ld: symbol(s) not found for architecture x86_64 
collect2: error: ld returned 1 exit status 

내 코드입니다 : 내가 납니까 일의

function test(n) 

!input 
integer :: n 


!output 
integer :: arraytest(n,n) 


!local 
integer :: i,j 


arraytest=0.0d0 

do i=1,n 
    do j=1,n 

arraytest(i,j)=i*i 

end do 

end do 



end function test 
+1

게시물에 오류 메시지를 추가하십시오. 대답을 얻을 확률이 높아질 것입니다. (저는'gfortran '을 사용하지 않지만 다른 사람들은 도울 수 있습니다). –

+0

'n'은 절대 10으로 초기화되지 않습니다. 상수 값 10에 'integer, parameter :: n = 10' 선언을 추가하십시오. – ja72

+0

또한 정적 배열 (미리 정의 된 크기)을 원할지 결정해야합니다. 할당 가능 배열은 프로그램 실행시 크기가 결정됩니다. – ja72

답변

1

커플.

  1. 이것은 프로그램이 아닌 기능입니다. 당신은 프로그램을 시작하면, 당신은 인터페이스와 같은 합병증을 피하기 위해 IMPLICIT NONE을 추가해야 PROGRAM <programname>

  2. 와 스크립트를 시작해야한다. 당신이해야 할 절대 최소 그래서,

    program <programname> 
    
    implicit none 
    
    <programbody> 
    
    stop 
    
    contains 
    
    function <functionname> (<variables>) result (<variable>) 
    
    end function <functionname> 
    
    subroutine <subroutinename> (<variables>) 
    
    end subroutine (<subroutinename>) 
    
    end program <programname> 
    
1

당신은 서브 루틴으로 test(n) 선언 - 당신이 프로그램 본체에 정의 된 함수 나 서브 루틴을 갖고 싶어

  • 이 예제 구조에 따라 (10,10)이이 모양을 만들 것입니다 동안이 같은 프로그램 내에서 호출합니다 :

    program hehe 
        implicit none 
        call test(10) 
    contains 
        subroutine test(n) 
         implicit none 
         . 
         . 
         . 
        end subroutine test 
    end program hehe 
    

    을하지만 이것은 당신의 문제의 끝이 아니다 배열을 u이라고하고 값을 채우면 서브 루틴이 끝날 때 즉시 다시 잊어 버리게됩니다. 어떤 컴파일러는 비밀리에 불필요한 도랑 것이 여전히 가능,

    program hehe 
        implicit none 
        integer, parameter :: n = 10 
        integer :: u(n, n) 
        call test(n, u) 
    contains 
        subroutine test(n, u) 
         implicit none 
         integer, intent(in) :: n 
         integer, intent(out) :: u(n, n) 
         < populate u > 
        end subroutine test 
    end program hehe 
    

    여전히 u로 아무것도 안하고 있기 때문에 : 당신이 u을 유지하려면

    , 당신은 좀 더 참여해야 u의 계산이지만, 예를 들어 인쇄 할 경우 예상 한대로 처리해야합니다.

    당신은 또한 기능을 사용할 수 있습니다. 두 변수 이름이 동일하지 않음을 보여주기 기능에

    program hehe 
        implicit none 
        integer, parameter :: n = 10 
        integer :: u(n, n) 
        u = test(n) 
    contains 
        function test(n) result(v) 
         implicit none 
         integer, intent(in) :: n 
         integer :: v(n, n) 
         < populate v > 
        end function test 
    end program hehe 
    

    (내가 사용한 적이 vtest(n)을 2 차원 배열을 반환합니다, 당신이 저장하는 것은 당신에게 달렸습니다.)

    내가 완벽을 위해 포함시키고 싶은 옵션이 있지만, 사용하지 못하게하는 것이 좋습니다. : (서브 루틴과 함수 모두) contains 섹션은 새 변수가 exp가 아닌 한 프로그램의 변수에 액세스 할 수 있습니다. 뭔가 잘못되면 이해하기 엉망이기 때문에 나는이가 마음에 들지

    program hehe 
        implicit none 
        integer, parameter :: n = 10 
        integer :: u(n, n) 
        call test() 
        write(*, '(10I6)') u 
    contains 
        subroutine test() 
         implicit none 
         integer :: i, j ! n and u not declared here, taken from program. 
         u = reshape((/((100*i + j, i = 1, n), j = 1, n)/), (/n, n/)) 
        end subroutine test 
    end program hehe 
    

    이유 :이 너무 일을 할 수 있도록 licitly, 같은 이름으로 선언했다. "u은 (는) 어디에서 변경 되었습니까?" 당신은 기본적으로 모든 곳을보아야합니다.