PGI User Forum
 SearchSearch   MemberlistMemberlist     RegisterRegister   ProfileProfile    Log inLog in 

Free OpenACC Webinar

PVF 2010 Linking C and Fortran

 
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling
View previous topic :: View next topic  
Author Message
initialzero



Joined: 12 Feb 2009
Posts: 10

PostPosted: Wed Mar 30, 2011 1:35 pm    Post subject: PVF 2010 Linking C and Fortran Reply with quote

I have some linking problems. Let me explain the dependency arrangement and hopefully we can figure this out.

static fortran library: foo.lib
static c library: bar.lib which depends on foo.lib
executable c program: baz.exe which depends on foo.lib and bar.lib


foo.lib has a subroutine:
SUBROUTINE AABB (RA,RB,AA,AB,A,B)

bar.lib has a a.c which wants to call AABB:
so in a.c I have:
extern aabb_();
...
void somefunc()
...
aabb_(&ra, &rb, &aa, &ab, &a, &b);
...

baz.exe wants to call somefunc()
but linking complains:
error LNK2019: unresolved external symbol _aabb_ referenced in function _somefunc File: bar.lib(a.obj)


Please help! Thanks


Last edited by initialzero on Fri Apr 01, 2011 10:23 am; edited 1 time in total
Back to top
View user's profile
initialzero



Joined: 12 Feb 2009
Posts: 10

PostPosted: Wed Mar 30, 2011 2:57 pm    Post subject: I have tried Reply with quote

And I have tried the following but it fails with:


Code:
Error   81   error LNK2005: __heap_alloc already defined in libcmt.lib(malloc.obj)
Error   82   error LNK2005: __recalloc already defined in libcmt.lib(recalloc.obj)
Error   83   error LNK2005: __msize already defined in libcmt.lib(msize.obj)
Error   84   error LNK2005: __crt_debugger_hook already defined in libcmt.lib(dbghook.obj)
Error   85   error LNK2005: __isctype_l already defined in libcmt.lib(isctype.obj)
Error   86   error LNK2005: __isctype already defined in libcmt.lib(isctype.obj)
Error   109   error LNK2019: unresolved external symbol __imp__strncpy referenced in function _xxxx
Error   110   error LNK2019: unresolved external symbol __imp__fwrite referenced in function _YYYYYY
Error   111   error LNK2019: unresolved external symbol __imp__fprintf referenced in function _zzzzz
Error   112   error LNK2019: unresolved external symbol __imp___localtime64 referenced in function _localtime
Error   113   error LNK2019: unresolved external symbol __imp___time64 referenced in function _time
Error   114   error LNK2019: unresolved external symbol __imp___stat64i32 referenced in function _stat
Error   115   error LNK2019: unresolved external symbol _foofoo_ referenced in function _aaaaa
Error   116   error LNK2019: unresolved external symbol _barbar_ referenced in function _bbbbbb
Error   117   error LNK2019: unresolved external symbol _parameters_ referenced in function _ccccccc
Error   118   error LNK2019: unresolved external symbol __imp__totor referenced in function _ssssssss
Error   119   error LNK1120: 10 unresolved externals



Add the PGI library directory, "C:\Program Files\PGI\win32\11.3\lib" to: "Linker | General | Additional Libraries Directories" (Adjust the PATH as necessary)

Then add the PGI Runtime libraries to "Linker | Input | Additional Dependencies"

Here's the full list:
Code:
Code:
libpgmp.lib;pg.lib;libpgf90.lib;libpgf90_rpm1.lib;libpgf902.lib;libpgf90rtl.lib;libpgftnrtl.lib;libpgc.lib;libnspgc.lib;libcmt.lib;



Finally add "-nodefaultlib:msvcrtd" to "Linker | Command Line | Additional Options". Note, change "msvcrtd" to "msvcrt" when changing from Debug to a Release build.
Back to top
View user's profile
initialzero



Joined: 12 Feb 2009
Posts: 10

PostPosted: Thu Mar 31, 2011 10:34 am    Post subject: Another try Reply with quote

So I decided to combine the fortran and c objects into one library using ar from the pgi tools.

I removed libcmt.lib since that seems to have a lot of conflicts event with -nodefaultlib:msvcrtd.

With the new library and removal of libcmt.lib I now get some unresolved symbols such as:
___mth_i_dsinx
___mth_i_dcosx
___mth_i_dsincosx
___mth_i_dasinx
___mth_i_idnintx
libpgf90.lib(initpar.obj) : error LNK2019: unresolved external symbol __environ referenced in function _pgf90_compiled
libpgc.lib(pgstdinit.obj) : error LNK2001: unresolved external symbol __environ
Back to top
View user's profile
initialzero



Joined: 12 Feb 2009
Posts: 10

PostPosted: Thu Mar 31, 2011 1:47 pm    Post subject: Reply with quote

Selecting the correct library type helps thanks to reading the documentation. Now I only can't find the external symbol which is referenced from the static c library to the static fortran library.

I even broke this problem down to the very basics. One function in a fortran library, one function in a c library that calls the fortran function. one c executable that references both libraries. And I still get unresolved external symbol libclibrary.lib(someobject.obj)
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 6214
Location: The Portland Group Inc.

PostPosted: Fri Apr 01, 2011 2:20 pm    Post subject: Reply with quote

Hi initialzero,

For the first problem, this looks like a calling conversion issue (See Chapter 12 "Win32 Calling Conventions" of the PGI User's Guide). Try compiling the Fortran code with "-Miface=unix".

Several of the Microsoft header files need to be compiled differently depending upon if you will be linking dynamically or statically. It seems to me that you're compiling dynamically but linking statically. The "__imp__" symbols are import library symbols, i.e. the interface to a DLL call. The "__environ" symbol occurs when you build dynamically but link with static C runtime library (libcmt.lib), since this symbol is only available in the dynamic C runtime library (mscvrt.dll)

Hope this helps,
Mat
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group