C++ 调C C调c++
一、c++ 调C:
/* c语言头文件:cExample.h */
#ifndef C_EXAMPLE_H
#define C_EXAMPLE_H
#ifdef __cplusplus
extern "C"
{
#endif
int add(int x,int y);
#ifdef __cplusplus
}
#endif
#endif
/* c语言实现文件:cExample.c */
#include "cExample.h"
int add( int x, int y )
{
return x + y;
}
// main.cpp
#include <iostream>
using namespace std;
// c++实现文件,调用add()
#include "cExample.h"
int main(int argc, char* argv[])
{
cout << add(2,3) << endl;
return 0;
}
二、C调C++
//C++头文件 cppExample.h
#ifndef CPP_EXAMPLE_H
#define CPP_EXAMPLE_H
#ifdef __cplusplus
extern "C"
{
#endif
int add(int x,int y);
#ifdef __cplusplus
}
#endif
#endif
//C++实现文件 cppExample.cpp
#include "cppExample.h"
int add( int x, int y )
{
return x + y;
}
/* C实现文件 */
#include <stdio.h>
#include "cppExample.h"
int main( int argc, char* argv[] )
{
printf ("%d\n", add( 2, 3 ));
return 0;
}
相关文档:
这个方块游戏是用linux终端的光标控制、颜色设置做的
(添了个功能,字母P暂停、恢复游戏)
用 A S D W 控制移动、转向,空格键下坠到底;
linux的异步aio函数解决了很多麻烦;
用了个简单的模板单例模式,继承它就可以;
对POSIX线程简单封装成java线程接口;
#include <memory>
#include "Tetris.h"
#include ......
C中的#号不仅是#include,#define等的关键字首使用而已,#在宏中还有很多用法。具体请看下面我来举例
1.双#号的宏用法。
我相信双##号的用法应该大部分人都见过吧,主要是用来连接宏变量名使用,在GCC参考手册中这样记载。
可用于宏内部将两个源代码权标连接成一个的连接指示字,可用来构造不会被解析器错误解释的名字。 ......
数组赋值我总结一下吧也就三种,那char的来举例:
定义的时候直接赋值.
1:char a[20] = "Hello World!";
2: char a[20];
strcpy(a, "Hello World!");
3:char a[20] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'};
常见错误赋值方式:
1:char a[20];
a = "Hello World ......
C/C++ development with the Eclipse Platform
Pawel Leszek
摘要:通过本文你将获得如何在Eclipse平台上开发C/C++项目的总体认识。虽然Eclipse主要被用来开发Java项目,但它的框架使得它很容易实现对其他开发语言的支持。在这篇文章里,你将学会如何使用CDT(C/C++ Development Toolkit),一个在Eclipse平台上最 ......
作者:Kevin Lynx 来源:C++博客
转自:http://www.kuqin.com/language/20080319/4797.html
众多C++书籍都忠告我们C语言宏是万恶之首,但事情总不如我们想象的那么坏,就如同goto一样。宏有
一个很大的作用,就是自动为我们产生代码。如果说模板可以为我们产生各种型别的代码(型别替换),
那么宏其实可以为我们在符号上 ......