Cannot call a C module from Fortran

C

Cottonwood

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?
 
I

Ian Collins

Cottonwood said:
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.
 
C

Cottonwood

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.
 
C

CBFalconer

Cottonwood said:
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/>
 
A

Andrew Poelstra

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'.
 
C

Cottonwood

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.
 
A

Andrew Poelstra

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.
 
C

Cottonwood

Andrew said:
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.
 
F

Flash Gordon

Andrew said:
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.
 
C

Chris Dollin

Cottonwood said:
Andrew Poelstra wrote:

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.
 
R

Richard Heathfield

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.
 
F

Flash Gordon

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.
 
J

Jordan Abel

Cottonwood said:


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?
 
R

Richard Heathfield

Jordan Abel said:
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." :)
 
K

Keith Thompson

Cottonwood said:
Andrew said:
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/>.
 
M

Malcolm

Cottonwood said:
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.
 
C

CBFalconer

Cottonwood said:
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/>
 
F

Flash Gordon

Malcolm said:
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.
 
A

Andrew Poelstra

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.
 
A

Andrew Poelstra

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top