在main.c中定义的宏在另一个包含的文件中不可见 [英] Macro defined in main.c not visible in another included file

查看:944
本文介绍了在main.c中定义的宏在另一个包含的文件中不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个C和H文件

main.c 中我定义了一个宏, code> ws_driver.c 我想使用它。



ws_driver.h 包含在 main.c中



main.c

  #define WS_PORT PORT_D8 
#define WS_BIT D8
#define WS_DDR DDR_D8

#包括ws_driver.h

ws_dirver.c 我有两项检查:

ws_driver.c

  #includews_driver.h

#ifndef WS_PORT
#errorWS_PORT not defined!
#endif

#ifndef WS_BIT
#错误WS_BIT未定义!
#endif

两者都失败。

  $ avr-gcc -std = gnu99 -mmcu = atmega328p -DF_CPU = 16000000UL -Os -I。 -I -funsigned-char -funsigned-bitfields -fpack-struct -fshort -enums -Wall -Wstrict-prototypes -Wno-main -Wno-strict-prototypes -Wno-comment -g2 -ggdb -ffunction-sections -fdata-sections -Wl, -  gc-sections -Wl, -  relax -std = gnu99 main.c ws_driver.c --output main.elf 
ws_driver.c:10:3​​:error:#errorWS_PORT not defined !
#错误WS_PORT未定义!
^
ws_driver.c:14:3:错误:#errorWS_BIT未定义!
#错误WS_BIT未定义!
^
ws_driver.c:在函数'ws_show'中:
ws_driver.c:23:20:错误:'WS_PORT'未声明(首次在此函数中使用)
#define bus_low()(WS_PORT)& =〜(1 ^
ws_driver.c:37:2:注意:在宏'bus_low'的扩展中
bus_low );
^
ws_driver.c:23:20:注意:每个未声明的标识符仅对于出现在
中的每个函数报告一次#define b ......

我做错了什么?请询问是否要查看代码的其他部分。

解决方案

您必须在头文件中定义宏,如果您想在多个地方使用它们,则不在.c文件中。



当编译器编译 ws_driver.c 它只包含 ws_driver.h 并且宏不在那里。它不包括main.c.每个.c文件都是单独编译的。



移动宏定义,比如 config.h 需要它。



你也可以使用编译器的定义 -DWS_BIT = 123 -DOTHER = SMTH 。您传递的值将位于生成的目标文件中,无需重新编译即可更改。



如果您只想编译一次,则将这些参数作为参数传递或创建 configure_my_library()函数...


I have multiple C and H files

In main.c I defined a macro, and in ws_driver.c I want to use it.

ws_driver.h is included in main.c.

main.c

#define WS_PORT PORT_D8
#define WS_BIT D8
#define WS_DDR DDR_D8

#include "ws_driver.h"

In ws_dirver.c I have two checks:

ws_driver.c

#include "ws_driver.h"

#ifndef WS_PORT
# error "WS_PORT not defined!"
#endif

#ifndef WS_BIT
# error "WS_BIT not defined!"
#endif

Both are failing.

$ avr-gcc -std=gnu99 -mmcu=atmega328p -DF_CPU=16000000UL -Os -I. -I -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wno-main -Wno-strict-prototypes -Wno-comment -g2 -ggdb -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax -std=gnu99 main.c  ws_driver.c --output main.elf
ws_driver.c:10:3: error: #error "WS_PORT not defined!"
 # error "WS_PORT not defined!"
   ^
ws_driver.c:14:3: error: #error "WS_BIT not defined!"
 # error "WS_BIT not defined!"
   ^
ws_driver.c: In function 'ws_show':
ws_driver.c:23:20: error: 'WS_PORT' undeclared (first use in this function)
 #define bus_low() (WS_PORT) &= ~(1 << WS_BIT)
                    ^
ws_driver.c:37:2: note: in expansion of macro 'bus_low'
  bus_low();
  ^
ws_driver.c:23:20: note: each undeclared identifier is reported only once for each function it appears in
 #define b......

What am I doing wrong? Please ask if you want to see some other part of the code.

解决方案

You have to define the macros in a header file, not in the .c file if you want to use them in multiple places.

When the compiler compiles ws_driver.c it only includes ws_driver.h and the macro is not there. It does not include main.c. Each .c file is compiled separately.

Move the macro definitions in say config.h and include it everywhere you need it.

You can also use the compiler's define -DWS_BIT=123 -DOTHER=SMTH. The value you pass will be in the generated object file and can not be changed without recompiling.

If you want to compile only once, then pass these as parameters or create a configure_my_library() function...

这篇关于在main.c中定义的宏在另一个包含的文件中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