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

Unresolved C external

mhovers
Beginner
2,538 Views

OK, I went back to basics:

IVF code:

program fortran_c_test

implicit none

interface

subroutine c_code_test(index,foo)

!DEC$ ATTRIBUTES C, ALIAS:'_c_code_test' :: c_code_test

integer index

real(8) foo

end subroutine c_code_test

end interface

integer index

real(8) foo

index = 3

call c_code_test(index,foo)

write(*,*) foo

stop

end

MVS C++(C) code

#include

void c_code_test(index,foo)

int index;

double *foo;

{

*foo = 3.21 * index;

}

This is compiled and put into: C:mhoversfortran_c_testDebugc_code_dir.lib

In Project->Linker->Input->Additional Dependencies I have c_code_dir.lib

In Project->Linker->General->Additional Library Directories I have

C:mhoversfortran_c_testDebug

In Tools->Options->Intel Fortran->General->Libraries I have

C:mhoversfortran_c_testDebug

When I build fortran_c_test I get:

error LINK2019: unresolved external symbol _c_code_test referenced in function _MAIN__

Have I screwed up the INTERFACE or what?

0 Kudos
26 Replies
Steven_L_Intel1
Employee
1,875 Views
Please don't start a new thread for the same topic - reply to your other thread instead.

If your "C++(C)" code has a .cpp file type, you need to add:

extern "C"

before the "void" so as to suppress C++ name mangling.
0 Kudos
mhovers
Beginner
1,875 Views

Sorry about the new threads, I will use "reply" from now on.

The C code is c_code_test.c and I have set:

Project->Properties->Configuration Properties->C/C++->Advanced/Compile as C Code (/TC).

However, my f90 application still give an unresolved external for the _c_code_test.

thanks

0 Kudos
Les_Neilson
Valued Contributor II
1,875 Views

Try replacing

void c_code_test

with

void __stdcall c_code_test

(Note thedouble underscore)

Les

0 Kudos
anthonyrichards
New Contributor III
1,875 Views

With 'ATTRIBUTES C' set in the Fortran, perhaps ' __cdecl' in the C might be needed.

Also, in the Fortran,I think 'VALUE' and 'REFERENCE' attributes should be specified for the 2 arguments.

0 Kudos
Les_Neilson
Valued Contributor II
1,875 Views

Yes of course. I was looking at our ownFortran->C code and jumped in without looking carefully at the OP code. :-(

In the Fortran help look for "Attibutes Properties and Calling Conventions"

Les

0 Kudos
Steven_L_Intel1
Employee
1,875 Views
I just tried the code in the original post and it linked fine for me without errors. I will agree that in the Fortran code, "foo" needs to have !DEC$ ATTRIBUTES REFERENCE :: FOO added so that it is passed correctly. All I can think of is that the C object is not being linked in. The build log should show this.
0 Kudos
mhovers
Beginner
1,875 Views

------ Build started: Project: fortran_c_test, Configuration: Debug|Win32 ------

The build log is below, I looks like it is looking in c_code_dir.lib??

Linking...

Link /OUT:&quotDebug/fortran_c_test.exe

" /INCREMENTAL:NO /NOLOGO /LIBPATH:&quotC:mhoversfortran_c_testc_code_dirDebug" /MANIFEST /MANIFESTFILE:&quotC:mhoversfortran_c_testdebugfortran_c_test.exe.intermediate.manifest" /DEBUG /PDB:&quotDebug/fortran_c_test.pdb" /MAP:&quotfortran_c_test_map" /SUBSYSTEM:CONSOLE c_code_dir.lib &quotDebug/fortran_c_test.obj"

Link: executing 'link'

fortran_c_test.obj : error LNK2019: unresolved external symbol _C_CODE_TEST referenced in function _MAIN__

Debug/fortran_c_test.exe : fatal error LNK1120: 1 unresolved externals

fortran_c_test build failed.

0 Kudos
mhovers
Beginner
1,875 Views

Maybe the problem is in the building of the C code library??

Under Project Defaults I have:

Configuration type: Static Library (.lib)

Use of MFCUse Standared Windows Libraries

Use of ATLNot Using ATL

Minimize CRT Use in ATLNO

Common Language Runtime supportNo Common Language runtime support

Are there any things I should check in the C library build??

thanks

0 Kudos
Steven_L_Intel1
Employee
1,875 Views
Visual Studio is not even trying to link in your library.

Are you using VS2005? If so, have you installed VS2005 Service Pack 1? If not, uninstall the Intel Fortran integrations, install VS05 SP1 and reinstall the integrations. This will probably fix the problem.

Next, is the static library project a "dependent" of the executable project? Right click on the executable and select Dependencies. Make sure the box for the library is checked.
0 Kudos
Mike_H_
Beginner
1,875 Views

I will have to install the service pac. I only mess with the fortran integration with MVS every couple of years when I get new systems and it never goes smoothly. Can you tell me the order. Should I 1) instlll MVS, 2) install the survice pac, 3) install IVF ? I can't remember if I need IVF installed before MVS for it to do the integration or not?

