Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

Problem on calling fortran routine in visual c++ code

sky4you
Beginner
1,440 Views

Hello

I am trying to call fortran code in visual c++.

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

since my english is poor, I linked some webpages that I got helps

To test, I used a code from this link..

http://www.physiology.wisc.edu/comp/docs/notes/not017.html

In addition, to make a project & compile

Igot a help fromthis link

(the link use korean, but from the figures, I guess you can understand what I did

In addition, I use Visual studio 2005, so my configuration is a little bit diffrent.)

http://user.chollian.net/~gitae/programming/fortran/linking.htm

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

Here is what I did in detail

1. I made a fotran project (the name is Fortran_test) witha static library option.

And, I specified output directory to my c++ project directory

(which is a ../Mixed_Lang_Test)

Both c++ and fortranRuntime Librariesare Debug Multithreaded.

2. I made a new fortran code.

SUBROUTINE FR1(N,M)
M=N*N
RETURN
END

INTEGER FUNCTION FF1(N)
FF1=N*N*N
RETURN
END

3I added a new project in the previous solution which is a console application and empty project.

the prj name is Mixed_Lang_Test.

4. I set some options..

Linker -> Input -> Fortrean_test.lib

C/C++ -> Code Generation -> Runtime Library: Debug Multithreaded.

5. I made a code in the Mixed_Lang_Test

#include
#include

extern "C"
{
void __stdcall FR1(int*,int *);
int __stdcall FF1(int *);
}


int main()
{
int n=10,nSq,nCube;
FR1(&n,&nSq);

//cout << "The square is:" << nSq << endl;
printf("the square is: %d \n",nSq);
nCube=FF1(&n);
//cout << "The Cube is:" << nCube << endl;
printf("the Cobe is: %d \n",nCube);
return 0;
}


6. I set the Mixed_Lang_Test as a start project and compile

7. Errors@!@@!!!

2>Mix_C.obj : error LNK2019: unresolved external symbol _FF1@4 referenced in function _main
2>Mix_C.obj : error LNK2019: unresolved external symbol _FR1@8 referenced in function _main
2>C:\Documents and Settings\mhkim\My Documents\Visual Studio 2005\Projects\Mixed_Lang_Test\Debug\Mixed_Lang_Test.exe : fatal error LNK1120: 2 unresolved externals
2>Build log was saved at "file://c:\Documents and Settings\mhkim\My Documents\Visual Studio 2005\Projects\Mixed_Lang_Test\Mixed_Lang_Test\Debug\BuildLog.htm"
2>Mixed_Lang_Test - 3 error(s), 0 warning(s)
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

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

How could I fix the errors???

Ahh..again, I am using visual studio 2005

and I recently downloaded the evaluation kit for fortran (v11??)

I will also add my project..

Plz help me out!!

Thank you in advance.

0 Kudos
1 Solution
Jugoslav_Dujic
Valued Contributor II
1,440 Views
Quoting - sky4you

5. I made a code in the Mixed_Lang_Test

#include
#include

extern "C"
{
void __stdcall FR1(int*,int *);
int __stdcall FF1(int *);
}

7. Errors@!@@!!!

2>Mix_C.obj : error LNK2019: unresolved external symbol _FF1@4 referenced in function _main

Ahh..again, I am using visual studio 2005

Intel Fortran does not use __stdcall anymore, like its predecessor Compaq Visual Fortran. Remove __stdcall (or substitute it with __cdecl) and you'll probably be fine (I didn't read thoroughly the rest of your message).

View solution in original post

0 Kudos
5 Replies
Jugoslav_Dujic
Valued Contributor II
1,441 Views
Quoting - sky4you

5. I made a code in the Mixed_Lang_Test

#include
#include

extern "C"
{
void __stdcall FR1(int*,int *);
int __stdcall FF1(int *);
}

7. Errors@!@@!!!

2>Mix_C.obj : error LNK2019: unresolved external symbol _FF1@4 referenced in function _main

Ahh..again, I am using visual studio 2005

Intel Fortran does not use __stdcall anymore, like its predecessor Compaq Visual Fortran. Remove __stdcall (or substitute it with __cdecl) and you'll probably be fine (I didn't read thoroughly the rest of your message).

0 Kudos
TimP
Honored Contributor III
1,440 Views
Quoting - Jugoslav Dujic

Intel Fortran does not use __stdcall anymore,

ifort has supported iso_c_binding for a year now, so you should be able to find suitable web references, as well as the references on C interoperability in the ifort documentation.

0 Kudos
anthonyrichards
New Contributor III
1,440 Views
You say at the start that you have created a static library from your Fortran code. In that case you have to EXPORT symbols so that the linker can find the symbols you use. When you have done this, include your Fortran static library with your C main project and it should be able to find the symbols you want. I think using__stdcall only prepends an underscore to the C++ name, whereas __cdecl prepends the underscore and appends (decorates) by adding @4N where N= the number of arguments (or is the other way round?!). The symbol sought by the C++ program must match the symbol held in the Static libray's symbol table. You can check symbol names in librariesusing the DUMPBIN tool supplied with the compiler.

0 Kudos
sky4you
Beginner
1,440 Views
Quoting - Jugoslav Dujic

Intel Fortran does not use __stdcall anymore, like its predecessor Compaq Visual Fortran. Remove __stdcall (or substitute it with __cdecl) and you'll probably be fine (I didn't read thoroughly the rest of your message).


Hi~~

Thank you for your help..

After substituting it and setting some paths..

it works!!

And for static library..for some reasons (I don't know why..)

even if I didn't export anything..it worked!!

Thank you for all your help!!

0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,440 Views
Just a couple of corrections...

Quoting - anthonyrichards
You say at the start that you have created a static library from your Fortran code. In that case you have to EXPORT symbols so that the linker can find the symbols you use.

No, you cannot "export" anything from a static library, only from dynamic ones. "Everything" in the static library is visible to the outer world. That's why they're bad in encapsulation.

I think using__stdcall only prepends an underscore to the C++ name, whereas __cdecl prepends the underscore and appends (decorates) by adding @4N where N= the number of arguments (or is the other way round?!).

The other way round, actually.

0 Kudos
Reply