Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

call c lib from fortran

kidariyoon
Beginner
3,511 Views
i am trying to call c lib from fortran.

here is how i built a c lib using visual studio 2005.

File>NewProject>VisualC++>Win32>Win32 Console Application

under Win32 Application Wizard
ApplicationType:StaticLibrary
no precompiled header

I added a c file in "SourceFiles" and a h file in "Header File"
and compile to get a lib file.

Then for fortran part under visual studio 2005 again, I put the directory and lib file name info to "Linker>Input>AdditionalDependencies" under project properties.

then when i complie, i got

error_during_IPO_compilation: problem during multi-file optimization compilation (code 3)

was the procedure for c lib wrong?

or, calling c lib from fortran was not done right?

Please teach me on how to resolve this.

Many thanks well in advance.


S.Yoon

0 Kudos
20 Replies
Steven_L_Intel1
Employee
3,490 Views
In most cases when you get that "IPO" error, there is another message that precedes it. Please show ALL the error messages or, better, attach the buildlog.htm from the failed build. (Do a Rebuild to show everything.)
0 Kudos
kidariyoon
Beginner
3,490 Views
a light in the end. thanks for your reply.

the following is the text i copied from BuildLog.htm

--------

Compiling with Intel Fortran Compiler 10.1.011 [IA-32]...

ifort /nologo /Zi /Od /gen-interfaces /warn:interfaces /module:"Debug\" /object:"Debug\" /traceback /check:bounds /libs:qwin /dbglibs /c /extfor:f /Qvc8 /Qlocation,link,"C:\Program Files\Microsoft Visual Studio 8\VC\bin" "C:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\hbcode1.f"

ifort /nologo /Zi /Od /gen-interfaces /warn:interfaces /module:"Debug\" /object:"Debug\" /traceback /check:bounds /libs:qwin /dbglibs /c /extfor:f /Qvc8 /Qlocation,link,"C:\Program Files\Microsoft Visual Studio 8\VC\bin" "C:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\f77_main.f"

Linking...

Link /OUT:"Debug\QWin1.exe" /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:"C:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin1\QWin1\debug\qwin1.exe.intermediate.manifest" /DEBUG /PDB:"C:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin1\QWin1\debug\qwin1.pdb" /SUBSYSTEM:WINDOWS C:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\lib_c\release\lib_c.lib "Debug\hbcode1.obj" "Debug\f77_main.obj"

backend signals

Severe: **Internal compiler error: internal abort** Please report this error along with the circumstances in which it occurred in a Software Problem Report. Note: File and line given may not be explicit cause of this error.

Link: error error_during_IPO_compilation: problem during multi-file optimization compilation (code 3)

Link: error error_during_IPO_compilation: problem during multi-file optimization compilation (code 3)

QWin1 - 2 error(s), 0 warning(s)

0 Kudos
Steven_L_Intel1
Employee
3,490 Views
The "internal compiler error" is the key.

I see you are using a rather old compiler. Can you try this with 11.1.065? If it fails there, please attach a ZIP of the sources needed to reproduce this error.

You might try, as a possible workaround, turning off /warn:interface and /gen-interface. If I recall correctly, in 10.1 one of those was under External Procedures and the other under Diagnostics.
0 Kudos
kidariyoon
Beginner
3,490 Views
Dear Steve,

Unfortunately turning-off those two didnt work for my case.

i wonder calling a c lib from fortran is this difficult ori amdealing withabad c fileto be called from fortran?

thanks again for your help.

0 Kudos
Steven_L_Intel1
Employee
3,490 Views
You have encountered a compiler bug. I don't know which one, but I know that sometimes disabling interface checking is a workaround. Clearly it isn't for your case. It has nothing to do with C - the issue is entirely related to the Fortran code.

Can you try the 11.1 .065 compiler? We've fixed many bugs since 10.1.011.
0 Kudos
kidariyoon
Beginner
3,490 Views
Thanks for your patience ...

finally i got 11.0.061. (thats the version i have in cd)

i try it again and get a different warning.
---------------------------------------------------------------------

Linking...

Link /OUT:&quotDebug\QWin2.exe" /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:&quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin2\QWin2\debug\qwin2.exe.intermediate.manifest" /DEBUG /PDB:&quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin2\QWin2\debug\qwin2.pdb" /SUBSYSTEM:WINDOWS /IMPLIB:&quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin2\QWin2\debug\qwin2.lib" C:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\lib_c\release\lib_c.lib &quotDebug\f77_main.obj" &quotDebug\hbcode1.obj"