The above assumes that I uninstall everything and start from scratch, which is usually the only way I can get any of the MVS IVF inegration to work, but I would try to "uninstall the IVF integration, and re-install it if you can direct me to how to do that.

thanks very much

0 Kudos
Steven_L_Intel1
Employee
1,875 Views
1. Control Panel > Add or Remove Programs. Select Intel Visual Fortran Compiler 10.0 Integrations in Microsoft Visual Studio and click Remove.
2. Download and install VS2005 SP1.
3.. Locate the file C:Program FilesIntelDownloadIntelFortranCompiler10installsw_ide_p_10.0.xxx.exe (where xxx is the latest number on your system - the _p_ may be _c_ instead) and run it.

This should take care of it.
0 Kudos
mhovers
Beginner
1,875 Views

OK, got the service pac installed and the IVF fortran interface re-installed. Still no joy.

For completeness here is the code and build.log

program fortran_c_test

implicit none

interface

subroutine c_code_test(index,foo)

!DEC$ ATTRIBUTES C, ALIAS:'_c_code_test' :: c_code_test

!DEC$ ATTRIBUTES VALUE :: index

!DEC$ ATTRIBUTES REFERENCE :: foo

integer index

real(8) foo

end subroutine c_code_test

end interface

integer index

real(8) foo

index = 3

call c_code_test(index,foo)

write(*,*) foo

stop

end

C- Code:

#include

void

c_code_test(index,foo)

int

index;

double

*foo;

{

*foo = 3.21 * index;

}

Build Log:

------ Build started: Project: fortran_c_test, Configuration: Debug|Win32 ------

Compiling with Intel Fortran Compiler 10.0.025 [IA-32]...

ifort /nologo /Zi /Od /module:&quotDebug/

" /object:&quotDebug/" /traceback /check:bounds /libs:static /threads /dbglibs /c /Qvc8 /Qlocation,link,&quotC:Program FilesMicrosoft Visual Studio 8VCin" &quotC:mhoversfortran_c_testfortran_c_test.F90"

Linking...

Link /OUT:&quotDebug/fortran_c_test.exe

" /INCREMENTAL:NO /NOLOGO /LIBPATH:&quotC:mhoversfortran_c_testc_code_dirDebug" /MANIFEST /MANIFESTFI LE:&quotC:mhoversfortran_c_testdebugfortran_c_test.exe.intermediate.manifest" /DEBUG /PDB:&quotDebug/fortran_c_test.pdb" /MAP:&quotfortran_c_test_map" /SUBSYSTEM:CONSOLE c_code_dir.lib &quotDebug/fortran_c_test.obj"

Link: executing 'link'

fortran_c_test.obj : error LNK2019: unresolved external symbol c_code_test referenced in function _MAIN__

Debug/fortran_c_test.exe : fatal error LNK1120: 1 unresolved externals

fortran_c_test - 2 error(s), 0 warning(s)

Is this a naming convention problem now?

0 Kudos
Steven_L_Intel1
Employee
1,875 Views
There's something odd in that log - it shows the C library being first in the link. That's not right and is the cause of the problems.

I have attached a ZIP of a working build. Compare it to yours. In particular, make sure that the Fortran project is the "start up project" (shown in bold) and that the C library project is a dependent of the Fortran project.
0 Kudos
mhovers
Beginner
1,875 Views

Your project works fine. I notice that you have the C and Fortran all in one. I had a seperate C project to build a static library and a seperate Fortran project that tried to link to it.

Where can I find instructions on how to have multiple directiories and source code types in one project as you have done?

