Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于调用C++;来自C++;由FORTRAN代码调用的函数 我一直在使用Fortran代码Ma.F90来发送一个数组到Falp.CPP,它将调用C++代码、Apdio.CPP和Addio.h。该代码在CentOS 4平台上正常工作,但当我将其移动到CentOS 6平台时,它给了我错误。我曾尝试在两台机器上使用相同版本的gcc(4.3.0),或在CentOS 6中使用更新版本4.4.7,但问题没有得到解决。我附上的代码过于简化的版本如下_C++_Gcc_Cmake_Fortran_Fortran Iso C Binding - Fatal编程技术网

用于调用C++;来自C++;由FORTRAN代码调用的函数 我一直在使用Fortran代码Ma.F90来发送一个数组到Falp.CPP,它将调用C++代码、Apdio.CPP和Addio.h。该代码在CentOS 4平台上正常工作,但当我将其移动到CentOS 6平台时,它给了我错误。我曾尝试在两台机器上使用相同版本的gcc(4.3.0),或在CentOS 6中使用更新版本4.4.7,但问题没有得到解决。我附上的代码过于简化的版本如下

用于调用C++;来自C++;由FORTRAN代码调用的函数 我一直在使用Fortran代码Ma.F90来发送一个数组到Falp.CPP,它将调用C++代码、Apdio.CPP和Addio.h。该代码在CentOS 4平台上正常工作,但当我将其移动到CentOS 6平台时,它给了我错误。我曾尝试在两台机器上使用相同版本的gcc(4.3.0),或在CentOS 6中使用更新版本4.4.7,但问题没有得到解决。我附上的代码过于简化的版本如下,c++,gcc,cmake,fortran,fortran-iso-c-binding,C++,Gcc,Cmake,Fortran,Fortran Iso C Binding,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

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
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时,为什么要链接到
g++
?另一种方法是,链接到
gfortran
并添加
-lstdc++

只需添加一行:
SET\u TARGET\u PROPERTIES(test.exe PROPERTIES LINKER\u LANGUAGE Fortran)


或者使用最新的GCC。即使在您的原始设置下,仍然支持的所有GCC版本都能正常工作。

从输出中我可以看出,问题出在fortran“main”文件中。显然fortran代码不生成“主”入口点。尝试编译一个简单的FORTRAN代码W/O,任何外部C++ LIBS,看看它是否工作,我也会运行<代码>使< /Cor>(在CGED生成的MaForm文件(s))作为<代码>使VBOSE=1 < /Cord >看到由GET(GCC,LD等)发出的确切命令,并且能够自己逐步地再现这个问题,W/O任何“构建系统”,只是你,代码、编译器和链接器。当我在没有任何cpp文件的情况下运行简单的fortran代码时,它工作正常。我删除了与addition.cpp和addition.h相关的所有内容,以使其更简单(因此只剩下对func.cpp的调用);但是,我得到了相同的错误。不要单独使用fortran90标记。尤其是在使用Fortran 2003 C互操作时。
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