Quantcast
Channel: Intel® Fortran Compiler
Viewing all articles
Browse latest Browse all 3270

Linker errors when calling a C++ routines from Fortran

$
0
0

I am sure that my error is a trivial one here, but I am at a loss for what I am doing wrong.  I want to test returning a struct from C++ to my Fortran code, but I can't even get the test program to link correctly and I am hoping someone can offer up advice.  My test solution was set up in VS2013, with a Fortran program, and a C++ DLL.  The C++ is as follows:

// Win32Project1.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"

struct interop_type {
	int i;
	double j;
};
extern "C" {
	__declspec(dllexport) interop_type __stdcall _CPP_func(int* arg) {
		interop_type ret_val;

		ret_val.i = *arg * 4;
		ret_val.j = *arg / 2.0;

		return ret_val;
	}
}

And the Fortran that I want to call this function from is:

program FortranCPPInterop
    implicit none
!DEC$ ATTRIBUTES DLLIMPORT, STDCALL, ALIAS: 'CPP_func' :: CPP_func
    type inter_op
        integer :: i
        double precision :: j
    end type inter_op

    TYPE(inter_op) :: test, CPP_func

    test = CPP_func( 3)

    print*, test%i
    print*, test%j

end program

When I attempt to build, I get the following error:
1>Source1.obj : error LNK2019: unresolved external symbol __imp_CPP_func referenced in function _MAIN__

Now, I have added the library (.lib) file created by the C++ side of the world as a library to be specifically included in the Fortran Linker options, and I have set the additional include paths for libraries in the Linker option to include the containing directory as well.  Can someone point to the mistake I am making?  Thanks!


Viewing all articles
Browse latest Browse all 3270

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>