By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,408 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,408 IT Pros & Developers. It's quick & easy.

Cannot call a C module from Fortran

P: n/a
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?

May 23 '06 #1
Share this Question
Share on Google+
21 Replies


P: n/a
Cottonwood wrote:
I want to call a C module from a Fortran program. Whatever I tried -
the linker could not find the C module.


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

--
Ian Collins.
May 23 '06 #2

P: n/a
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.

May 23 '06 #3

P: n/a
Cottonwood wrote:

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:


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/>
May 23 '06 #4

P: n/a
On 2006-05-23, Cottonwood <go****@c-d-j.de> wrote:
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;
}

Some standard (not sure if it's a C one or an assembler one) dictates
that C funtions are preceded with '_' in the object code. I'm not sure
if Fortran has the same behavior.

So, int main(void) is assembled as a label '_main'.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
Get your game faces on, because this is not a game.
May 23 '06 #5

P: n/a
G95 follows the f2c convention of adding an underscore to public names,
or two underscores if the name contains an underscore.

I tried it also with underscore and with the option -fno-underscoring
al well. That surely is not the problem.

But thanks anyhow.

May 23 '06 #6

P: n/a
On 2006-05-23, Cottonwood <go****@c-d-j.de> wrote:
G95 follows the f2c convention of adding an underscore to public names,
or two underscores if the name contains an underscore.

I tried it also with underscore and with the option -fno-underscoring
al well. That surely is not the problem.

But thanks anyhow.


Please click 'More Options' and then 'Reply' when posting from Google;
the Reply button on the bottom of the page is broken, and you need to
post context.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
Get your game faces on, because this is not a game.
May 23 '06 #7

P: n/a
Andrew Poelstra wrote:
On 2006-05-23, Cottonwood <go****@c-d-j.de> wrote:
G95 follows the f2c convention of adding an underscore to public names,
or two underscores if the name contains an underscore.

I tried it also with underscore and with the option -fno-underscoring
al well. That surely is not the problem.

But thanks anyhow.


Please click 'More Options' and then 'Reply' when posting from Google;
the Reply button on the bottom of the page is broken, and you need to
post context.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
Get your game faces on, because this is not a game.


Sorry, I think it is working. You can see on the left that it was a
reply to your answer.
This kind of way multiplies the amount of text with no aditional
information.

May 23 '06 #8

P: n/a
Andrew Poelstra wrote:
On 2006-05-23, Cottonwood <go****@c-d-j.de> wrote:
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;
}