ipo: warning #11009: file format not recognized for C:\Documents
ipo: warning #11009: file format not recognized for and
ipo: warning #11009: file format not recognized for Settings\syoon\My
ipo: warning #11009: file format not recognized for Documents\My
Link: executing 'link'
LINK : fatal error LNK1181: cannot open input file 'C:\Documents.obj'
QWin2 - 1 error(s), 4 warning(s)
----------------------------------

then i moved the lib directory right under c:\

then i get the following

Compiling with Intel Fortran 11.0.061 [IA-32]...

ifort /nologo /debug:full /Od /I&quotC:\release" /gen-interfaces /warn:interfaces /module:&quotDebug\" /object:&quotDebug\" /traceback /check:bounds /libs:qwin /dbglibs /c /extfor:f /Qvc8 /Qlocation,link,&quotC:\Program Files\Microsoft Visual Studio 8\VC\bin" &quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\f77_main.f"

ifort /nologo /debug:full /Od /I&quotC:\release" /gen-interfaces /warn:interfaces /module:&quotDebug\" /object:&quotDebug\" /traceback /check:bounds /libs:qwin /dbglibs /c /extfor:f /Qvc8 /Qlocation,link,&quotC:\Program Files\Microsoft Visual Studio 8\VC\bin" &quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\hbcode1.f"

Linking...

Link /OUT:&quotDebug\QWin2.exe" /INCREMENTAL:NO /NOLOGO /LIBPATH:&quotC:\release" /MANIFEST /MANIFESTFILE:&quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin2\QWin2\debug\qwin2.exe.intermediate.manifest" /DEBUG /PDB:&quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin2\QWin2\debug\qwin2.pdb" /SUBSYSTEM:WINDOWS /IMPLIB:&quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin2\QWin2\debug\qwin2.lib" C:\release\lib_c.lib &quotDebug\f77_main.obj" &quotDebug\hbcode1.obj"

Link: executing 'link'

f77_main.obj : error LNK2019: unresolved external symbol _C_BRIDGE_PDGSSV referenced in function _MAIN__

Debug\QWin2.exe : fatal error LNK1120: 1 unresolved externals

QWin2 - 2 error(s), 0 warning(s)


---------------------------------------

do i need to try the exact version you mensioned before?

or i am assigning wrong directories for fortran compilation?

Thanks again.

0 Kudos
Steven_L_Intel1
Employee
3,490 Views
You should not have to move LIB folders. The first error appears to be an issue regarding quoting of file paths. Did you do this from within Visual Studio?

The second problem is that there is a reference to a routine named C_BRIDGE_PDGSSV that is not found. If this is in your C code, note that it must be spelled exactly like that, in upper case, unless you have added things to the Fortran code to change the case (such as an ATTRIBUTES ALIAS directive or BIND(C).)
0 Kudos
kidariyoon
Beginner
3,490 Views
well. yes. i did it within Visual Studio.

for fortran project,

Project>Properties>Linker>Input>Additional Dependencicies>C:\Documents and Settings\....\lib_c.lib

then i got the first error. did i put the path in the wrong place?
0 Kudos
Steven_L_Intel1
Employee
3,490 Views
Well, no, but I think you may have to enclose the full path in quotes because it has spaces.

Another option is to add the library to the project as a "source file".
0 Kudos
kidariyoon
Beginner
3,490 Views
Once I enclose whole path and lib name, only complaint I got was that

"unresolved external symbol _C_BRIDGE_PDGSSV referenced in function _MAIN__"

but,

in c, its written with lower letters and
...
c_bridge_pdgssv_(int *nprocs,......,int *info)

while in fortran, its again written in lower letters

call c_bridge_pdgssv(nprocs,........, info)

do i have to turn on/off some option in the project properties ?

---------------

I found this thread"Fortran Calling C Library" in this site,

and portion of the responce in the thread reads like

"Intel Fortran does not use _stdcall anymore (as CVF did), but _cdecl"

I dont know if its related to my case.

----------------

Am I going to add ##.c intoFortran Source files?

Since I got a "unresolved ..." error again when I put ##.c file into Soure Files.



