Visual Studio 工程C++程序中添加.c.h的方法

方法一、
1、把要添加的.c和.h文件复制到c++程序目录中。
2、把.c文件改为.cpp文件 (预编译头可用,可不用)
3、把.cpp 和 .h文件都添加到该项目中 就可以使用了
 
ps:
直接Add files也行;
copy的时候如果.c 和 .h文件不在项目root目录里,则在项目-右键属性-配置属性-VC++目录,在包含目录中添加.c 和 .h的路径
 
方法二、
1、 项目配置不使用预编译头
  1. In the Solution Explorer pane of the project, right-click the project name, and then click Properties.

  2. In the left pane, click the C/C++ folder.

  3. Click the Precompiled Headers node.

  4. In the right pane, click Create/Use Precompiled Header, and then click Not Using Precompiled Headers.

2、.c文件加载到程序需中
3、把c语言 函数的声明用extern "C"{};
 
最好的写法是:
//------test.h----------
#ifdef __cplusplus
 extern "C" {
#endif
 
//c语言函数声明写在这
 
#ifdef __cplusplus
}
#endif
 
//------test.c ------------
#ifdef __cplusplus
 extern "C" {
#endif
 
#include "test.h"
 
//.c文件内容
 
#ifdef __cplusplus
}
#endif
 
========================================
h文件:
 
C++程序中添加.c.h的方法
 
C文件:
C++程序中添加.c.h的方法
 
路径添加:

C++程序中添加.c.h的方法
 
文件添加:
C++程序中添加.c.h的方法

 
编译即可~

猜你喜欢

转载自blog.csdn.net/lm393485/article/details/88892361
今日推荐