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

linking C++ and Fortran in Visual Studio 2010

Morteza_N_
Beginner
1,377 Views

Hello

I have the same problem as described in the following web page

https://software.intel.com/en-us/forums/topic/326037

I am trying to call the c++ function from Fortran main program. to do that I followed the bellow steps in visual Studio 2010:

To create a C++ static library project

  1. On the menu bar, choose FileNewProject.
  2. In the left pane of the New Project dialog box, expand InstalledTemplatesVisual C++, and then select Win32.
  3. In the center pane, select Win32 Console Application.
  4. Specify a name for the project—for example, MathFuncsLib—in the Name box. Specify a name for the solution—for example, StaticLibrary—in theSolution Name box. Choose the OK button.
  5. On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.
  6. On the Application Settings page, under Application type, select Static library.
  7. On the Application Settings page, under Additional options, clear the Precompiled header check box.
  8. Choose the Finish button to create the project.

 

To create a executable Fortran project

  1. On the menu bar, choose FileNewProject.
  2. In the left pane of the New Project dialog box, expand InstalledTemplatesIntel(R) Visual Fortran , and then select Console Application.
  3. In the center pane, select Empty Project.
  4. Specify a name for the project and then Specify a name for the solution. In the solution box select the "Add to solution". Choose the OK button.

 

In addition I do some setting in Visual studio as below:

  1. Right-click the executable Fortran project and select Dependencies to set the executable project as dependent on the static library project.
  2. Right-click on the executable project and select Set as Startup Project so that you can build it and debug.

I have the below Fortran main program and C++ function.

-------------------------- Fortran program  -------------------------------------

