CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2011
    Posts
    2

    Incorporating C in C++

    Hi all! I am trying to use an open source C library to process STDF files. I need to get the output from that library and transform it into SQL. I kept getting the error as shown below:

    Code:
    error C3861: 'stdf_open': identifier not found
    error C3861: 'stdf_read_record': identifier not found
    The functions are used in a CPP file while the actual definitions of the functions are in the C library. I have checked for the function definition in the header file and its linked to ANOTHER C file using the #include.

    I am running this project in MS VC++ 2010 Express. Can anyone help me figure out this error and how to solve it? I have attached the project along in this thread. Any help is greatly appreciated.
    Attached Files Attached Files

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Incorporating C in C++

    First, are those compiler or linker errors?

    Second, those C header files were not designed to be placed in a C++ module just like that. The reason is that in C, function names are not mangled by the compiler, while C++, they are mangled.

    You need to edit those header files and do this:
    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    // the function declarations
    //...
    #ifdef __cplusplus
    }
    #endif
    The reason why you need to do this is that C++ mangles function names when code is generated, since in C++, you can have two different functions that have the same name and different arguments. In 'C', this cannot happen as overloaded functions do not exist.

    So for example, if you have a C++ function called "foo(int)", the compiler will generate a mangled name: (this is just made up, but just to illustrate a point):
    Code:
    foo@int
    This is to distinguish this foo() function from, say, a function called foo(double), which would have a different mangled name, (again made up)
    Code:
    foo@double
    So what winds up happening when you link your C++ program to a 'C' library is that the linker is looking for foo@int, but the C library has just "foo" as the function. To make sure the compiler doesn't mangle the name, you have to use the
    Code:
    extern "C"
    directive.

    Regards,

    Paul McKenzie

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Incorporating C in C++

    Quote Originally Posted by Paul McKenzie View Post
    You need to edit those header files and do this:
    Strictly speaking, editing the headers is not necessary. This will work as well, although it may be non-optimal if they're included many places:

    main.cpp
    Code:
    extern "C" {
    #include "myClibrary.h"
    }

  4. #4
    Join Date
    Oct 2011
    Posts
    2

    Re: Incorporating C in C++

    Hi guys,

    thanks for giving suggestions. I appreciate that. But it seems that both your suggestions does not work. Is there any other settings in VC++ Express 2010 that I should be aware of?

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Incorporating C in C++

    Quote Originally Posted by chanjw View Post
    Hi guys,

    thanks for giving suggestions. I appreciate that. But it seems that both your suggestions does not work.
    Oh, they work.
    Is there any other settings in VC++ Express 2010 that I should be aware of?
    Why figure out another setting (which doesn't exist), when you were told what the issue is? But that is just one of your problems. Let's look at this one:
    Code:
    error C3861: 'stdf_open': identifier not found
    error C3861: 'stdf_read_record': identifier not found
    Are these compiler errors? If so, then the issue should be obvious -- you are calling the functions stdf_open and stdf_read in that C++ module, but those functions were not declared. There is no setting that fixes a compiler error -- you have to fix the error by making sure those functions are declared.

    Simple program:
    Code:
    int main()
    {
        a();
    }
    Try to compile this as a C++ module (give the file a .CPP extension)-- you will receive the same error, since function a() was never declared. Now, change the extension to .C, and it will compile successfully.

    In C, you don't have to declare functions before using them, but in C++, you must declare all your functions before using them, else you get a compiler error. So are you compiling a module that used to be a 'C' module that is now being compiled as a C++ module? If you are, then again, that is another issue.

    The bottom line is that if you want to use 'C' in a C++, you better know the 'C' and C++ languages. Knowing one language doesn't mean you know the other. Both languages have subtle differences, but those differences become magnified when you are trying to compile what used to be a straight 'C' module using a C++ compiler. If the original code was written without any concern for whether a C++ compiler understands it, that makes the code susceptible to compiler errors similar to what you're seeing now.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 1st, 2011 at 05:11 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured