2011-04-14 2 views
28

gcc를 사용하여 g ++ 또는 gfortran을 사용하여 c와 C++ 사이 또는 c와 fortran 사이에서 각각 호출 할 수 있습니다. 그러나 C++과 Fortran 사이에서 프로 시저 호출을 시도하면 g ++ 또는 gfortran으로 컴파일 할 때 오류가 발생합니다. 둘 다 다른 필수 라이브러리에 대해 알지 못하기 때문입니다. C++과 Fortran 모두로 작성된 소스 코드를 사용하는 프로젝트를 어떻게 링크 할 수 있습니까? 정의되지 않은 참조에 대한gcc를 사용하여 포트란과 C++ 바이너리 연결하기

$ g++ main.o print_hi.o -o main 
print_hi.o: In function `print_hi': 
print_hi.f90:(.text+0x3f): undefined reference to `_gfortran_st_write' 

추가 오류 :

$ cat print_hi.f90 
subroutine print_hi() bind(C) 
    implicit none 
    write(*,*) "Hello from Fortran." 
end subroutine print_hi 

$ cat main.cpp 
#include <iostream> 

extern "C" void print_hi(void); 

using namespace std; 

int main() { 
    print_hi(); 
    cout << "Hello from C++" << endl; 
    return 0; 
} 
$ gfortran -c print_hi.f90 -o print_hi.o 
$ g++ -c main.cpp -o main.o 

나는 ++ g로 연결하려고합니다.

그리고 gfortran와

:

$ gfortran main.o print_hi.o -o main 
main.o: In function `main': 
main.cpp:(.text+0xf): undefined reference to `std::cout' 

... 등등.

어떻게 gfortran과 g ++ 라이브러리를 함께 사용하여 바이너리를 링크 할 수 있습니까?

답변

47

표준 포트란 라이브러리에 연결하려면 g++ main.o print_hi.o -o main -lgfortran을 찾고 있습니다.

+8

우수 감사합니다. 또한'-lstdC++'를 전달하여 gfortran을 사용할 수도 있습니다. – sverre

관련 문제