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

Need help linking c++ function with fortran main in visual studio 2003 .NET

pal24
Beginner
1,677 Views
Hi,

Could anybody provide me with a simple walkthrough of how to link a c++ function with a visual fortran main program in visual studio 2003. I'm using Intel Visual fortran 9.0.

I would like to write the following program for example:

Fortran (hello.f):
-------------------
program hello
call helloworld()
stop
end program hello

C++ (helloworld.c):
-------------------
void helloworld()
{
printf("Hello World ");
}

In all my previous attempts, I get the unresolved reference error in when trying to compile the "solution".


Thanks,
0 Kudos
10 Replies
TimP
Honored Contributor III
1,677 Views
Several methods for reconciling them are discussed in the docs files which come with ifort. For example, change to
extern "C" void HELLOWORLD ()
I question whether you want to tackle writing to stdout using C source embedded in a C++ function called from Fortran, if you aren't willing to read about the difficulties.
0 Kudos
pal24
Beginner
1,677 Views
Hi Tim,

Thanks for the help. I finally got my program working.
0 Kudos
tom_c_lin
Beginner
1,677 Views
Hi Gentlemen, Good Day,
I have been trying, but unsuccessfully, to link a Fortran main program with C++ library using .NET IDE. This is an'unmanaged' code project(The codes were linked successfully under Intel Fortran command-line). I am really glad to find this message and would like to learn more. Are you using command-line or IDE? If it was .NET IDE. How do you setup the project? one soluction with two inter-dependent projects? How about the project dependencies? Can you share with us your experience on Fortran calling C/C++? Thanks. Your responses are highly appreciated.
Tom Lin
0 Kudos
eric_zhou
Beginner
1,677 Views

Hi, pal24,

After I tried you helloworld code, error comes out:

error LNK2019: unresolved external symbol _HELLWORLD referenced in function _MAIN

Any changes in project properties

Thanks

Eric

0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,677 Views
_HELLWORLD is missing an "O".
0 Kudos
eric_zhou
Beginner
1,677 Views

sorry it was _HELLOWORLD

I generated test fortran project "test" than add "hellow.cpp" into the project

All the configrations are default and build the solution was not successed

here is my code:

program test ! test.f90

implicit none

! Variables
call helloworld();

! Body of test


end program test

///// "hellow.cpp""

extern "C" void helloworld();
void helloworld()
{
printf("hello world ");

}

/////build output

------ Rebuild All started: Project: test, Configuration: Debug Win32 ------

Deleting intermediate files and output files for project 'test', configuration 'Debug|Win32'.
Compiling with Intel Fortran 9.0...
test.f90
Linking...
test.obj : error LNK2019: unresolved external symbol _HELLOWORLD referenced in function _MAIN__
Debug/test.exe : fatal error LNK1120: 1 unresolved externals

Build log written to "file://C:Documents and SettingsEricMy Documents estDebugBuildLog.txt"
test build failed.


---------------------- Done ----------------------

Rebuild All: 0 succeeded, 1 failed, 0 skipped

It seems to me that it doesn't compile the c file (hellow.cpp).

any thought please, thanks

Eric

0 Kudos
tom_c_lin
Beginner
1,677 Views
I finally got my 'Fortran-call-C++' project working. Here is what I did.
1. create a Fortran main project
2. add a C++ project inside the Fortran project
(I select static library in application settings)
3. use add item (in C++ project) to enter C++ code.
4. enter Fortran code in Fortran main project
5. right click Fortran project, select project dependencies,
check the box of the C++code (name). Make sure that the Fortran is
depending on C++ code (Build Order should be C++ code then Fortran)
6. select the same runtime library for both C++ and Fortran
(I used Debug Single-threaded)
7. In the 'additional dependency' of the Fortran add your C++ library name
8. remember in C++, your need to use 'extern "C" ' instead of 'extern'.
Hope this will help. Good luck.
Tom Lin
0 Kudos
eric_zhou
Beginner
1,677 Views

I did it exactly same.

error comes out: cannot open "ctest.lib"

When I add new c++ project, I choose win32 (console) project with static lib

and then add a hellow.cpp file into c++ project.

Do we need main() in c++ project?

Eric

0 Kudos
tom_c_lin
Beginner
1,677 Views
For my project, C++ consists of only routines (so called subroutines in Fortran). I don't think you need a C++ main program. My main programis a Fortran which calls C++ routines. I did see the same error message before, i.e., can't open Csub.lib. Whatyou can do is 'add the directory of you C++ library'before you C++ library name (in the additional dependencies of your Fortran project) or copy your C++ library manually toyour Fortran project directory.You need to make sure that your Fortran program 'sees' the C++ library. Good luck.
Tom Lin
0 Kudos
eric_zhou
Beginner
1,677 Views

Tom,

Thanks a lot

Mine is working now.

Eric

0 Kudos
Reply