Some standard (not sure if it's a C one or an assembler one) dictates
that C funtions are preceded with '_' in the object code. I'm not sure
if Fortran has the same behavior.

So, int main(void) is assembled as a label '_main'.


Since when does assembler have a standard? I've used a few and *none* of
them was compatible with any other and none of the the documentation
referenced any standard, only the manufacturers specifications. None of
them mentioned C either. The C standard also fails to define linkage to
assembler.

This is all completely implementation specific so the OP needs to ask in
a group/mailing list/web forum that deals with s/he specific tool chain.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 23 '06 #9

P: n/a
Cottonwood wrote:
Andrew Poelstra wrote:
Please click 'More Options' and then 'Reply' when posting from Google;
the Reply button on the bottom of the page is broken, and you need to
post context.


Sorry, I think it is working. You can see on the left that it was a
reply to your answer.


"On the left" of /what/?
This kind of way multiplies the amount of text with no aditional
information.


It makes posts more stand-alone. Since they may be delivered out
of order, and read at separate times, this is a good thing.

One should also prune.

--
Chris "gardening hedgehog" Dollin
"One side of what? The other side of what?" said Alice.

May 23 '06 #10

P: n/a
Cottonwood said:
You can see on the left that it was a
reply to your answer.


What I see on the left is a list of newsgroups, each giving the total number
of articles, and the number of unread articles, in that group. How does
this information show me that your reply was a reply to Andrew Poelstra?

Don't make the mistake of thinking all newsreaders work in the same way that
your newsreader works.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 23 '06 #11

P: n/a
Cottonwood wrote:
Andrew Poelstra wrote:
On 2006-05-23, Cottonwood <go****@c-d-j.de> wrote:
G95 follows the f2c convention of adding an underscore to public names,
or two underscores if the name contains an underscore.

I tried it also with underscore and with the option -fno-underscoring
al well. That surely is not the problem.

But thanks anyhow.
Please click 'More Options' and then 'Reply' when posting from Google;
the Reply button on the bottom of the page is broken, and you need to
post context.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
Get your game faces on, because this is not a game.


Please snip peoples sigs (the bit normally after the "-- ") and anything
else you are not replying to.
Sorry, I think it is working. You can see on the left that it was a
reply to your answer.
How can I see that when there is nothing on the left? Google is *not*
Usenet, just an atrocious interface to it.
This kind of way multiplies the amount of text with no aditional
information.


There is no guarantee that people have seen the message you are replying
to before they see your message. There is no guarantee they will ever
see it. Even if they have seen it there is no guarantee they remember
it, still have it on their server or cached in there client, or normally
have there client set to display already read messages.

Usenet and the practice of quoting so that each message stands on its
own has existed for a lot longer than Google has existed.

I suggest you read http://clc-wiki.net/wiki/Intro_to_clc and in
particular the section on that page about Google and the pages it links to.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 23 '06 #12

P: n/a
On 2006-05-23, Richard Heathfield <in*****@invalid.invalid> wrote:
Cottonwood said:
You can see on the left that it was a
reply to your answer.
What I see on the left is a list of newsgroups, each giving the total number
of articles, and the number of unread articles, in that group. How does
this information show me that your reply was a reply to Andrew Poelstra?


I see you use knode.

I'm not saying he shouldn't have quoted, but it's not like the
information that it was a reply wasn't there, and claiming it isn't
leads to reduced credibility

And in any newsreader worth anything you can look at the references
header and find the previous message by message-id.
Don't make the mistake of thinking all newsreaders work in the same
way that your newsreader works.


If he'd said to look above the article display rather than to its left,
what would your response have been?
May 23 '06 #13

P: n/a
Jordan Abel said:
On 2006-05-23, Richard Heathfield <in*****@invalid.invalid> wrote:
Don't make the mistake of thinking all newsreaders work in the same
way that your newsreader works.


If he'd said to look above the article display rather than to its left,
what would your response have been?


I'd have said "looking above the article to the article above it, I see an
article by pete in a thread entitled 'Find the size of a datatype'. Don't
make the mistake of thinking all newsreaders work in the same way that your
newsreader works." :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 23 '06 #14

P: n/a
"Cottonwood" <go****@c-d-j.de> writes:
Andrew Poelstra wrote:
On 2006-05-23, Cottonwood <go****@c-d-j.de> wrote:
> G95 follows the f2c convention of adding an underscore to public names,
> or two underscores if the name contains an underscore.
>
> I tried it also with underscore and with the option -fno-underscoring
> al well. That surely is not the problem.
>
> But thanks anyhow.
>
Please click 'More Options' and then 'Reply' when posting from Google;
the Reply button on the bottom of the page is broken, and you need to
post context.

[...]
Sorry, I think it is working. You can see on the left that it was a
reply to your answer.
This kind of way multiplies the amount of text with no aditional
information.


No, most of can't see anything on the left, because we're not using
groups.google.com. Google provides an interface to Usenet, a
discussion system that has existed many years longer than Google has,
and in some ways Google's interface is inferior.

Read <http://cfaj.freeshell.org/google/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 23 '06 #15

P: n/a
Cottonwood wrote:
Andrew Poelstra wrote:
.... snip ...
Please click 'More Options' and then 'Reply' when posting from
Google; the Reply button on the bottom of the page is broken,
and you need to post context.


Sorry, I think it is working. You can see on the left that it
was a reply to your answer. This kind of way multiplies the
amount of text with no aditional information.


Now that you have learned to quote, the next step is to learn to
snip. You simply remove the portion of the quoted material that is
not relevant to your answer. The signature is almost never
relevant. The result is a coherent whole. You will shortly become
a valued netizen, as you obviously have the capacity to learn.

By the way, the view you have bears no resemblance to the view most
others have. "see on the left" just doesn't exist at most
destinations.

--
"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/>
May 23 '06 #16

P: n/a

"Cottonwood" <go****@c-d-j.de> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
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?


Fortran has call by value, so all arguments are passed by address.

Fortran

call cfunc(x, y)

C
cfunc_(int *x, int *y)
{
/* function here */
}

To call Fortran from C

C
ffunc_(&x, &y)

Fortan
subroutine ffunc(x, y)
integer x, y
c----- Function here
Strings are a little tricky. Fortan uses length-delimited, not necessarily
NUL-terminated strings. The length is passed as an invisible last parameter.
This is a C int, not an int * as a normal integer parameter.

Fortan

cstrfn("My name is Fred")

C
void cstrfn_(char *str, int len)
{
/* don't rely on string being NUL-terminated. len gives its
length. */
}

Notes this applies to Fortan77. The rules are probably similar for Fortran
90, but I don't use it.

--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
May 23 '06 #17

P: n/a
On 2006-05-23, Cottonwood <go****@c-d-j.de> wrote:
Andrew Poelstra wrote:
Please click 'More Options' and then 'Reply' when posting from Google;
the Reply button on the bottom of the page is broken, and you need to
post context.


Sorry, I think it is working. You can see on the left that it was a
reply to your answer.
This kind of way multiplies the amount of text with no aditional
information.


Please don't quote signatures.

On the left of this message I see:
Computer, apoelstra's Home, Trash, Terminal, OpenOffice.org Math

Each of those has an icon above it, but that still doesn't give me any
information about what you didn't quote. Also, quite frequently I'm
not logged into X, and so "to the left" of the message would be off
the screen and probably somewhere on my couch. As Richard Heathfield
said elsethread, don't assume all newsreaders behave the way that yours
does.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
It's just like stealing teeth from a baby.
May 23 '06 #18

P: n/a
Malcolm wrote:
"Cottonwood" <go****@c-d-j.de> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
I want to call a C module from a Fortran program. Whatever I tried -
<snip>
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?
Fortran has call by value, so all arguments are passed by address.


<snip stuff about Fortran vs C calling conventions.
Notes this applies to Fortan77. The rules are probably similar for Fortran
90, but I don't use it.


None of this is topical here and in any case it does not address the OPs
problem, which was not that the parameters were incorrect but that the
program was not linking.

If you actually know about the OPs problem then please take the
discussion to a group where it is topical or to email.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 23 '06 #19

P: n/a
On 2006-05-23, Flash Gordon <sp**@flash-gordon.me.uk> wrote:
Andrew Poelstra wrote:
Some standard (not sure if it's a C one or an assembler one) dictates
that C funtions are preceded with '_' in the object code. I'm not sure
if Fortran has the same behavior.

So, int main(void) is assembled as a label '_main'.


Since when does assembler have a standard? I've used a few and *none* of
them was compatible with any other and none of the the documentation
referenced any standard, only the manufacturers specifications. None of
them mentioned C either. The C standard also fails to define linkage to
assembler.


I meant (an assembler) standard, not an (assembler standard). That is, the
standard of some assembler, not some standard of an imaginary generic
assembler.

In "PC Assembly Language" by Dr. Paul A. Carter, it is clearly stated that
C functions have an _ before them in most typical cases (a Win/Lintel),
with the notable exception of ELF-style linking.

It is true that this is OT, but I think it is a helpful answer, and one
that would be tricky to find elsewhere.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
It's just like stealing teeth from a baby.
May 23 '06 #20

P: n/a
Cottonwood wrote:
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?
robert.corb...@sun.com wrote: I just noticed that you crossposted to gnu.g++,help.
Is there any chance you compiled your C code with
g++? If so, you might need to use extern "C". Bob Corbett


Thank you so much. As a C beginner I wasn't able to see that. Now it
works with my changed C procedure:

extern "C" void qqcprint()
{
return;
}

And many thanks to all the other guys. Meanwhile I understood why I
shouldn't use the answer button below the posts.

May 24 '06 #21

P: n/a
"Cottonwood" <go****@c-d-j.de> writes:
[...]
robert.corb...@sun.com wrote:
I just noticed that you crossposted to gnu.g++,help.
Is there any chance you compiled your C code with
g++? If so, you might need to use extern "C".

Bob Corbett


Thank you so much. As a C beginner I wasn't able to see that. Now it
works with my changed C procedure:

extern "C" void qqcprint()
{
return;
}


Or, if you're writing C code, you can just compile it with a C
compiler, and you won't need the extern "C". <OT>Use gcc, not
g++.</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 24 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.