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.
29298 Discussions

Linking Visual C++ .NET w/ Visual Fortran

gummioc3
Beginner
767 Views
I'm a newbie and in need of help. I would like to be able to create a mixed-mode C++ program so that I can call it from withing a Visual Fortran Compiled source, and have it available to C#.
My problem and question is, how do I compile and especially link a mixed-mode project using Visual Studio .NET and either Compaq Visual Fortran or Intel Visual Fortran?
Do you first have to compile the Fortran into a .lib or something? Then do you link? What is the procedure?
Below is a VERY simple example,and if anyone could help me figure out how to compile it and link it in Visual Studio, that would be most helpful.

C++:

#include

extern "C" /* this line should be omitted in C */
{
void hello_(long *integ, char *in_str, int in_str_length) {
// must take care of the string since fortran does not provide a
// � at the end of the string.
char* str=new char[in_str_length+1];// C++ memory allocation
for (unsigned int i=0; i str=in_str;// strncpy is probably nicer here
str[in_str_length]='�';

// normal C/C++ programming here
std::cout << "Hello ";
std::cout << "You passed the following to me: ";
std::cout << "long integer: " << *integ << ' ';
std::cout << "string: " << str << ' ';
std::cout << "string length: " << in_str_length << " ";

delete[] str;// C++ memory cleanup
}
}

/* Need a small wrapper for the fortran main program, since fortran
code should not be used as the main program, see information
elsewhere */
extern "C" int mymain_();
int main()
{
return mymain_();
}

FORTRAN:

SUBROUTINE MYMAIN

CHARACTER*11 NAME

INTEG=42
NAME='HELLO WORLD'
CALL HELLO(INTEG,NAME)

RETURN
END

Thanks so much for all replys!!

0 Kudos
6 Replies
Jugoslav_Dujic
Valued Contributor II
767 Views
Sorry, I don't have time to make a VF project out of your code; I've just posted a similar solution in this thread so you can use it as starting point. Basically, you have to make a .lib consisting solely of Fortran code.

The following changes are required to combine a C++ project (.exe) and Fortran library:

* add yourlib.libto C++ linker settings
* add libifcore.lib and ifconsole.lib to C++ linker settings*
* add directories of yourlib and Intel libraries (by default C:Program FilesIntelCompilerFortran9.0IA32Lib) to "Additional Library Directories"**
* specify that project yourexe depends on project yourlib (Project/Dependencies...)

Further, in your current code, you used lowercase on C++ side, but IVF treats external symbols as uppercase by default. So, do one of:
* uppercase "hello" and "mymain" on C++ side
* specify "Lower case" for yourlib in Project/Properties/Fortran/External procedures
* use DEFAULT, ALIAS attributes to lowercase "hello" and "mymain" on Fortran side

Also, get rid of underscores -- they're appended by default.

HTH
Jugoslav

* Actually, you should match C++ run-time library setting and IVF run-time library you're using. See "Intel Fortran/C Mixed-Language Programs Overview" in Mixed-Language chapter of IVF documentation.

** Best, add IVF Lib directory to Tools/Options/Projects/C++ Directories/Library files. That will save you the need to add it in every new project.

Message Edited by JugoslavDujic on 10-14-2005 10:24 AM

Message Edited by JugoslavDujic on 10-14-2005 02:38 PM

0 Kudos
gummioc3
Beginner
767 Views
Thank you for all your help. Do you happen to have a different link to the thread you tried to point me to? It says the forum thread has been deleted. Thanks....
0 Kudos
Jugoslav_Dujic
Valued Contributor II
767 Views
Fixed above.

Jugoslav

P.S. Post-editing Forum messages with my Mozilla Firefox sometimes leads to unexpected results. Although it's been raised, the Forum does not support the editing tab in Firefox. I still think that Lithium Forum used by Intel is not an optimal solution, as it's the quirkiest one of all forum softwares I've used; too late to change it, but maybe Intel should bitch at the vendor a bit more.

Message Edited by JugoslavDujic on 10-14-2005 02:46 PM

0 Kudos
TimP
Honored Contributor III
767 Views
Jugoslav's project is posted at http://softwareforums.intel.com/ids/board/message?board.id=5&message.id=15283

Mozilla http://www.mozilla.org/releases/mozilla1.7.12/ is the only browser I have used on linux which posts replies successfully on these forums. It even follows hidden links. Linux firefox doesn't work well at all.

Given that the forums are supposed to support linux users, it does seem that more support should be given to standard browsers.
0 Kudos
Steven_L_Intel1
Employee
767 Views
Adding the editing controls for Firefox was supposed to be already done, but clearly it is not (I use Firefox here myself.) I expect this to be fixed soon. Note that you can use simple HTML here, including "pre" tags for code. Also, when editing, it's best to uncheck the box "Automatically convert carriage returns to HTML line breaks".
0 Kudos
Jugoslav_Dujic
Valued Contributor II
767 Views
I do utilize all the techniques you mention, but the ^$#@& software insists on checking "Convert carriage returns" and clearing "E-mail me..." box every time I preview or edit a message.

Jugoslav
0 Kudos
Reply