473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Cannot call a C module from Fortran

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
21 2721
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
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
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
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
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
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
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
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
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
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
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
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
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
"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
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

"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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Anonymous | last post by:
I have a mixed-language pgm (Fortran and C++) which needs to pass a few hundred values from C++ to Fortran. One way to do this is to have a Fortran module and a C++ structure (or class) with an...
3
by: Dennis Kernighan | last post by:
On Microsoft's pages there is an example of making Fortran call from C++, see http://tinyurl.com/2692f . If I put C code in the test.c and Fortran code in FORSUBS.FOR and press F5 in C++, I get...
4
by: Cyde Weys | last post by:
I'm currently working on converting a simulator program from Visual Basic 6.0 to Visual C++ .NET. I've figured out most of the stuff, but there's still one thing I haven't gotten to and I've never...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
5
by: Amit | last post by:
I tried calling a subroutine in a fortran module from C ,but couldn't.I always get the error: undefined reference in the main.o file (main is in C calling the subroutine). for calling the...
8
by: Luna Moon | last post by:
Hi all, As a C/C++ programmer, there are a few reasons to use Fortran: (1) Fortran is very similar to Matlab and easy to port; (2) Fortran has support of complex numbers and vectorized numbers...
1
by: kw.housing | last post by:
I have all the library already in path: $ ls -l /opt/IBM/db2/lib64 | grep libdb2o -r-xr-xr-x 1 bin bin 7757295 Jul 12 2006 libdb2osse.a* -r--r--r-- 1 bin bin ...
0
by: John [H2O] | last post by:
Hello, I have a module created from a Fortran file to read in unformatted binary fortran output. It works fine for some datasets, but crashes with others. The strange thing is it will loop...
0
by: John [H2O] | last post by:
I can try, would you mind giving very brief instructions on how to 'run it under gdb'... thanks! I'll post results over at numpy-discussions. Robert Kern-2 wrote: -- View this message in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.