C 如何正确使用#include
C++ how to properly use #include?
我想在我的main.cpp程序中包含printstuff.h。我遇到了没有这样的文件或目录错误。我不想将整个目录带有双重报价。我只想简单地放置printStuff.h如何完成此操作?
我正在使用Visual Studio 2012。
main.cpp
#include<iostream>
#include<cstdlib>
#include<printstuff.h>
using namespace std;
inline void swap( int *x, int *y ) {
int *z = x;
*x = *y;
*y = *z;
}
int main( ) {
/*int x = 0, y = 1;
swap( x, y );
cout << x << endl << y << endl;*/
printStuff( );
system( "pause" );
return 0;
}
printstuff.h
#include<iostream>
using namespace std;
void printStuff( );
void printStuff( ) {
int count[ ] = { 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
char symb[ ] = "abcdefghijk";
for ( int j = 1; j < 12; j++ ) {
char c = symb[ j ];
for ( int i = 0; i < 11; i++ ) {
int times = count[ i ];
while ( times != 0 ) {
cout << c;
times--;
}
cout << endl;
}
}
}
在项目的属性页面中,将'printStuff.h'的路径放在'其他包含目录'中。这样,您可以通过#include <printstuff.h>
所以,假设" some.h"文件路径在'c: ref include'中,您的项目路径在'c: projection'中。
- 如果将" C: ref include"的路径放在"附加目录"中,则可以使用
#include <some.h>
。 - 如果您不采用路径,则必须使用相对路径,例如
#include "..refincludesome.h"
。
您可以通过告诉Visual Studio在哪里搜索包括指令。
要右键单击解决方案资源管理器的项目,然后选择属性。
在属性中编辑配置属性>包括指令。添加您的目录放置标题文件的位置。
查看快照。显示的图像包括指令定制选项
//printstuff.h
#ifndef ADD_H_INCLUDED
#define ADD_H_INCLUDED
void printStuff( );
#endif
//printstuff.cpp
#include<printstuff.h>
void printStuff( ) {
int count[ ] = { 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
char symb[ ] = "abcdefghijk";
for ( int j = 1; j < 12; j++ ) {
char c = symb[ j ];
for ( int i = 0; i < 11; i++ ) {
int times = count[ i ];
while ( times != 0 ) {
cout << c;
times--;
}
cout << endl;
}
}
}
//main.cpp
#include <iostream>
#include <cstdlib>
#include <printstuff.h>
using namespace std;
inline void swap( int *x, int *y ) {
int *z = x;
*x = *y;
*y = *z;
}
int main( ) {
/*int x = 0, y = 1;
swap( x, y );
cout << x << endl << y << endl;*/
printStuff( );
system("PAUSE");
return 0;
}
如果您只想添加printstuff.h添加您的目录中的目录:右键单击您的项目 ->属性 -> c/c ->常规 ->添加includnal include directory
然后添加您的目录,然后#include header;)
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- CMake-按正确顺序将项目与C运行时对象文件链接
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 用于访问容器<T>数据成员的正确 API
- 既然存在危险,为什么项目要使用-I include开关
- 如何使用Luacneneneba API正确读取字符串和表参数
- 如何使用用户输入在C++中正确填充2D数组
- node-gyp 在 macOS 上未正确链接库
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何在 C 中正确使用 libiconv 使其不会报告"Arg list too long"?
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 为什么我的for循环不能正确获取argv
- 如何在 c++ 中正确指定 #include 路径以使程序可移植
- #include < regex.h> 用于创建正确正则表达式的问题
- C 如何正确使用#include
- 如何包含正确的:<SDL2/SDL.h> 或 <SDL.h> 并设置 -I/usr/include/SDL2?
- 为最简单的项目生成的EclipseCDT构建并没有正确设置include路径
- swig接口文件的正确sstream include路径是什么
- 正确的#include指令格式
- 尽管路径设置正确,但错误"C1083 cannot open include file gecode/int.hh"