我一直在使用FORTRAN代码main.f90将数组发送到func.cpp,它将调用C ++代码,add.cpp& addition.h。代码在CentOS 4平台上运行正常,但当我将它移动到CentOS 6平台时,它给了我错误。我曾尝试在两台机器上使用相同版本的gcc(4.3.0)或在CentOS 6中使用较新版本的4.4.7,但问题仍未解决。我将代码的过度简化版本附加为
main.f90时:
program main
use iso_c_binding
implicit none
interface
function func (a) bind (C, name="func")
import
integer(c_int):: func
real(c_double), dimension(1:4), intent(in):: a
end function func
end interface
real(c_double), dimension(1:4):: a = [ 2.3, 3.4, 4.5, 5.6 ]
integer(c_int):: result
result = func(a)
write (*,*) result
end program main
func.cpp:
#include <iostream>
#include "addition.h"
using namespace std;
#ifdef __cplusplus
extern"C" {
#endif
void *__gxx_personality_v0;
int func(double a[]) {
int i;
for(i=0; i<4; i++) {
cout << a[i] << endl;
}
int z;
z = addition (5,3);
cout << z << endl;
return 0;
}
#ifdef __cplusplus
}
#endif
addition.cpp:
#include <iostream>
#include "addition.h"
using namespace std;
int addition (int a, int b)
{
int r;
r = a + b;
return r;
}
addition.h:
#ifndef ADDITION_H
#define ADDITION_H
int addition (int a, int b);
#endif /* ADDITION_H */
的CMakeLists.txt:
PROJECT(test)
cmake_minimum_required(VERSION 2.6)
enable_language(C Fortran)
# Setting the compilers
set (CMAKE_Fortran_COMPILER /usr/bin/gfortran)
set (CMAKE_CXX_COMPILER /usr/bin/g++)
# Setting the flags
set (CMAKE_CXX_FLAGS "-lgfortran")
set_source_files_properties(main.f90 func.cpp PROPERTIES COMPILE_FLAGS -c)
# Making the executable
ADD_EXECUTABLE(test.exe main.f90 func.cpp addition.cpp addition.h)
我现在得到的错误是:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
make[2]: *** [test.exe] Error 1
make[1]: *** [CMakeFiles/test.exe.dir/all] Error 2
make: *** [all] Error 2
我感谢你解决这个问题的任何帮助。
答案 0 :(得分:1)
当您的主程序在Fortran中时,为什么要与g++
链接?以其他方式执行此操作,与gfortran
关联并添加-lstdc++
。
只需添加行:SET_TARGET_PROPERTIES(test.exe PROPERTIES LINKER_LANGUAGE Fortran)
或使用更近期的GCC。仍然支持的所有GCC版本甚至可以使用您的原始设置。