0 Kudos
Steven_L_Intel1
Employee
3,490 Views
No, the calling convention is not related. Do not use an option, do add this in the Fortran code that calls the routine:

!DEC$ ATTRIBUTES DECORATE, ALIAS:"c_bridge_pdgssv" :: c_bridge_pdgssv
0 Kudos
kidariyoon
Beginner
3,490 Views
Dear Steve,
Really appreciate all your help and much of your patience.
---
with adding the line into my fortran file, it now looks like:
----
program f77_main
integer n, nnz, nrhs, ldb, info
integer nprocs
!DEC$ ATTRIBUTES DECORATE, ALIAS:"c_bridge_pdgssv" :: c_bridge_pdgssv
call c_bridge_pdgssv(nprocs, n, nnz, nrhs, values, rowind, colptr, b, ldb, info)
stop
end
----
then I got the error something like this.
Deleting intermediate files and output files for project 'QWin3', configuration 'Debug|Win32'.
Compiling with Intel Fortran 11.0.061 [IA-32]...
ifort /nologo /debug:full /Od /gen-interfaces /warn:interfaces /module:&quotDebug\" /object:&quotDebug\" /traceback /check:bounds /libs:qwin /dbglibs /c /extfor:f /Qvc8 /Qlocation,link,&quotC:\Program Files\Microsoft Visual Studio 8\VC\bin" &quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\hbcode1.f"
ifort /nologo /debug:full /Od /gen-interfaces /warn:interfaces /module:&quotDebug\" /object:&quotDebug\" /traceback /check:bounds /libs:qwin /dbglibs /c /extfor:f /Qvc8 /Qlocation,link,&quotC:\Program Files\Microsoft Visual Studio 8\VC\bin" &quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\f77_main.f"
C:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\f77_main.f(9): error #7794: Only a function or subroutine subprogram may have the !DEC$ ATTRIBUTES directive DECORATE specifier.   [C_BRIDGE_PDG]
      !DEC$ ATTRIBUTES DECORATE, ALIAS:&quotc_bridge_pdgssv" :: c_bridge_pdgssv     
------------------------------------------------------------^
compilation aborted for C:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\f77_main.f (code 1)


QWin3 - 2 error(s), 0 warning(s)
i dont know how tointerpretthe error. do i have to make my fortran file to a subroutine ? (it dint work either)
so i tried the following:
program f77_main
INTERFACE
SUBROUTINE c_bridge_pdgssv(nprocs, n, nnz, nrhs, values,
$ rowind, colptr, b, ldb, info)
!DEC$ ATTRIBUTES C, DECORATE, ALIAS: "c_bridge_pdgssv"
& :: c_bridge_pdgssv
integer maxn, maxnz
parameter ( maxn = 10000, maxnz = 100000 )
integer rowind(maxnz), colptr(maxn)
real*8 values(maxnz), b(maxn)
integer n, nnz, nrhs, ldb, info
integer nprocs
END SUBROUTINE
END INTERFACE
call c_bridge_pdgssv(nprocs, n, nnz, nrhs, values, rowind,
$ colptr, b, ldb, info)
stop
end
errors are the same.

something like.

.....
f77_main.obj : error LNK2019: unresolved external symbol _c_bridge_pdgssv referenced in function _MAIN__
Debug\QWin3.exe : fatal error LNK1120: 1 unresolved externals
...
S. Yoon
0 Kudos
Steven_L_Intel1
Employee
3,490 Views
You have fixed-form source and went past column 72. Split it up like this:

