编译器就像没有定义预处理程序指令一样

Pil*_*pel 0 c++

我有一个头文件及其cpp文件(Error.h,Error.cpp).cpp文件对预处理程序指令执行检查,但始终失败.

Error.h:

/*
Optional macros:
AE_EXIT_AT_ERROR
AE_CONSOLE_WRITE_AT_ERROR
*/
#pragma once

extern void aeError(const char *str, int code=1);

extern void aeAssert(bool b, const char *failStr = "assertion failed");
Run Code Online (Sandbox Code Playgroud)

Error.cpp:

#include "Error.h"
#include <stdexcept>
#ifdef AE_CONSOLE_WRITE_AT_ERROR
#include <iostream>
#endif

void aeError(const char *str, int code)
{
    #ifdef AE_CONSOLE_WRITE_AT_ERROR
    std::cout << str << std::endl;
    #endif

    throw std::runtime_error(str);

    #ifdef AE_EXIT_AT_ERROR
    std::exit(code);
    #endif
}

void aeAssert(bool b, const char *failStr)
{
    if(!b)
        aeError(failStr);
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中:

//define both macros:
#define AE_CONSOLE_WRITE_AT_ERROR
#define AE_EXIT_AT_ERROR
#include "Error.h"

//rest of code
//...
Run Code Online (Sandbox Code Playgroud)

两者std::cout << str << std::endl;std::exit(code);没有得到编译(我为"手动"检查,虽然他们也标志着由IDE,这是VS2010灰色).

可能是什么原因造成的?

Ant*_*vin 7

main.cpp并且Error.cpp是不同的翻译单位.您只为其定义宏main.cpp,而不是Error.cpp.

您应该将#define指令放在.cpp文件包含的头文件中,或者在项目设置/ makefile中定义这些宏.