C/C++笔试题目常见题目
C++引用C函数例子工程中包含的三个文件的源代码如下:
/* c语言头文件:cExample.h */
#ifndef C_EXAMPLE_H
#define C_EXAMPLE_H
extern int add(int x,int y);
#endif
/* c语言实现文件:cExample.c */
#i nclude "cExample.h"
int add( int x, int y )
{
return x + y;
}
// c++实现文件,调用add:cppFile.cpp
extern "C"
{
#i nclude "cExample.h"
}
int main(int argc, char* argv[])
{
add(2,3);
return 0;
}
如果C++调用一个C语言编写的.DLL时,当包括.DLL的头文件或声明接口函数时,应加extern "C" { }
。
(2)在C中引用C++语言中的函数和变量时,C++的头文件需添加extern "C",但是在C语言中不能直接
引用声明了extern
"C"的该头文件,应该仅将C文件中将C++中定义的extern "C"函数声明为extern类型。
C引用C++函数例子工程中包含的三个文件的源代码如下:
//C++头文件 cppExample.h
#ifndef CPP_EXAMPLE_H
#define CPP_EXAMPLE_H
extern "C" int add( int x, int y );
#endif
//C++实现文件 cppExample.cpp
#i nclude "cppExample.h"
int add( int x, int y )
{
return x + y;
}
/* C实现文件 cFile.c
/* 这样会编译出错:#i nclude "cExample.h" */
extern int add( int x, int y );
int main( int argc, char* argv[] )
{
add( 2, 3 );
return 0;
}
15题目的解答请参考《C++中extern “C”含义》
16. 关联、聚合(Aggregation)以及组合(Composition)的区别?
- 上一篇:百度公司的笔试题目
《C/C++笔试题目常见题目》相关文章
- C/C++笔试题目常见题目
- › 应聘职位名称 —— Accounting and Finance(会计与财务部分)
- › ACCOUNTANT(General)
- › An accounting position about financial
- › Resume accounts 会计行业英文简历应届生
- › 英文简历2--accounting-cl.doc
- › 英文简历(会计师)ACCOUNTANT(General)
- › 会计师英文简历ACCOUNTANT(General)
- › 专家写的英文简历模板ACCOUNTANT(General)
- › 会计人员英文简历Accounting Manager
- › 会计Accountant笔试题
- › cicc MRM 笔经
- › 埃森哲accenture笔试题型
- 在百度中搜索相关文章:C/C++笔试题目常见题目
- 在谷歌中搜索相关文章:C/C++笔试题目常见题目
- 在soso中搜索相关文章:C/C++笔试题目常见题目
- 在搜狗中搜索相关文章:C/C++笔试题目常见题目