在构建期间仅在标头中显示#pragma消息一次

时间:2016-06-08 08:43:17

标签: c gcc c-preprocessor pragma

我在#pragma message("A message")标题中有一堆config.h个,它包含在项目的许多地方;目标是告知在构建期间选择哪些配置选项。标头受#ifndef #define样式标头保护。问题是每次包含此标题时,都会打印消息。有没有更好的方法来执行此操作,以便在构建期间只打印一次消息?

编辑:我了解构建选项通常是使用cmake,qmake,autotools等构建工具进行操作和查看的,但我不能真正选择构建工具到期根据项目的性质。

2 个答案:

答案 0 :(得分:0)

您可以将config.h放在一个单独的文件中,只包含/* config.h */ #ifndef CONFIG_H #define CONFIG_H #ifndef CONFIG_MESSAGE_PRINTED #define CONFIG_MESSAGE_PRINTED #include "config_message.h" #endif /* ... */ #endif /* CONFIG_H */ 一次。即使在错误的条件内,gcc也可能会打印这些pragma,但它不会在false条件中包含一个文件。有点像这样:

config_message.h

#pragma message("A message")

  File "/var/opt/igp_modules/Mod51_LAM/mod51_LAM.py", line 235, in process
    self.linesStructureCSV(inlist)
  File "/var/opt/igp_modules/Mod51_LAM/mod51_LAM.py", line 610, in linesStructureCSV
    from  line_analysis  import LAM
  File "/var/opt/igp_modules/Mod51_LAM/line_analysis.py", line 2, in <module>
    import pandas as pd
  File "/usr/lib64/python2.6/site-packages/pandas/__init__.py", line 44, in <module>
    from pandas.core.api import *
  File "/usr/lib64/python2.6/site-packages/pandas/core/api.py", line 9, in <module>
    from pandas.core.groupby import Grouper
  File "/usr/lib64/python2.6/site-packages/pandas/core/groupby.py", line 17, in <module>
    from pandas.core.frame import DataFrame
  File "/usr/lib64/python2.6/site-packages/pandas/core/frame.py", line 41, in <module>
    from pandas.core.series import Series
  File "/usr/lib64/python2.6/site-packages/pandas/core/series.py", line 2909, in <module>
    import pandas.tools.plotting as _gfx
  File "/usr/lib64/python2.6/site-packages/pandas/tools/plotting.py", line 28, in <module>
    import pandas.tseries.converter as conv
  File "/usr/lib64/python2.6/site-packages/pandas/tseries/converter.py", line 7, in <module>
    import matplotlib.units as units
  File "/usr/lib64/python2.6/site-packages/matplotlib/__init__.py", line 709, in <module>
    rcParams = rc_params()
  File "/usr/lib64/python2.6/site-packages/matplotlib/__init__.py", line 627, in rc_params
    fname = matplotlib_fname()
  File "/usr/lib64/python2.6/site-packages/matplotlib/__init__.py", line 565, in matplotlib_fname
    fname = os.path.join(get_configdir(), 'matplotlibrc')
  File "/usr/lib64/python2.6/site-packages/matplotlib/__init__.py", line 240, in wrapper
    ret = func(*args, **kwargs)
  File "/usr/lib64/python2.6/site-packages/matplotlib/__init__.py", line 439, in _get_configdir
    raise RuntimeError("Failed to create %s/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data"%h)
RuntimeError: Failed to create /root/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data

答案 1 :(得分:0)

受@fuz启发,也许我们可以在单独的#pragma message文件中写.c。 这是一个例子:

/* config.h */
#ifndef CONFIG_H
#define CONFIG_H

#define CONFIG_OPTION_A
#define CONFIG_OPTION_B

#endif /* CONFIG_H */

.c文件中写入消息:

/* config_msg.c */
#include "config.h"

#ifdef CONFIG_OPTION_A
#pragma message "Config option A is enabled!"
#endif

#ifdef CONFIG_OPTION_C
#pragma message "Config option C is enabled!"
#endif

最后将编译依赖项添加到Makefile(即config_msg.o):

/* Makefile */
.PHONY: all
%.o: %.c
    gcc -c -o $@ $<
config_msg.o: config_msg.c config.h
    gcc -c -o $@ $<
all: main.o config_msg.o
    gcc -o main $^

只要config.h被修改,config_msg.o将被重新编译并在其中打印消息。 (将config.h添加到Rule config_msg.o的依赖关系是必要的。这也取决于Rule config_msg.o的优先级高于Rule %.o。如果我错了,请纠正我。)

以下是测试:

$ make all
gcc -c -o main.o main.c
gcc -c -o config_msg.o config_msg.c
config_msg.c:4:9: warning: Config option A is enabled! [-W#pragma-messages]
#pragma message "Config option A is enabled!"
        ^
1 warning generated.
gcc -o main main.o config_msg.o
$ make all
gcc -o main main.o config_msg.o

#define CONFIG_OPTION_C添加到config.h后运行:

$ make all
gcc -c -o config_msg.o config_msg.c
config_msg.c:4:9: warning: Config option A is enabled! [-W#pragma-messages]
#pragma message "Config option A is enabled!"
        ^
config_msg.c:8:9: warning: Config option C is enabled! [-W#pragma-messages]
#pragma message "Config option C is enabled!"
        ^
2 warnings generated.
gcc -o main main.o config_msg.o

我不认为这是一个完美的解决方案。但希望它会有所帮助。 :)