简体   繁体   English

链接 fortran 和 c++ 使用 CMake - 跳过不兼容...错误

[英]link fortran and c++ using CMake - skipping incompatible … Error

One colleague did send me a Fortran function to include in my C++ program.一位同事确实向我发送了 Fortran function 以包含在我的 C++ 程序中。 So far, everything in my program is coded in C++.到目前为止,我程序中的所有内容都使用 C++ 编码。 To keep things simple (especially dependencies and installation) I thought I'll just re-code it in C++.为了简单起见(尤其是依赖项和安装),我想我会在 C++ 中重新编码。 Unfortunately, the code is very complex with many goto statements and other stuff I'm not very familiar with.不幸的是,代码非常复杂,包含许多goto语句和其他我不太熟悉的东西。 (I have never worked with Fortran and this is from an old scientific Fortran 77 program) (我从未使用过 Fortran,这是来自一个古老的科学 Fortran 77 程序)

Thus, I would like to call the Fortran function directly in C++.因此,我想直接在 C++ 中调用 Fortran function。 A prerequisite is, that I'm using CMake for my program and everything (like linking) has to be done in the CMake file.一个先决条件是,我将 CMake 用于我的程序,并且所有内容(如链接)都必须在 CMake 文件中完成。 Additionally, the CMake file should be as simple as possible since only scientists work and extend the program with no sophisticated programming background.此外,CMake 文件应该尽可能简单,因为只有科学家在没有复杂的编程背景的情况下工作和扩展程序。

I found many approaches and solutions on the internet - however, most are very complex dealing with modules and libraries - I only need to call one function, we are not working with libraries or such.我在互联网上找到了许多方法和解决方案——但是,大多数处理模块和库都非常复杂——我只需要调用一个 function,我们不使用库等。

Unfortunately, I get a lot of errors when executing my code:不幸的是,我在执行代码时遇到了很多错误:

c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/MinGW/lib/gcc/mingw32/6.3.0/libgfortran.dll.a when searching for -lgfortran c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/MinGW/lib/gcc/mingw32/6.3.0/libgfortran.a when searching for -lgfortran c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/MinGW/lib/gcc/mingw32/6.3.0\libgfortran.a when searching for -lgfortran c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/MinGW/lib/gcc/min c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: 跳过不兼容C:/MinGW/lib/gcc/mingw32/6.3.0/libgfortran.dll.a when searching for -lgfortran c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/. ./../../../x86_64-w64-mingw32/bin/ld.exe: 在搜索-lgfortran Z4A8A08F09D37408793FZ90时跳过不兼容的C:/MinGW/lib/gcc/mingw32/6.3.0/libgfortran.a: /mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe:跳过不兼容的C: /MinGW/lib/gcc/mingw32/6.3.0\libgfortran.a 时搜索 -lgfortran c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../.. /../../x86_64-w64-mingw32/bin/ld.exe: 跳过不兼容的 C:/MinGW/lib/gcc/min gw32/6.3.0/libgfortran.dll.a when searching for -lgfortran c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/MinGW/lib/gcc/mingw32/6.3.0/libgfortran.a when searching for -lgfortran c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgfortran gw32/6.3.0/libgfortran.dll.a 时搜索-lgfortran c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9...0/.. ./x86_64-w64-mingw32/bin/ld.exe:在搜索-lgfortran Z4A8A08F09D37B73795649038408B5./lib时跳过不兼容的C:/MinGW/lib/gcc/mingw32/6.3.0/libgfortran.a /gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe:找不到-lgfortran

My main question is: Are these errors due to a problem in my code or are they related to a problem with my environment?我的主要问题是:这些错误是由于我的代码问题还是与我的环境问题有关?

This is what my code looks like:这就是我的代码的样子:

main.cpp主文件

#include <iostream>

extern double f_add(double *, double *, double *);

int main() {
    double a = 1.;
    double b = 2.;
    double c;
    f_add(&a, &b, &c);

    std::cout << c << std::endl;
}

f_add.f f_add.f

  real function f_add(a, b, c)
  real a,b,c
  c = a+b
  end

CMakeLists.txt CMakeLists.txt

cmake_minimum_required(VERSION 3.17)
project(test_cpp)

set(CMAKE_CXX_STANDARD 14)
SET (CMAKE_Fortran_COMPILER  gfortran)
ENABLE_LANGUAGE(Fortran)

set(SOURCE_FILES
        main.cpp
        f_add.f
        )

add_executable(test_cpp ${SOURCE_FILES})

I think your C++ code is missing extern "C" and some additional corrections to the Fortran code.我认为您的 C++ 代码缺少extern "C"以及对 Fortran 代码的一些额外更正。 For example, the following would work:例如,以下将起作用:

#include <iostream>
extern "C" {
    double f_add(double, double);
}
int main() {
    double a = 1.;
    double b = 2.;
    double c;
    c = f_add(a, b);
    std::cout << c << std::endl;
}

and,和,

function f_add(a, b) result(c) bind(C, name = "f_add")
    use iso_c_binding, only: c_double
    implicit none ! remove this line if your F77 code has implicitly-declared variables.
    real(c_double), intent(in), value   :: a, b
    real(c_double)                      :: c
    c = a + b
end function f_add

Then compile, link, and run (via MinGW GNU 10.1 that I am using),然后编译、链接和运行(通过我正在使用的 MinGW GNU 10.1),

gfortran -c f_add.f90
g++ -c main.cpp
g++ *.o -o main.exe
./main.exe

The output is, output 是,

3

I do not have CMake installed in MinGW, but setting it up should be straightforward with the above modifications.我没有在 MinGW 中安装 CMake,但是通过上述修改设置它应该很简单。 Your CMake file is fully functional in a Linux environment if that helps.如果有帮助,您的 CMake 文件可以在 Linux 环境中完全正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM