3
 // f.for is a Fortran source file
 // c.c is a C source file   
 # gfortran -c f.for            // produce f.o
 # gcc -c c.c                   // produce c.o

since f.for calls some functions from c.c. How can I produce a third object file fc.o which will be used later to generate an executable with another C source code. explain:

 # gcc/gfortran? (or other) -?? f.o c.o -o fc.o
 # gcc -o executable source.c fc.o  

thank you

What I have done but not work:

  # ar rvs fc.a f.o c.o  
  # gcc -o executable source.c fc.a  

But it shows me error like : undefined reference to 'function_name_X'. function_name_X is in f.for

5
  • ar rvs fc.a f.o c.o will get you a static library (fc.a), which you can use as if it were an object file made by merging f.o and c.o. Commented Oct 22, 2015 at 21:03
  • PSkocik: Yes, I'll try again this solution, perhaps it is the unique solution Commented Oct 22, 2015 at 21:08
  • Have you tried ld -r f.o c.o -o fc.o, then? (Dark Falcon's link) Commented Oct 22, 2015 at 21:08
  • You may be asking the wrong question. If you're producing all these object files in the same build process, then you don't need to make a combined object file. You can just link all the needed object files together to produce the final executable. Commented Oct 22, 2015 at 21:10
  • John Bollinger -> I have this this idea in my mind: if a.o depends on b.o, c.c depends on a.o , so # gcc -o exe c.c a.o b.o will work? Commented Oct 22, 2015 at 21:13

1 Answer 1

2

Credit: PSkocik, John Bollinger
My comment :

I have this this idea in my mind: if a.o depends on b.o, c.c depends on a.o , so # gcc -o exe c.c a.o b.o will work?

should works. In fact, I listed all functions in f.o :

 # nm f.o  

I find that Fortran add underscore '_' at the end of every function name, since I'm calling these functions from C without adding underscore at end, this error happened. According to this question: I added -fno-underscoring to gfortran compiler when generating f.o

# gfortran -c f.for -fno-underscoring
# nm f.o

at this time, there is no underscore at the end.
Response to the question:

# gcc -o executable source.c f.o c.o  // not need fc.o  
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.