link fortran code with external libraries

link fortran code with external libraries

Dear All,

I need  link a fortran code with four external libraries (lib1.lib, lib2.lib, lib3.lib lib4.lib). I have no problem linking these libraries with a c code (main.c) by using the following command: 

icl main.c lib1.lib lib2.lib lib3.lib lib4.lib

An executable named main.exe will be generated. However, when I convert main.c to two equivalent files main.f and s.c. I used the following command to generate s.obj

icl /c s.c

I use the following for linking but did not work. 

ifort s.obj main.f lib1.lib lib2.lib lib3.lib lib4.lib

It complains the the function calls in main.f (which should be in the four libraries) are not found. What command should I use for linking? I am sorry that I can not provide more details about files and libraries as these are provided to me by another company. I hope these question can be answered without such details. 

Thanks a lot!

 

 

 

 

13 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

I assume the code in your libraries was written in C.

In your Fortran program, did you declare the interfaces to these routines using BIND (C)?

The default Fortran external-naming convention is different from the C naming convention, which means there would be a name mismatch.

                     --Lorri

Lorri,

Thanks a lot for your reply. Those are static c libraries. The compilers (ifort, icl) are version 12.1.5.344. I have no problem to link them using Absoft. The naming issues have been taken care of using the subroutines in s.c. I thought I could just missed some other libraries needed to be linked when use ifort to link c object and libraries. Thanks again.

 

.

 

Lorri,

Thanks a lot for your reply. Those are static c libraries. The compilers (ifort, icl) are version 12.1.5.344. I have no problem to link them using Absoft. The naming issues have been taken care of using the subroutines in s.c. I thought I could just missed some other libraries needed to be linked when use ifort to link c object and libraries. Thanks again.

 

.

 

Please show the actual linker error messages, how you declare the routines in C and how the routines are declared in Fortran. Also show us the compile options used for both C and Fortran.

Steve

Dear Steve,

Thanks a lot for your attention. The compiler options I used were given in the original post. The libraries can be linked with a main function written in c using the following command:

icl main.c lib1.lib lib2.lib lib3.lib lib4.lib

To link the mixed fortran and c I used the following command

icl /c s.c

ifort s.obj main.f lib1.lib lib2.lib lib3.lib lib4.lib

where main.f is equivalent to main.c with s.c as the glue between fortran and c. I have link error 2019. Basically, the function calls in main.f are unresolved.

 

 

I might want to add that main.f and s.c can be compiled and linked with the libraries using absoft fortran compiler without any problem. 

Please show the error messages and the declarations of the functions in both Fortran and C.

Steve

Dear Steve,

I am checking with the legal department of the software provider whether I am allowed to share these files. The error message are as follows. Those unresolved external symbols are functional calls and the corresponding functions are defined in the libraries.

main.obj : error LNK2019: unresolved external symbol NETLIBTOCINIT referenced in
 function MAIN__
main.obj : error LNK2019: unresolved external symbol NETLIBTOCCHECKOUTTOKENS ref
erenced in function MAIN__
main.obj : error LNK2019: unresolved external symbol NETLIBTOCGETERRORCODE refer
enced in function MAIN__
main.obj : error LNK2019: unresolved external symbol NETLIBTOCGETERRORSTRING ref
erenced in function MAIN__
main.obj : error LNK2019: unresolved external symbol NETLIBTOCGETWARNING referen
ced in function MAIN__
main.obj : error LNK2019: unresolved external symbol NETLIBTOCCHECK referenced i
n function MAIN__
main.obj : error LNK2019: unresolved external symbol NETLIBTOCCHECKIN referenced
 in function MAIN__
main.obj : error LNK2019: unresolved external symbol NETLIBTOCCLEANUP referenced
 in function MAIN__

 

I don't need to see the whole file - just the statement that declares, for example, NETLIBTOCINIT. If this routine is in the C code and has a name that is not all uppercase, then what Lorri said above is correct - you need to make adjustments for C being case-sensitive and Fortran not. Do you have INTERFACE blocks for NETLIBTOCINT and its friends or do you just call them? If the latter, then the C code needs to change to have the routine names in all upper case (on Windows - this would be different on Linux or OS X.)

The best way to solve this is to write INTERFACE blocks for the C routines and add BIND(C,NAME="MixedCaseRoutineName") to the SUBROUTINE or FUNCTION line, then have these interface blocks visible in the program unit where you call the functions

Steve

Steve,

I believe this reply is very helpful. I am feeding this back to the software company, hoping it will be resolved very soon. Thanks a lot!

Lori and Steve,

I changed all the function names to be capitals and delete all the trailing under scores in the interface written in c (s.c). Now it works. Thanks for your help. 

Very good. Your C code was written assuming Linux conventions. The C interoperability features of Fortran 2003 are a better way of dealing with such differences, but what you did is simpler and will work for now. It won't be portable to other environments, however.

Steve

Login to leave a comment.