From: kidari on
hello. all fortran experts!

please help me.

i am trying to call a c lib from fortran. (file 1 and 2 are for c
library and file 3 is for fortran.)

file 1 == 'ac_lib_static.h':

#ifndef _AC_LIB_STATIC_H_
#define _AC_LIB_STATIC_H_

extern int ADD(int*, int*, int*);

#endif // _AC_LIB_STATIC_H_
----------------------------------------------------
file 2 ==test_c.c:

#include "ac_lib_static.h"
int ADD(int* a, int* b, int* answer)
{
int ans;
ans = (*a+*b);
*answer = ans;
*answer = (*a+*b);
return(1);
}
---------------------------------------------------
Then i did the following.

File>NewProject>VisualC++>Win32>Win32 Console Application

under Win32 Application Wizard
ApplicationType:StaticLibrary
no precompiled header

I added c file in "SourceFiles" and h file in "Header File"
and compile to get a lib file. No errors!

Next to compile fortran.

file 3: test_f.f90:

program Console1
implicit none

! Variables
INTEGER :: A = 1
INTEGER :: B = 3
INTEGER :: AN = 0

INTERFACE
SUBROUTINE ADD(A, B, AN)
INTEGER A, B, AN
END SUBROUTINE
END INTERFACE

! Body of Console1
print *, 'Hello World'

CALL ADD(A, B, AN)
PRINT *, AN

end program Console1
-----------------------------------------------------------

i got the following errors when i compiled this fortran file after I
put the directory and lib file name info to
"Linker>Input>AdditionalDependencies" under project properties.


Linking...
Link /OUT:&quotDebug\test_f.exe" /INCREMENTAL:NO /NOLOGO /MANIFEST /
MANIFESTFILE:&quotC:\Documents and Settings\syoon\My Documents\My
Fortran\EXAMPLE\test_f\test_f\debug\test_f.exe.intermediate.manifest" /
DEBUG /PDB:&quotC:\Documents and Settings\syoon\My Documents\My Fortran
\EXAMPLE\test_f\test_f\debug\test_f.pdb" /SUBSYSTEM:WINDOWS /
IMPLIB:&quotC:\Documents and Settings\syoon\My Documents\My Fortran
\EXAMPLE\test_f\test_f\debug\test_f.lib" &quotC:\Documents and Settings
\syoon\My Documents\My Fortran\EXAMPLE\test_c\debug\test_c.lib"
&quotDebug\test_f.obj"

Link: executing 'link'
test_c.lib(test_c.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(a)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(a)ABV0@@Z) already defined in
LIBCMTD.lib(typinfo.obj)

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

Debug\test_f.exe : fatal error LNK1169: one or more multiply defined
symbols found


test_f - 3 error(s), 2 warning(s)


could you guys tell me what causes my problem?

did i miss some setting?
From: glen herrmannsfeldt on
kidari <kidariyoon(a)gmail.com> wrote:

> i am trying to call a c lib from fortran. (file 1 and 2 are for c
> library and file 3 is for fortran.)

(snip)

Note that ADD is a function returning int, not void.

> #include "ac_lib_static.h"
> int ADD(int* a, int* b, int* answer)
> {
> int ans;
> ans = (*a+*b);
> *answer = ans;
> *answer = (*a+*b);
> return(1);
> }

(snip)

> INTERFACE
> SUBROUTINE ADD(A, B, AN)
> INTEGER A, B, AN
> END SUBROUTINE
> END INTERFACE

Likely completely unrelated, but this should be:

INTEGER FUNCTION ADD(A, B, AN)

or the C function should return void.

(snip)

> i got the following errors when i compiled this fortran file after I
> put the directory and lib file name info to
> "Linker>Input>AdditionalDependencies" under project properties.


> Link: executing 'link'
> test_c.lib(test_c.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(a)ABV0@@Z) already defined in LIBCMTD.lib(typinfo.obj)

MSVCRT is, in some form or other, the C Run Time library.

> MSVCRTD.lib(ti_inst.obj) : error LNK2005: &quotprivate: class
> type_info & __thiscall type_info::operator=(class type_info const
> &)" (??4type_info@@AAEAAV0(a)ABV0@@Z) already defined in
> LIBCMTD.lib(typinfo.obj)

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

I think it means that you have two libraries with the same routine,
possibly the C library and Fortran library.

> Debug\test_f.exe : fatal error LNK1169: one or more multiply defined
> symbols found

Someone who understands Visual Studio better than I do will have
to answer, but, at least before Fortran 2003, libraries were the
biggest problem with mixing Fortran and C. Especially regarding I/O,
you might find that each buffers its I/O separately and the result
doesn't come out in the order you extect.

-- glen
From: Steve Lionel on
On 5/20/2010 7:46 PM, kidari wrote:

> i am trying to call a c lib from fortran. (file 1 and 2 are for c
> library and file 3 is for fortran.)

> i got the following errors when i compiled this fortran file after I
> put the directory and lib file name info to
> "Linker>Input>AdditionalDependencies" under project properties.
>
>
> MSVCRTD.lib(ti_inst.obj) : error LNK2005:&quotprivate: __thiscall
> type_info::type_info(class type_info const&)" (??
> 0type_info@@AAE(a)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(a)ABV0@@Z) already defined in
> LIBCMTD.lib(typinfo.obj)
>
> LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of
> other libs; use /NODEFAULTLIB:library

You have what we call "Multiple C Library Syndrome". It is caused by
having different settings for "runtime library" type in your C and
Fortran builds. You didn't say which Fortran you're using, but I'm
going to guess that it is Intel Visual Fortran. Even if it isn't, this
advice still applies.

Current Microsoft Visual C++ defaults Debug configurations to
"Multithreaded Debug DLL (/MDd)" for Code Generation > Runtime Library,
whereas Intel Fortran defaults to "Multithreaded Debug (/MTd)", a static
library (property is Fortran > Libraries > Use Runtime Library).

All you should need to do is adjust one project's setting to match the
other - it doesn't really matter which you choose to change. You'll
also want to change the Release configurations to match as well (don't
choose the Debug libraries for a Release configuration.)

If you are using Intel Visual Fortran (or Compaq Visual Fortran), you
can get more specific help in our user forum at
http://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/

--
Steve Lionel
Developer Products Division
Intel Corporation
Nashua, NH

For email address, replace "invalid" with "com"

User communities for Intel Software Development Products
http://software.intel.com/en-us/forums/
Intel Software Development Products Support
http://software.intel.com/sites/support/
My Fortran blog
http://www.intel.com/software/drfortran