#ifdef #else #endif #fi #ifndef 的用法(转)

预处理就是在进行编译的第一遍词法扫描和语法分析之前所作的工作。说白了,就是对源文件进行编译前,先对预处理部分进行处理,然后对处理后的代码进行编译。这样做的好处是,经过处理后的代码,将会变的很精短。
   关于预处理命令中的文件包含(#include),宏定义(#define),书上已经有了详细的说明,在这里就不详述了。这里主要是对条件编译(#ifdef,#else,#endif,#if等)进行说明。以下分3种情况:
  1:情况1:
  #ifdef _XXXX
  ...程序段1...
  #else
  ...程序段2...
  #endif
   这表明如果标识符_XXXX已被#define命令定义过则对程序段1进行编译;否则对程序段2进行编译。
  例: 
  #define NUM
  .............
  .............
  .............
  #ifdef NUM
   printf("之前NUM有过定义啦!:) \n");
  #else
   printf("之前NUM没有过定义!:( \n");
  #endif
  }
   如果程序开头有#define NUM这行,即NUM有定义,碰到下面#ifdef NUM的时候,当然执行第一个printf。否则第二个printf将被执行。
   我认为,用这种,可以很方便的开启/关闭整个程序的某项特定功能。
  2:情况2: 
  #ifndef _XXXX 
  ...程序段1... 
  #else 
  ...程序段2... 
  #endif
   这里使用了#ifndef,表示的是if not def。当然是和#ifdef相反的状况(如果没有定义了标识符_XXXX,那么执行程序段1,否则执行程序段2)。例子就不举了。
  3:情况3:
  #if 常量 
  ...程序段1...
  #else
  ...程序段2...
  #endif 
   这里表示,如果常量为真(非0,随便什么数字,只要不是0),就执行程序段1,否则执行程序段2。
   我认为,这种方法可以将测试代码加进来。当需要开启测试的时候,只要将常量变1就好了。而不要测试的时候,只要将常量变0。

 

我们主要使用以下几种方法,假设我们已在程序首部定义#ifdef DEBUG与#ifdef TEST:

  1.利用#ifdef/#endif将某程序功能模块包括进去,以向某用户提供该功能。

  在程序首部定义#ifdef HNLD:

  #ifdef HNLD

  #include"n166_hn.c"

  #endif

  如果不许向别的用户提供该功能,则在编译之前将首部的HNLD加一下划线即可。

  2.在每一个子程序前加上标记,以便追踪程序的运行。

  #ifdef DEBUG

  printf(" Now is in hunan !");

  #endif

  3.避开硬件的限制。有时一些具体应用环境的硬件不一样,但限于条件,本地缺乏这种设备,于是绕过硬件,直接写出预期结果。具体做法是:

  #ifndef TEST

  i=dial(); 

  //程序调试运行时绕过此语句

  #else 

  i=0;

  #endif

  调试通过后,再屏蔽TEST的定义并重新编译,即可发给用户使用了。 

转自:https://bbs.youkuaiyun.com/topics/210046082