program main

 

  use iso_c_binding, only : C_CHAR, C_NULL_CHAR

  

  implicit none

 

  interface

    subroutine print_C ( string ) bind ( C, name = "print_C" )

      use iso_c_binding, only : C_CHAR

      character ( kind = C_CHAR ) :: string ( * )

    end subroutine print_C

  end interface

 

  call print_C ( C_CHAR_"Hello World!" // C_NULL_CHAR )

 

end

 

 

-------------------------- C++ Function  -------------------------------------

# include <stdlib.h>

# include <stdio.h>

 

extern "C" void print_C (char *text)

{

  printf("%s\n", text);

}

 

When I build the program I will confront to the following errors:

Error 1: error LNK2019: unresolved external symbol _print_C referenced in function _MAIN__       Fortranmain.obj     

Error 2: fatal error LNK1120: 1 unresolved externals   Debug\Fortranmain.exe     

 

Could anyone help me? Any suggestion would be highly appreciate.

0 Kudos
9 Replies
mecej4
Honored Contributor III
1,377 Views

I followed the steps that you listed but in VS 2013, and I was able to build the solution.

In your C++ library project properties, check "Librarian->Command Line" tab and note the name of the library that is to be generated.

Then, after a build attempt, examine the build log and see if the linker command line for the Fortran project contains the name of that library. If you do not see the library name included, check to see if the solution properties shows, under Common Properties-->Project Dependencies--> Projects select the Fortran project. In the "Depends on" pane, you should see the box for the library project checked. In not, select it. 

0 Kudos
Morteza_N_
Beginner
1,377 Views
Thank you very much. I am a beginner in C++ so, I don't know how to do the setting mentioned above (i.e. In your C++ library project properties, check "Librarian->Command Line" tab and note the name of the library that is to be generated.) could you please guide me step by step. Thank you again.
0 Kudos
mecej4
Honored Contributor III
1,377 Views

This is not a "setting to do", but just a check to find out the name. Please see the screenshot below. Once you know the name of the library (by looking at the property page, or by searching in the project directories after a build of the library project has succeeded), check the build log from the Fortran project build. You should see the name of the library in the linker command line.

ifort /nologo /debug:full /Od /warn:interfaces /module:"Debug\\" /object:"Debug\\" /Fd"Debug\vc120.pdb" /traceback /check:bounds /check:stack /libs:dll /threads /dbglibs /c /Qvc12 /Qlocation,link,"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\\bin" "s:\lang\mixed\fmain\fmain.f90" Linking... Link /OUT:"Debug\fmain.exe" /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:"s:\lang\mixed\fmain\Debug\fmain.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"s:\lang\mixed\fmain\Debug\fmain.pdb" /SUBSYSTEM:CONSOLE /IMPLIB:"s:\lang\mixed\fmain\Debug\fmain.lib" "Debug\fmain.obj" "s:\lang\mixed\clib\Debug\clib.lib"

clib.jpg

0 Kudos
Morteza_N_
Beginner
1,377 Views

I did that and I saw the below:

/OUT:"C:\Users\morteza\Desktop\New folder (3) - Copy\Cpp_function\Debug\Cpp_function.lib" /NOLOGO 

for a suggestion and according to lots of forum which I read I think it is a good idea to explain an step by step procedure to mix a C++ function to a Fortran project. That would be helpful for dozen of people who want to call a C++ function from Fortan project.

thank you very much

0 Kudos
mecej4
Honored Contributor III
1,377 Views

Please show the build-log from the failed build that gave you the linker error message; in particular, I need to see the line in the build log that corresponds to what I showed in #4 after "Linking...".

I think it is a good idea to explain an step by step procedure
At first thought it would appear so, but the reality is that it will not help and will lead to more frustration, for two reasons: (i) the steps may depend on Visual Studio and compiler versions; (ii) if, whether by carelessness or an incorrect reading of the instructions, the steps are not performed exactly as instructed, the procedure will be considered "incorrect". It is far better to use logic and figure things out.

By the way, this whole task is trivially accomplished at the command line. However, if you insist on using the IDE, watch out:there are many knobs to turn, and many bells and whistles to distract you.

0 Kudos
Morteza_N_
Beginner
1,377 Views

this is the code and project associated to that matter.

0 Kudos
Morteza_N_
Beginner
1,377 Views

This the build Log:

Compiling with Intel(R) Visual Fortran Compiler XE 13.0.0.089 [IA-32]...

ifort /nologo /debug:full /Od /warn:interfaces /module:"Debug\\" /object:"Debug\\" /Fd"Debug\vc100.pdb" /traceback /check:bounds /check:stack /libs:static /threads /dbglibs /c /Qvc10 /Qlocation,link,"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\\bin" "C:\Users\morteza\Desktop\Call Cpp function\Cpp_function\Fortran_project\Fortran_project.f90"

Linking...

Link /OUT:"Debug\Fortran_project.exe" /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:"C:\Users\morteza\Desktop\Call Cpp function\Cpp_function\Fortran_project\Debug\Fortran_project.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\Users\morteza\Desktop\Call Cpp function\Cpp_function\Fortran_project\Debug\Fortran_project.pdb" /SUBSYSTEM:CONSOLE /IMPLIB:"C:\Users\morteza\Desktop\Call Cpp function\Cpp_function\Fortran_project\Debug\Fortran_project.lib" "Debug\Fortran_project.obj"

Link: executing 'link'

Fortran_project.obj : error LNK2019: unresolved external symbol _print_C referenced in function _MAIN__

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

0 Kudos
Steven_L_Intel1
Employee
1,377 Views

You didn't do this step:

  1. Right-click the executable Fortran project and select Dependencies to set the executable project as dependent on the static library project.
0 Kudos
Steven_L_Intel1
Employee
1,377 Views

I should add that it isn't sufficient to select Dependencies. You must then check the box for the library project to make it a dependent. For completeness, though it doesn't apply here, one can no longer do this to have a C++ project dependent on a Fortran project, as Microsoft disabled that for non-C++ projects. In such a case you have to manually add the dependent library to the C++ project.

0 Kudos
Reply