PRB: putchar Fails in C Code Called from FORTRAN Application

Last reviewed: August 7, 1997
Article ID: Q118396
1.00 4.00 WINDOWS NT kbtool

The information in this article applies to:

  • Microsoft FORTRAN PowerStation 32 for Windows NT, version 1.0 and 4.0

SYMPTOMS

In a mixed-language program, compiling the FORTRAN code using the /MD option prevents the C Run Time function "putchar" from operating.

For Fortran PowerStation 4.0, the following warning message is generated:

   LINK: warning LNK4098: defaultlib "LIBC" conflicts with use of other
   libs; use /NODEFAULTLIB:library

CAUSE

This behavior is by design. You cannot safely mix objects built for different program execution models. In this case, putchar('c') is a macro that expands to putc(c,stdout) and stdout is defined differently in MSVCRT.LIB than it is in the statically linked libraries.

RESOLUTION

To work around this problem, either

  • Use the /MD option on the C code as well.

    -or-

  • Compile the Fortran code using /MT instead of using /MD.

MORE INFORMATION

To generate the problem, compile the C code using Visual C++ 32-bit Edition and compile the FORTRAN program using option /MD. Include the C object module on the compile line so that it is passed to the linker. The command lines used are as follows:

   CL -c C_SAMPLE.C
   FL32 -MD F_SAMPLE.FOR C_SAMPLE.OBJ

Run the sample code. It fails to display the letter "c" after "in the dll".

The program will run without error if the FORTRAN program is compiled using the /MT option instead of the /MD option.

Sample Code: FORTRAN

C Compile options needed: /MD

      INTERFACE TO SUBROUTINE csub [C,ALIAS:'_csub']()
      END
      WRITE(*,*) 'In the main'
      CALL csub()
      END

Sample Code: C

/* Compile options needed: /c
*/

   #include <stdio.h>
   #include <string.h>

   #define DLLexport _declspec(dllexport)

   DLLexport void csub();
   void csub()
   {
      printf("\nIn the dll\n");
      putchar('c');
   }


Additional reference words: 1.00 4.00
KBCategory: kbtool kbprb
KBSubcategory: FL32Iss
Keywords : FL32Iss kbprb kbtool
Version : 1.00 4.00
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 7, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.