代码之家  ›  专栏  ›  技术社区  ›  JXG Jasmynn Flores

如何严格执行一个定义?

  •  1
  • JXG Jasmynn Flores  · 技术社区  · 17 年前

    给予 C

    我已经可以做到:

        #if defined PROJA
        (blah blah blah)
        #elif defined PROJB
        (etc)
        #else
        #error "No project defined"
        #endif
    

    不过,所做的只是告诉我是否定义了0个项目。如果某个有用的灵魂同时定义了项目A和项目B,预处理器将只假设项目A。然而,从我的角度来看,正确的行为是标记错误。

    诚然,由于只定义了2个项目,这个问题是微不足道的。我如何用200解决它?

    6 回复  |  直到 10 年前
        1
  •  3
  •   Shay Erlichmen    17 年前

    大概是这样的:

    #if defined PROJA
      #ifdef HAVE_PROJ
        #error 
      #endif
    
      #define HAVE_PROJ
    #endif
    
    #if defined PROJB
      #ifdef HAVE_PROJ
        #error 
      #endif
    
      #define HAVE_PROJ
    #endif
    
    #ifndef HAVE_PROJ
      #error No project selected (you need to define PROJA, PROJB, or ...)
    #endif
    
        2
  •  2
  •   Aiden Bell    17 年前

    include_proja.h
    include_projc.h
    

    然后使用Makefile或其他任何东西来包含正确的文件。然后,您可以使用代码生成200个不同的文件,并在编译时包含正确的文件。

    这就是构建系统的目的 . 如果你正在用宏做这样奇怪的事情。。。在源代码之外找到更好的方法。

    每个文件都可以(请原谅这里的冗长)

    #define A_PROJECT_INCLUDE_WAS_INCLUDED
    

    然后呢

    #ifndef A_PROJECT_INCLUDE_WAS_INCLUDED
        #error "No project include"
    #endif
    

    祝你好运

        3
  •  2
  •   Markus Schnell    17 年前

    #define ENV_UNKNOWN 0
    #define ENV_MACOSX  1
    #define ENV_LINUX   2
    #define ENV_WIN32   3
    /* and so on */
    
    
    #ifndef ENVIRONMENT
    /* no environment given, default to something (perhaps) */
    #define ENVIRONMENT ENV_UNKNOWN
    #endif
    
    /* and now the environment specific parts */
    #if (ENVIRONMENT == ENV_MACOSX)
    #include "macosx_port.h"
    #endif
    
    #if (ENVIRONMENT == ENV_LINUX)
    #include "linux_port.h"
    #endif
    
    #if (ENVIRONMENT == ENV_WIN32)
    #include "win32_port.h"
    #endif
    
    #if (ENVIRONMENT == ENV_UNKNOWN)
    #error You have to specify the ENVIRONMENT.
    #endif
    

    现在,您可以在命令行上指定要编译的环境,如下所示:

    cc -DENVIRONMENT=2 ...
    

    另一种方法是根据编译环境,包含/链接构建系统中的不同模块。

        4
  •  2
  •   Daniel Daranas    17 年前
    #if defined PROJA
    bool one_project_defined = true;
    #endif
    
    #if defined PROJB
    bool one_project_defined = true;
    #endif
    
    #if defined PROJC
    bool one_project_defined = true;
    #endif
    
    #if defined PROJD
    bool one_project_defined = true;
    #endif
    
    one_project_defined; // Won't compile in wrong builds
    
        5
  •  0
  •   Jacob B    17 年前

    在这种情况下,您只需要一些表达式就可以给出编译时错误。也许:

    #ifdef PROJA
    #define PROJA-TEST "
    #else
    #define PROJA-TEST ""
    #endif
    

    以此类推,B、C等。

    然后:

    const char *test_only_one_project = PROJA-TEST PROJB-TEST PROJC-TEST " "More than one project is defined!";
    

    编辑:。。。当然,这只是测试定义了奇数个项目。但这应该是可行的:

    #ifdef PROJA
    #define PROJA-TEST (
    #endif
    

    那么,等等

    const char *test_only_one_project = PROJA-TEST PROJB-TEST PROJC-TEST "More than one project is defined!" );
    
        6
  •  -1
  •   hiena    17 年前

    也许你可以做如下事情:

    #if PROJ==A
    (blah blah blah)
    #elif PROJ==B
    (etc)
    #else
    #error "No project defined"
    #endif