!DEC$ ATTRIBUTES DECORATE :: c_bridge_pdgssv
!DEC$ ATTRIBUTES ALIAS:"c_bridge_pdgssv" :: c_bridge_pdgssv
0 Kudos
kidariyoon
Beginner
3,490 Views
now it looks like
-----
!23456789012345678901234567890123456789012345678901234567890123456789012
program f77_main
integer maxn, maxnz
parameter ( maxn = 10000, maxnz = 100000 )
integer rowind(maxnz), colptr(maxn)
real*8 values(maxnz), b(maxn)
integer n, nnz, nrhs, ldb, info
integer nprocs
INTERFACE
SUBROUTINE c_bridge_pdgssv(nprocs, n, nnz, nrhs, values,
$ rowind, colptr, b, ldb, info)
!DEC$ ATTRIBUTES DECORATE :: c_bridge_pdgssv
!DEC$ ATTRIBUTES ALIAS:"c_bridge_pdgssv" :: c_bridge_pdgssv
integer maxn, maxnz
parameter ( maxn = 10000, maxnz = 100000 )
integer rowind(maxnz), colptr(maxn)
real*8 values(maxnz), b(maxn)
integer n, nnz, nrhs, ldb, info
integer nprocs
END SUBROUTINE
END INTERFACE
call c_bridge_pdgssv(nprocs, n, nnz, nrhs, values, rowind,
$ colptr, b, ldb, info)
stop
end
while the c function looks like:
....
void
c_bridge_pdgssv_(int *nprocs, int *n, int *nnz, int *nrhs, double *values,
int *rowind, int *colptr, double *b, int *ldb, int *info)
.....
well. the error is by now very familiar....
-----
Deleting intermediate files and output files for project 'QWin3', configuration 'Debug|Win32'.
Compiling with Intel Fortran 11.0.061 [IA-32]...
ifort /nologo /debug:full /Od /gen-interfaces /warn:interfaces /module:&quotDebug\" /object:&quotDebug\" /traceback /check:bounds /libs:qwin /dbglibs /c /extfor:f /Qvc8 /Qlocation,link,&quotC:\Program Files\Microsoft Visual Studio 8\VC\bin" &quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\hbcode1.f"
ifort /nologo /debug:full /Od /gen-interfaces /warn:interfaces /module:&quotDebug\" /object:&quotDebug\" /traceback /check:bounds /libs:qwin /dbglibs /c /extfor:f /Qvc8 /Qlocation,link,&quotC:\Program Files\Microsoft Visual Studio 8\VC\bin" &quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\f77_main.f"
Linking...
Link /OUT:&quotDebug\QWin3.exe" /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:&quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin3\QWin3\debug\qwin3.exe.intermediate.manifest" /DEBUG /PDB:&quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin3\QWin3\debug\qwin3.pdb" /SUBSYSTEM:WINDOWS /IMPLIB:&quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin3\QWin3\debug\qwin3.lib" &quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\lib_c\release\lib_c.lib" &quotDebug\hbcode1.obj" &quotDebug\f77_main.obj"
Link: executing 'link'
f77_main.obj : error LNK2019: unresolved external symbol _c_bridge_pdgssv referenced in function _MAIN__
Debug\QWin3.exe : fatal error LNK1120: 1 unresolved externals


QWin3 - 2 error(s), 0 warning(s)
----

am i not properly defining some options or path that i am supposed to set in properties?
i described how i generated c lib. was it correct?
S.Yoon
0 Kudos
Steven_L_Intel1
Employee
3,490 Views
Please show the complete C declaration. Also, is the file .cpp or .c? Do you have __stdcall in there? "looks like" is not the same as "is".
0 Kudos
kidariyoon
Beginner
3,490 Views
Dear Steve,
Sorry, the file is quite long so i put only part of it.
its 'c_bridge_pdgssv.c' file;
#include
#include
#include "pdsp_defs.h"
#include "slu_mt_util.h"
#include "slu_mt_Cnames.h"
void
c_bridge_pdgssv_(int *nprocs, int *n, int *nnz, int *nrhs, double *values,
int *rowind, int *colptr, double *b, int *ldb, int *info)
{
... (do you have to see inside the function?)
}
i could not find any 'stdcall' in the c file.
i hope this will help.
S. Yoon
0 Kudos
Steven_L_Intel1
Employee
3,490 Views
Thanks - that's as much as I need. You need "extern" in fromt of "void".
0 Kudos
kidariyoon
Beginner
3,490 Views
adding extern didn't work.
one question.
in the thread named 'Fortran calling C library,'
the questioner defined a c file as;

...
int ADD(int* a, int* b, int* answer) { ...}
...

and the function is called from fortran

...
CALL ADD(A, B, AN)
...



in my case after adding extern,

...
extern void
c_bridge_pdgssv_(int*nprocs,...) {...}
and its called in the fortran
c_bridge_pdgssv(nprocs, ...)
do you see a little difference ? so i modified the function in c as
c_bridge_pdgssv(int*nprocs,...) {...}
and get lib file. then when i compile in fortran, i get whole new errors;
is this relevant?

0 Kudos
kidariyoon
Beginner
3,490 Views
so i matched two functions in c and fortran.
c:

