0
我一直在使用FORTRAN代码main.f90将数组发送到func.cpp,它将调用C++代码addition.cpp & addition.h。代码在CentOS 4平台上正常工作,但是当我将它移动到CentOS 6时,它给了我错误。我曾尝试在两台机器上使用相同版本的gcc(4.3.0),或在CentOS 6中使用较新版本4.4.7,但问题未解决。我附上的代码过于简单化版本Cmake配置文件,用于从FORTRAN代码调用的C++函数中调用C++代码
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;
}
除.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
我欣赏解决这个问题的任何帮助。
从我看到的输出中我可以说问题出在fortran“main”文件中。显然Fortran代码不会生成“主”入口点。尝试编译一个简单的Fortran代码,不带任何外部C++库,看看它是否可行 – user3159253 2014-10-03 18:54:14
另外,我会运行'make'(在CMake生成的Makefile(s))作为'make VERBOSE = 1'来查看确切的命令由make(gcc,ld等)发布,并且能够一步一步地自行重现问题,而不需要任何“构建系统”,只有你自己,代码,编译器和链接器。 – user3159253 2014-10-03 18:57:32
当我运行没有任何cpp文件的简单Fortran代码时,它可以正常工作。我删除了与addition.cpp和addition.h相关的所有内容,以使它更简单(所以只需要调用func.cpp左边)。但是,我收到了同样的错误。 – Vahid 2014-10-03 22:21:31