无法从Fortran调用C模块 [英] Cannot call a C module from Fortran

查看:97
本文介绍了无法从Fortran调用C模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Fortran程序中调用C模块。无论我尝试了什么 -

链接器都找不到C模块。我知道领先的

下划线,甚至关闭了。我抽象了一切

。当我用Fortran子程序替换C模块时,链接

有效。这里的Fortran子程序取代了C模块进行了
测试:


子程序qqcprint

返回

结束


但是当我在同一个库中使用这个C模块(替换Fortran

testmodule)时,链接不再起作用了。 />

无效qqcprint()

{

返回;

}


我也试过这个:


extern void qqcprint()

{

return;

}


结果总是一样的。使用Fortran子程序链接

有效,其中一个C模块我收到消息


qqmodlib.a(qqprint.o):qqprint.for: (.text + 0x42):未定义引用

`qqcprint''


那么我做错了什么?

I want to call a C module from a Fortran program. Whatever I tried -
the linker could not find the C module. I know about the leading
underscore and switched even that off. I abstracted everything
possible. When I replace the C module by a Fortran subroutine linking
works. Here the Fortran subroutine that replaced the C module for
testing:

subroutine qqcprint
return
end

But when I use this C module in the same library (replacing the Fortran
testmodule) linking didn''t work any longer.

void qqcprint()
{
return;
}

I also tried it with this one:

extern void qqcprint()
{
return;
}

The result was always the same. With the Fortran subroutine linking
works, with one of the C modules I get the message

qqmodlib.a(qqprint.o):qqprint.for:(.text+0x42): undefined reference to
`qqcprint''

So what am I doing wrong?

推荐答案

Cottonwood写道:
Cottonwood wrote:
我想从Fortran程序中调用C模块。无论我做了什么 -
链接器都找不到C模块。
I want to call a C module from a Fortran program. Whatever I tried -
the linker could not find the C module.




这应该在编译器的文档中介绍,假设你是

对两种语言使用相同的工具链。


-

Ian Collins。



This should be covered in your compiler''s documentation, assuming you
are using the same tool chain for both languages.

--
Ian Collins.


附加信息:从C调用Fortran模块是绝对的

没问题。当我在lib中有Fortran testmodule时,没有

消息。但是如果Fortran子例程调用C testmodule(同样的

lib,我只是替换了模块),则会出现消息:


[链接器错误]未定义引用到'qqcprint''

什么表明这里有同样的问题。


文档没有帮助。它只说:


与G95程序接口


虽然g95生成独立的可执行文件,但偶尔会有

希望与其他程序接口,通常是C.多语言程序将面临的第一个

难度是

公共符号的名称。 G95遵循f2c惯例,在公共名称中添加下划线

,或者如果名称包含下划线,则使用两个下划线。

-fno-second-underscore和-fno-下划线对于
强制g95生成与C编译器兼容的名称非常有用。


使用''nm''程序查看o文件

编译器生产。 G95也将公共名称折叠为小写,除非给出了
-fupper-case,在这种情况下一切都是大写的。

模块名称表示为模块名称-MP-name。


链接后,主要有两种情况:FORTRAN调用C子程序

和C调用FORTRAN子程序。对于C调用FORTRAN子例程,

FORTRAN子例程通常会调用FORTRAN库子例程

,期望以某种方式初始化堆。


要强制从C进行手动初始化,请在完成后调用g95_runtime_start()以

初始化FORTRAN库和g95_runtime_stop()。 g95_runtime-start()的原型是
原型:


void g95_runtime-start(int argc,char * argv []);


库必须能够处理命令行选项。如果这是尴尬的,你的程序不需要命令行

参数,则传递argc = 0和argv = NULL。


在OSX / Tiger上,包含''-SSystemStubs'',当使用g95运行链接器

并链接由gcc编译的对象文件时。


===> nm程序不在交付中。

Additional information: Calling a Fortran module from C is absolutely
no problem. When I have the Fortran testmodule in the lib there''s no
message. But if the Fortran subroutine calls the C testmodule (same
lib, I just replaced the module), the message appears:

[Linker error] undefined reference to `qqcprint''
what shows that here''s the same problem.

The documentation doesn''t help. It says only:

Interfacing with G95 Programs

While g95 produces stand-alone executables, it is occasionally
desirable to interface with other programs, usually C. The first
difficulty that a multi-language program will face is the names of the
public symbols. G95 follows the f2c convention of adding an underscore
to public names, or two underscores if the name contains an underscore.
The -fno-second-underscore and -fno-underscoring can be useful to
force g95 to produce names compatible with your C compiler.

Use the ''nm'' program to look at the o files being produce by both
compilers. G95 folds public names to lowercase as well, unless
-fupper-case is given, in which case everything will be upper case.
Module names are represented as module-name-MP-name.

After linking, there are two main cases: FORTRAN calling C subroutines
and C calling FORTRAN subroutines. For C calling FORTRAN subroutines,
the FORTRAN subroutines will often call FORTRAN library subroutines
that expect the heap to be initialized in some way.

To force a manual initialization from C, call g95_runtime_start() to
initialize the FORTRAN library and g95_runtime_stop() when done. The
prototype of g95_runtime-start() is:

void g95_runtime-start(int argc, char *argv[]);

The library has to be able to process command-line options. If this is
awkward to do and your program doesn''t have a need for command-line
arguments, pass argc=0 and argv=NULL.

On OSX/Tiger, include ''-lSystemStubs'', when using g95 to run the linker
and linking objects files compiled by gcc.

===> The ''nm'' program was not in the delivery.


Cottonwood写道:
Cottonwood wrote:

我想要从Fortran程序中调用C模块。无论我做了什么 -
链接器都找不到C模块。我知道领先的
下划线,甚至切换了。我抽象了所有可能的东西。当我用Fortran子程序替换C模块时,链接工作。这里的Fortran子程序取代了C模块进行测试:

I want to call a C module from a Fortran program. Whatever I tried -
the linker could not find the C module. I know about the leading
underscore and switched even that off. I abstracted everything
possible. When I replace the C module by a Fortran subroutine linking
works. Here the Fortran subroutine that replaced the C module for
testing:




这与C语言无关(这个主题是

新闻组)以及与你b
$ b使用的任何特定系统有关的一切。你应该找到一个处理那个

系统的新闻组。


-

"如果你想发布一个后续行动通过groups.google.com,不要使用

破损的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson

更多详细信息:< http://cfaj.freeshell.org/google/>

另见< http://www.safalra .com / special / googlegroupsreply />



This has nothing to do with the C language (the subject of this
newsgroup) and everything to do with whatever specific system you
are using. You should find a newsgroup that deals with that
system.

--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>


这篇关于无法从Fortran调用C模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