Thanks very much for all your help, I never would have figured this out (may still not when I have to go add the real stuff) without your help.

cheers

0 Kudos
Steven_L_Intel1
Employee
1,875 Views
What I have is still two separate projects, but they are together in one solution. You can still do this - right click on the solution in the Solution Explorer pane, select Add, Existing Project. Browse to the .vcproj file of your C library and add it. I happen to like to keep the directories together, but they don't have to.

To do this from scratch, use Add, New Project. Either way, you'll then want to right click on the executable project, select Dependencies, and check the box for the library project. Also make sure that the executable project is the "startup project" (shown in bold). If not, right click on it and select Set As Startup Project.

How did you specify that the C library should be linked in?
0 Kudos
mhovers
Beginner
1,875 Views

In MVS before the upgrade to SP1 there was a button under Project types -> Visual C++ to build static libraries (*.lib). That is gone now. How do I build a static library project now?

Thanks,

0 Kudos
Steven_L_Intel1
Employee
1,875 Views
I don't know what it was like before SP1, but for Visual C++, you select a Win32 project and then when the project wizard opens, static library is one of the additional options choices.
0 Kudos
mhovers
Beginner
1,875 Views

I am trying to reproduce the example you sent which works on my system. I have created the 2 projects, built the static lib and set the dependencies in the fortran project. When I try to build I get the following errors.

I have tried to compare all of the properties between your projects and mind and can find no differences (although I know they must exist). Somehow my build is having trouble with MSVCRTD.lib but I cant figure out why?

Linking...

Link /OUT:&quotDebugfortran_c_test2.exe

" /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:&quotC:mhoversfortran_c_test2debugfortran_c_test2.exe.intermediate.manifest" /DEBUG /PDB:&quotC:mhoversfortran_c_test2debugfortran_c_test2.pdb" /SUBSYSTEM:CONSOLE &quotDebugfortran_c_test.obj" &quotc:mhoversfortran_c_test2debugc_code.lib"

Link: executing 'link'

c_code.lib(c_code_dir.obj) : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification

MSVCRTD.lib(ti_inst.obj) : error LNK2005: &quotprivate: __thiscall type_info::type_info(class type_info const

&)" (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)

MSVCRTD.lib(ti_inst.obj) : error LNK2005: &quotprivate: class type_info

& __thiscall type_info::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)

LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library

Debugfortran_c_test2.exe : fatal error LNK1169: one or more multiply defined symbols found

0 Kudos
Steven_L_Intel1
Employee
1,875 Views
The C++ project defaults to using the multithreaded debug DLL libraries, whereas the Fortran project is using the multithreaded debug static libraries. I changed the C++ project (under Code Generation) to match.
0 Kudos
mhovers
Beginner
1,720 Views

OK, now I can build my version of the test code. Now I am trying to get it real. I need to allocate memory in the fortran and pass it into the c code. I have:

program fortran_c_test

implicit none

interface

subroutine c_code_test(index,foo,val)

!DEC$ ATTRIBUTES C, ALIAS:'_c_code_test' :: c_code_test

!DEC$ ATTRIBUTES VALUE :: index

!DEC$ ATTRIBUTES REFERENCE :: foo

!DEC$ ATTRIBUTES REFERENCE :: val

integer index

real(8) foo

complex(8) val

end subroutine c_code_test

end interface

integer index, n, i

complex(8), dimension(:), allocatable :: val

n=5

allocate(val(n))

do i=1,n

val(i) = dcmplx(0.5, 1.2) * i;

end do

index = 3

call c_code_test(index,foo,val)

write(*,*) foo

stop

end

#include

typedef struct { double r, i; } doublecomplex;

void c_code_test(index,foo,val)

int index;

double *foo;

doublecomplex *val;

{

*foo = 3.21 * index;

}

As long as val is a single complex(8) all is well, but as soon as I allocate val I get:

C:mhoversfortran_c_test2fortran_c_test.f90(29) : Error: The shape matching rules of actual arguments and dummy arguments have been violated. [VAL]

compilation aborted for C:mhoversfortran_c_test2fortran_c_test.f90 (code 1)

The c_code_test routine matches the real c code I am trying to interface, so any changes to make this work need to be in the calling fortran code.

thanks,

0 Kudos
Reply