DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Vinod Guest

    MFC requires C++ compilation (use a .cpp suffix) ????


    Hi I have some code in .c and and some in .cpp which uses MFC. I am getting
    following error "fatal error C1189: #error : MFC requires C++ compilation
    (use a .cpp suffix)" in My ".c" files. I am including a ".h" header file
    which has "#include stdafx.h" . If I remove this "#include stdafx.h" from
    header compilation and linking is fine. I cannot rename my .c file to .cpp
    . Since there are some callback functions which are in .cpp files called
    by .c files, I am having this problem. Please suggest me what can I do to
    resolve this problem.

    Thanks,
    Vinod


  2. #2
    ralph Guest

    Re: MFC requires C++ compilation (use a .cpp suffix) ????


    "Vinod" <vkushwaha@hotmail.com> wrote:
    >
    >Hi I have some code in .c and and some in .cpp which uses MFC. I am getting
    >following error "fatal error C1189: #error : MFC requires C++ compilation
    >(use a .cpp suffix)" in My ".c" files. I am including a ".h" header file
    >which has "#include stdafx.h" . If I remove this "#include stdafx.h" from
    >header compilation and linking is fine. I cannot rename my .c file to .cpp
    >. Since there are some callback functions which are in .cpp files called
    >by .c files, I am having this problem. Please suggest me what can I do to
    >resolve this problem.
    >
    >Thanks,
    >Vinod
    >


    A compiler directive which will soon become your friend -> extern "C"

    The compiler will automatically compile your 'c' modules (.c) as "C" code
    and .cpp files as "C++" code. The problem comes in telling the c++ about
    the c-function in the c-modules with the c-headers. You do that by telling
    the cpp compiler/linker that the linkage specification for these functions
    are "C".

    You need to bracket the C code/function/headers with the extern "C" directive.

    <Out of the Manual>
    C++ uses the same calling convention and parameter-passing techniques as
    C, but naming conventions are different because of C++ decoration of external
    symbols. By causing C++ to drop name decoration, the extern "C" syntax makes
    it possible for a C++ module to share data and routines with other languages.

    The following example declares prn as an external function using the C naming
    convention. This declaration appears in C++ source code.

    extern "C"
    {
    void prn();
    }

    and or

    extern "C"
    {
    #include "myCFile.h"
    }

    This will solve the problem for most situations.

    It was my intention to send you the URL for Mixed-Language Programming but
    M$ has moved stuff on their site around and I can't find it. If you have
    your MSDN disk do a search for "Mixed-Language Programming".

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links