// nx003_c.cpp : Defines the entry point for the console application.
//在C++工程里加入C文件
#include "nx003_c.h"
int main(int argc, char* argv[])
{
func1();
func2();
cout<<"test"<<endl;
return 0;
}
===========================================
nx003_c.h
extern "C"
{
#include "test.h"
}
#include "test2.h"
===========================================
test.h
#ifndef _TESTH_
#define _TESTH_
#include <stdio.h>
void func1();
#endif
===========================================
test.c
#include "test.h"
void func1()
{
printf("enter here !/r/n");
return;
}
===========================================
test2.h
#ifndef _TEST2H_
#define _TEST2H_
#include "iostream"
using namespace std;void func2();
#endif
===========================================
test2.cpp
#include "test2.h"
void func2()
{
printf("2 enter here !/r/n");
return;
}
===========================================
map文件:
0001:000014a0 _func1 004024a0 f test.obj
0001:00001710 ?func2@@YAXXZ 00402710 f test2.obj
:)
本文展示了如何在C++工程中引入并使用C语言编写的源文件。通过`extern "C"`来处理C++的名称修饰问题,使得C函数可以在C++中调用。示例中包括了`nx003_c.cpp`主文件,它包含`nx003_c.h`头文件,而头文件中引用了`test.h`和`test2.h`。`test.h`定义了C函数`func1()`,`test.c`实现了这个函数,`test2.h`则定义了C++的`func2()`,并使用了`iostream`库。
625

被折叠的 条评论
为什么被折叠?



