Linkage to Non-C++ Functions C functions and data can be accessed only if they are previously declared as having C linkage. However, they must be defined in a separately compiled translation unit.Syntaxlinkage-specification :extern string-literal { declaration-listopt } extern string-literal declarationdeclaration-list :declaration declaration-list declarationMicrosoft C++ supports the strings "C" and "C++" in the string-literal field. The following example shows alternative ways to declare names that have C linkage:// Declare printf with C linkage. extern "C" int printf( const char *fmt, ... );// Cause everything in the header file "cinclude.h" // to have C linkage. extern "C" { #include <cinclude.h> }// Declare the two functions ShowChar and GetChar // with C linkage. extern "C" { char ShowChar( char ch ); char GetChar( void ); }// Define the two functions ShowChar and GetChar // with C linkage. extern "C" char ShowChar( char ch ) { putchar( ch ); return ch; }extern "C" char GetChar( void ) { char ch; ch = getchar(); return ch; }// Declare a global variable, errno, with C linkage. extern "C" int errno;
Mixed-Language Programming with C++ Home | Overview | How Do IC++ 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(); }To call functions written in Fortran (or MASM), declare the function as you would in C and use a "C" linkage specification. For example, to call the Fortran function FACT from C++, declare it as follows:extern "C" { int __stdcall FACT( int n ); }The extern "C" syntax can be used to adjust a call from C++ to other languages, or to change the naming convention of C++ routines called from other languages. However, extern "C" can be used only from within C++. If the C++ code does not use extern "C" and cannot be changed, you can call C++ routines only by determining the name decoration and generating it from the other language. You can always determine the decoration by using the DUMPBIN utility. Use this approach only as a last resort, because the decoration scheme is not guaranteed to remain the same between versions.Use of extern "C" has some restrictions: You cannot declare a member function with extern "C". You can specify extern "C" for only one instance of an overloaded function; all other instances of an overloaded function have C++ linkage. For more information on the extern "C" linkage specification, see Linkage Specifications in C++ Language Reference.
to isdong(有些事情应该忘记):我当然知道它可以编译c的代码,我的意思是c++与c混合编程 比如用vc造个对话框,但是框里显示的一些数据是通过c与 单片机通讯获得的
to fang_jb(寂寞如雪):你的意思是,比如在一个基于对话框的工程中,我只要把一些与c 有关的头文件(如stdio)include上,我就能直接用这些c的库函数了? ( 如kbhit()) to isdong(有些事情应该忘记):呵呵,小弟系初学,对于dll之类的还不甚了解,以后在这些 还请多多指教
C functions and data can be accessed only if they are previously declared as having C linkage. However, they must be defined in a separately compiled translation unit.Syntaxlinkage-specification :extern string-literal { declaration-listopt }
extern string-literal declarationdeclaration-list :declaration
declaration-list declarationMicrosoft C++ supports the strings "C" and "C++" in the string-literal field. The following example shows alternative ways to declare names that have C linkage:// Declare printf with C linkage.
extern "C" int printf( const char *fmt, ... );// Cause everything in the header file "cinclude.h"
// to have C linkage.
extern "C"
{
#include <cinclude.h>
}// Declare the two functions ShowChar and GetChar
// with C linkage.
extern "C"
{
char ShowChar( char ch );
char GetChar( void );
}// Define the two functions ShowChar and GetChar
// with C linkage.
extern "C" char ShowChar( char ch )
{
putchar( ch );
return ch;
}extern "C" char GetChar( void )
{
char ch; ch = getchar();
return ch;
}// Declare a global variable, errno, with C linkage.
extern "C" int errno;
Mixed-Language Programming with C++
Home | Overview | How Do IC++ 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();
}To call functions written in Fortran (or MASM), declare the function as you would in C and use a "C" linkage specification. For example, to call the Fortran function FACT from C++, declare it as follows:extern "C" { int __stdcall FACT( int n ); }The extern "C" syntax can be used to adjust a call from C++ to other languages, or to change the naming convention of C++ routines called from other languages. However, extern "C" can be used only from within C++. If the C++ code does not use extern "C" and cannot be changed, you can call C++ routines only by determining the name decoration and generating it from the other language. You can always determine the decoration by using the DUMPBIN utility. Use this approach only as a last resort, because the decoration scheme is not guaranteed to remain the same between versions.Use of extern "C" has some restrictions: You cannot declare a member function with extern "C".
You can specify extern "C" for only one instance of an overloaded function; all other instances of an overloaded function have C++ linkage.
For more information on the extern "C" linkage specification, see Linkage Specifications in C++ Language Reference.
比如用vc造个对话框,但是框里显示的一些数据是通过c与
单片机通讯获得的
有关的头文件(如stdio)include上,我就能直接用这些c的库函数了?
( 如kbhit())
to isdong(有些事情应该忘记):呵呵,小弟系初学,对于dll之类的还不甚了解,以后在这些
还请多多指教