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):
This is to distinguish this foo() function from, say, a function called foo(double), which would have a different mangled name, (again made up)
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
directive.
Regards,
Paul McKenzie
Re: Incorporating C in C++
Quote:
Originally Posted by
Paul McKenzie
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"
}
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?
Re: Incorporating C in C++
Quote:
Originally Posted by
chanjw
Hi guys,
thanks for giving suggestions. I appreciate that. But it seems that both your suggestions does not work.
Oh, they work.
Quote:
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