Cmake配置文件,用于从FORTRAN代码调用的C++函数中调用C++代码
Cmake configutarion file for calling a C++ code from a C++ function called by a FORTRAN code
我一直在使用FORTRAN代码main.f90向func.cpp发送一个数组,该数组将调用C++代码addition.cpp&addition.h。该代码在centos4平台上运行正常,但当我将其移动到centos6平台时,它会给我带来错误。我曾尝试在两台机器上使用相同版本的gcc(4.3.0),或在centos6中使用更新的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;
}
附加.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时,为什么要与g++
链接?按另一种方式执行,链接到gfortran
并添加-lstdc++
。
只需添加行:SET_TARGET_PROPERTIES(test.exe PROPERTIES LINKER_LANGUAGE Fortran)
或者使用更新的GCC。所有仍然受支持的GCC版本都可以使用您的原始设置。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值