extern void
c_bridge_pdgssv(int *nprocs, int *n, int *nnz, int *nrhs, double *values,
int *rowind, int *colptr, double *b, int *ldb, int *info)
fortran:
subroutine c_bridge_pdgssv(nprocs, n, nnz, nrhs, values,
$ rowind, colptr, b, ldb, info)
then the following errors show up.
(during last several days, i learn that mixing fortran and c may cause problems,
specially for libraries regarding I/O)
Then, can I turn on/off some of those?
Compiling with Intel Fortran 11.0.061 [IA-32]...
ifort /nologo /debug:full /Od /gen-interfaces /warn:interfaces /module:&quotDebug\" /object:&quotDebug\" /traceback /check:bounds /libs:qwin /dbglibs /c /extfor:f /Qvc8 /Qlocation,link,&quotC:\Program Files\Microsoft Visual Studio 8\VC\bin" &quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\f77_main.f"
Linking...

Link /OUT:&quotDebug\QWin4.exe" /INCREMENTAL:NO /NOLOGO /LIBPATH:&quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\lib_c\release" /MANIFEST /MANIFESTFILE:&quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin4\QWin4\debug\qwin4.exe.intermediate.manifest" /DEBUG /PDB:&quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin4\QWin4\debug\qwin4.pdb" /SUBSYSTEM:WINDOWS /IMPLIB:&quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\QWin4\QWin4\debug\qwin4.lib" &quotC:\Documents and Settings\syoon\My Documents\My Fortran\EXAMPLE\lib_c\release\lib_c.lib" &quotDebug\hbcode1.obj" &quotDebug\f77_main.obj"

Link: executing 'link'

lib_c.lib(c_bridge_pdgssv.obj) : MSIL .netmodule or module compiled with /GL found; restarting link with /LTCG; add /LTCG to the link command line to improve linker performance

MSVCRT.lib(MSVCR80.dll) : error LNK2005: _printf already defined in LIBCMTD.lib(printf.obj)

MSVCRT.lib(ti_inst.obj) : error LNK2005: &quotprivate: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)

MSVCRT.lib(ti_inst.obj) : error LNK2005: &quotprivate: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)

LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library

lib_c.lib(c_bridge_pdgssv.obj) : error LNK2001: unresolved external symbol _intMalloc

lib_c.lib(c_bridge_pdgssv.obj) : error LNK2001: unresolved external symbol _get_perm_c

lib_c.lib(c_bridge_pdgssv.obj) : error LNK2001: unresolved external symbol _Destroy_SuperMatrix_Store

lib_c.lib(c_bridge_pdgssv.obj) : error LNK2001: unresolved external symbol _superlu_dQuerySpace

lib_c.lib(c_bridge_pdgssv.obj) : error LNK2001: unresolved external symbol _Destroy_CompCol_Matrix

lib_c.lib(c_bridge_pdgssv.obj) : error LNK2001: unresolved external symbol _sp_ienv

lib_c.lib(c_bridge_pdgssv.obj) : error LNK2001: unresolved external symbol _Destroy_SuperNode_Matrix

lib_c.lib(c_bridge_pdgssv.obj) : error LNK2001: unresolved external symbol _dCreate_CompCol_Matrix

lib_c.lib(c_bridge_pdgssv.obj) : error LNK2001: unresolved external symbol _pdgssv

lib_c.lib(c_bridge_pdgssv.obj) : error LNK2001: unresolved external symbol _superlu_free

lib_c.lib(c_bridge_pdgssv.obj) : error LNK2001: unresolved external symbol _dCreate_Dense_Matrix

Debug\QWin4.exe : fatal error LNK1120: 11 unresolved externals


QWin4 - 15 error(s), 1 warning(s)

0 Kudos
Steven_L_Intel1
Employee
3,363 Views
You can mix Fortran and C, but you have to do it right.

First, you need to make sure that the "runtime library" settings in the Fortran and C projects are the same. Since you are using QuickWin in Fortran, there is no "QuickWin" setting in C, but the correct option there is
"Multithreaded (/MT)". Find that under "Code Generation". That will tyake care of the 2005 and 4098 messages.

The others are link errors internal to your C libray, where c_bridge_pdgssv is referencing other routines. Where are those defined, such as superlu_free and pdgssv?
0 Kudos
Reply