首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

头文件被包孕的顺序

2013-01-21 
头文件被包含的顺序?头文件被包含的顺序?想定义一个宏,然后根据这个宏是否被定义来个条件编译,问题来了:怎

头文件被包含的顺序?
头文件被包含的顺序?

想定义一个宏,然后根据这个宏是否被定义来个条件编译,问题来了:怎么保证编译这个条件编译的时候,定义宏的头文件一定被包含了呢?   如果在定义这个宏的头文件之前使用了条件编译,就达不到想要的效果了。

所以,这个宏应该定义在哪个头文件里呢?工程里文件很多的话,怎么确定这样的宏应该写在哪里?
[解决办法]
不想在头文件里定义,那就 ,工程->项目属性->通用配置->C/C++ ->预处理器->预处理器定义,
把你的宏仍在那里就行。 
[解决办法]
CoCreateGuid
Creates a GUID, a unique 128-bit integer used for CLSIDs and interface identifiers.

HRESULT CoCreateGuid(
  GUID  *pguid  //Pointer to the GUID on return
);
 
Parameter
pguid 
[out] Pointer to the requested GUID on return. 
Return Value
S_OK 
The GUID was successfully created. 
Win32 errors are returned byUuidCreate but wrapped as an HRESULT.

Remarks
The CoCreateGuid function calls the RPC function UuidCreate, which creates a GUID, a globally unique 128-bit integer. Use the CoCreateGuid function when you need an absolutely unique number that you will use as a persistent identifier in a distributed environment.To a very high degree of certainty, this function returns a unique value – no other invocation, on the same or any other system (networked or not), should return the same value.


[解决办法]

引用:
头文件被包含的顺序?

想定义一个宏,然后根据这个宏是否被定义来个条件编译,问题来了:怎么保证编译这个条件编译的时候,定义宏的头文件一定被包含了呢?   如果在定义这个宏的头文件之前使用了条件编译,就达不到想要的效果了。

所以,这个宏应该定义在哪个头文件里呢?工程里文件很多的话,怎么确定这样的宏应该写在哪里?
              
           ……


如果楼主真的有这用需要求的话,就要检查需要的宏定义是否定义,然后检查宏的值是多少。
must.h
#define MACRO_MUST 1

prog.c里

#include "must.h"

#if defined(MACRO_MUST) && (MACRO_MUST == 1)
/* code for MACRO_MUST == 1 */
#elif defined(MACRO_MUST) && (MACRO_MUST == 0)
/* code for MACRO_MUST = 0 */
#else
#error "must.h must be included"
#endif

这样的话如果一个.c没有包含must.h,编译时就会有提示啦。
[解决办法]
引用:
我试验了下,或许编译程序obj没有先后顺序,所以如果要使用这个定义宏,所有用这宏进行条件编译的cpp必须都直接或者间接包含这个定义宏的.h文件,没有obj都是独立编译的?编译好的obj是怎么生成.exe的呢,看了装载链接和库还是不太明白啊

obj到exe那时连接器的工作了,不懂也没什么,建议LZ看一下我的一篇博客,讲的是条件编译的区别:
http://blog.csdn.net/zlhy_/article/details/8192580 也许有帮助的

热点排行