Cannot call a C module from Fortran

C

Cottonwood

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?

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

Keith Thompson

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

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,255
Latest member
TopCryptoTwitterChannels

Latest Threads

Top