# Copyright 2018 Embedded Microprocessor Benchmark Consortium (EEMBC) # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Original Author: Shay Gal-on # Make sure the default target is to simply build and run the benchmark. RSTAMP = v1.0 .PHONY: run score run: $(OUTFILE) rerun score score: @echo "Check run1.log and run2.log for results." @echo "See README.md for run and reporting rules." ifndef PORT_DIR # Ports for a couple of common self hosted platforms UNAME=$(shell if command -v uname 2> /dev/null; then uname ; fi) ifneq (,$(findstring CYGWIN,$(UNAME))) PORT_DIR=cygwin endif ifneq (,$(findstring Darwin,$(UNAME))) PORT_DIR=macos endif ifneq (,$(findstring FreeBSD,$(UNAME))) PORT_DIR=freebsd endif ifneq (,$(findstring Linux,$(UNAME))) PORT_DIR=linux endif endif ifndef PORT_DIR $(error PLEASE define PORT_DIR! (e.g. make PORT_DIR=simple)) endif vpath %.c $(PORT_DIR) vpath %.h $(PORT_DIR) vpath %.mak $(PORT_DIR) include $(PORT_DIR)/core_portme.mak ifndef ITERATIONS ITERATIONS=0 endif ifdef REBUILD FORCE_REBUILD=force_rebuild endif CFLAGS += -DITERATIONS=$(ITERATIONS) CORE_FILES = core_list_join core_main core_matrix core_state core_util ORIG_SRCS = $(addsuffix .c,$(CORE_FILES)) SRCS = $(ORIG_SRCS) $(PORT_SRCS) OBJS = $(addprefix $(OPATH),$(addsuffix $(OEXT),$(CORE_FILES)) $(PORT_OBJS)) OUTNAME = coremark$(EXE) OUTFILE = $(OPATH)$(OUTNAME) LOUTCMD = $(OFLAG) $(OUTFILE) $(LFLAGS_END) OUTCMD = $(OUTFLAG) $(OUTFILE) $(LFLAGS_END) HEADERS = coremark.h CHECK_FILES = $(ORIG_SRCS) $(HEADERS) $(OPATH): $(MKDIR) $(OPATH) .PHONY: compile link ifdef SEPARATE_COMPILE $(OPATH)$(PORT_DIR): $(MKDIR) $(OPATH)$(PORT_DIR) compile: $(OPATH) $(OPATH)$(PORT_DIR) $(OBJS) $(HEADERS) link: compile $(LD) $(LFLAGS) $(XLFLAGS) $(OBJS) $(LOUTCMD) else compile: $(OPATH) $(SRCS) $(HEADERS) $(CC) $(CFLAGS) $(XCFLAGS) $(SRCS) $(OUTCMD) link: compile @echo "Link performed along with compile" endif $(OUTFILE): $(SRCS) $(HEADERS) Makefile core_portme.mak $(EXTRA_DEPENDS) $(FORCE_REBUILD) $(MAKE) port_prebuild $(MAKE) link $(MAKE) port_postbuild .PHONY: rerun rerun: $(MAKE) XCFLAGS="$(XCFLAGS) -DPERFORMANCE_RUN=1" load run1.log $(MAKE) XCFLAGS="$(XCFLAGS) -DVALIDATION_RUN=1" load run2.log PARAM1=$(PORT_PARAMS) 0x0 0x0 0x66 $(ITERATIONS) PARAM2=$(PORT_PARAMS) 0x3415 0x3415 0x66 $(ITERATIONS) PARAM3=$(PORT_PARAMS) 8 8 8 $(ITERATIONS) run1.log-PARAM=$(PARAM1) 7 1 2000 run2.log-PARAM=$(PARAM2) 7 1 2000 run3.log-PARAM=$(PARAM3) 7 1 1200 run1.log run2.log run3.log: load $(MAKE) port_prerun $(RUN) $(OUTFILE) $($(@)-PARAM) > $(OPATH)$@ $(MAKE) port_postrun .PHONY: gen_pgo_data gen_pgo_data: run3.log .PHONY: load load: $(OUTFILE) $(MAKE) port_preload $(LOAD) $(OUTFILE) $(MAKE) port_postload .PHONY: clean clean: rm -f $(OUTFILE) $(OBJS) $(OPATH)*.log *.info $(OPATH)index.html $(PORT_CLEAN) .PHONY: force_rebuild force_rebuild: echo "Forcing Rebuild" .PHONY: check check: md5sum -c coremark.md5 ifdef ETC # Targets related to testing and releasing CoreMark. Not part of the general release! include Makefile.internal endif 参考以上makefile给出在ubuntu上使用coremark交叉编译对嵌入式设备进行跑分的完整教程
最新发布
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值