如何在Xcode 4.5中向项目添加对FORTRAN代码的调用?

时间:2022-10-30 13:58:31

My company has a bunch of Fortran code. Traditionally, we compiled the code we needed into a .dll and called that .dll when we needed a calculation done. We are now trying to create an iPad app, which unfortunately means we can't just call a .dll anymore.

我的公司有一堆Fortran代码。传统上,我们将所需的代码编译成.dll,并在需要完成计算时调用.dll。我们现在正在尝试创建一个iPad应用程序,遗憾的是我们不能再调用.dll了。

One of my coworkers have managed to make a simple Command Line Tool project, where we call a Fortran file to write "Hello, World" in the debugger. However, when I try to get it to work on view based iPad app, I get a bunch of linker errors saying the symbols I'm using cannot be found. I know that the Command Line Tool uses a .cpp file to actually run the Fortran, and I've seen many threads concerning calling .cpp files in an app, but all the ones I've seen are outdated, directly contradict each other, and their fixes don't work for me.

我的一个同事设法创建了一个简单的命令行工具项目,我们调用Fortran文件在调试器中编写“Hello,World”。但是,当我试图让它在基于视图的iPad应用程序上工作时,我收到一堆链接器错误,说明我正在使用的符号无法找到。我知道命令行工具使用.cpp文件来实际运行Fortran,我看到很多关于在应用程序中调用.cpp文件的线程,但我看到的所有文件都已过时,直接相互矛盾,他们的修复不适合我。

My question is, is there a more direct way to call Fortran straight from a .m file? If not, what do I have to do to take the working Command Line Tool and get it into an app?

我的问题是,是否有更直接的方式直接从.m文件调用Fortran?如果没有,我需要做什么才能使用命令行工具并将其放入应用程序?

UPDATE 1: following the tutorials posted in the comments, I have been able to create a .o file from my Fortran code. I can do a File-Add Files to add it in easily enough, but now how do I actually call it?

更新1:按照评论中发布的教程,我已经能够从我的Fortran代码创建一个.o文件。我可以做一个文件添加文件来轻松添加它,但现在我该如何实际调用呢?

UPDATE 2: Okay, baby steps. I found out you can make a .a static library (I'll call it "new_library") from .o files ("source_file.o") using the Terminal command ar crv new_library.a source_file.o (you can do it for multiple .o files by just adding source_file2.o source_file3.o for as many .o files as you want - NOTE: make sure you use cd to get to the folder where the .o files are located). I'm pretty sure Xcode will work with .a files, but it seems a .h file is still needed to let the other files in the project (like the view controllers) make calls to what's in the .a file. I know I can make a new .a file from Xcode itself (New Project -> iOS -> Framework & Library -> Cocoa Touch Static Library), but when I've done it that way in the past, I just write normal .m and .h files, and when I build the new library it just mashes all the .m files into 1 .a. When I want to use the code in that .a in another project, I just import the .a and all the .h files into the new project, and I can call the methods in the .a file just as if I had imported all the separate .m files. So the next question is, do I still need a .h when my .a is made with the terminal instead of Xcode? If so, how would I make a Fortran header file? If not, how do I call my Fortran code in the .a?

更新2:好的,宝贝步骤。我发现你可以使用终端命令ar crv new_library.a source_file.o从.o文件(“source_file.o”)创建一个.a静态库(我称之为“new_library”)(你可以这样做)多个.o文件,只需添加source_file2.o source_file3.o即可获得任意数量的.o文件 - 注意:确保使用cd进入.o文件所在的文件夹。我很确定Xcode可以使用.a文件,但似乎还需要.h文件才能让项目中的其他文件(如视图控制器)调用.a文件中的内容。我知道我可以从Xcode本身创建一个新的.a文件(新项目 - > iOS - >框架和库 - > Cocoa Touch静态库),但是当我过去这样做时,我只是写正常。 m和.h文件,当我构建新库时,它只是将所有.m文件混合成1 .a。当我想在另一个项目中使用.a中的代码时,我只是将.a和所有.h文件导入到新项目中,我可以调用.a文件中的方法,就好像我已经导入了所有单独的.m文件。所以下一个问题是,当我的.a是用终端代替Xcode时,我还需要.h吗?如果是这样,我将如何制作Fortran头文件?如果没有,我如何在.a中调用我的Fortran代码?

1 个解决方案

#1


4  

Let's try to sum up things:

让我们试着总结一下:

  1. You need a FORTRAN cross compiler that can produce ARM code. GCC is probably a good choice. But you will need to build it yourself from the source downloads.

    您需要一个可以生成ARM代码的FORTRAN交叉编译器。 GCC可能是一个不错的选择。但是你需要自己从源代码下载中构建它。

  2. Compile your FORTRAN code and put it into a static library. This task is perform outside XCode, probably from the command line.

    编译FORTRAN代码并将其放入静态库中。此任务在XCode外部执行,可能来自命令行。

  3. Write C header files for those FORTRAN routines that you wand to call directly from C. You might want to look at Fortran and C/C++ mixed programming or Tutorial: Using C/C++ and Fortran together. These pages explain how to map the C data types to FORTRAN data types and vice versa.

    编写用于直接从C调用的FORTRAN例程的C头文件。您可能希望查看Fortran和C / C ++混合编程或教程:一起使用C / C ++和Fortran。这些页面解释了如何将C数据类型映射到FORTRAN数据类型,反之亦然。

  4. Add the C header files and the static library to the XCode project.

    将C头文件和静态库添加到XCode项目。

  5. Import the C header files into your Objective-C files (.m) where required and call the FORTRAN routines as if they were C functions.

    在需要时将C头文件导入Objective-C文件(.m)并调用FORTRAN例程,就好像它们是C函数一样。

  6. Build the iOS app with XCode.

    使用XCode构建iOS应用程序。

#1


4  

Let's try to sum up things:

让我们试着总结一下:

  1. You need a FORTRAN cross compiler that can produce ARM code. GCC is probably a good choice. But you will need to build it yourself from the source downloads.

    您需要一个可以生成ARM代码的FORTRAN交叉编译器。 GCC可能是一个不错的选择。但是你需要自己从源代码下载中构建它。

  2. Compile your FORTRAN code and put it into a static library. This task is perform outside XCode, probably from the command line.

    编译FORTRAN代码并将其放入静态库中。此任务在XCode外部执行,可能来自命令行。

  3. Write C header files for those FORTRAN routines that you wand to call directly from C. You might want to look at Fortran and C/C++ mixed programming or Tutorial: Using C/C++ and Fortran together. These pages explain how to map the C data types to FORTRAN data types and vice versa.

    编写用于直接从C调用的FORTRAN例程的C头文件。您可能希望查看Fortran和C / C ++混合编程或教程:一起使用C / C ++和Fortran。这些页面解释了如何将C数据类型映射到FORTRAN数据类型,反之亦然。

  4. Add the C header files and the static library to the XCode project.

    将C头文件和静态库添加到XCode项目。

  5. Import the C header files into your Objective-C files (.m) where required and call the FORTRAN routines as if they were C functions.

    在需要时将C头文件导入Objective-C文件(.m)并调用FORTRAN例程,就好像它们是C函数一样。

  6. Build the iOS app with XCode.

    使用XCode构建iOS应用程序。