- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- On the menu bar, choose File, New, Project.
- In the left pane of the New Project dialog box, expand Installed, Templates, Visual C++, and then select Win32.
- In the center pane, select Win32 Console Application.
- 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.
- On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.
- On the Application Settings page, under Application type, select Static library.
- On the Application Settings page, under Additional options, clear the Precompiled header check box.
- Choose the Finish button to create the project.
To create a executable Fortran project
- On the menu bar, choose File, New, Project.
- In the left pane of the New Project dialog box, expand Installed, Templates, Intel(R) Visual Fortran , and then select Console Application.
- In the center pane, select Empty Project.
- 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:
- Right-click the executable Fortran project and select Dependencies to set the executable project as dependent on the static library project.
- 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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 procedureAt 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You didn't do this step:
- Right-click the executable Fortran project and select Dependencies to set the executable project as dependent on the static library project.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page