简体   繁体   English

使用宏在两个函数定义之间切换

[英]Change between two function definitions with macros

I'm working on two different LCD initializations. 我正在进行两种不同的LCD初始化。 One is from the Professor's hardware and the other one is mine. 一个来自教授的硬件,另一个是我的。 I want to switch between two functions using macro definitions: 我想使用宏定义在两个函数之间切换:

#ifndef LCD_Professor

void InitLCD (uint8_t N, uint8_t F, uint8_t D, uint8_t C, uint8_t B, uint8_t ID, uint8_t SH) {
 ...
}

#endif

#ifndef LCD_Group3

void InitLCD (uint8_t N, uint8_t F, uint8_t D, uint8_t C, uint8_t B, uint8_t ID, uint8_t SH) { 
...
}

#endif

I was hoping that if I used #define LCD_Group3 in the beginning of the file, I could choose between both functions. 我希望如果在文件开头使用#define LCD_Group3,则可以在两个函数之间进行选择。 Truth is, I never really worked with these macros. 事实是,我从未真正使用过这些宏。 Should this work? 应该行吗? or rather, is there any other way to do this? 或者更确切地说,还有其他方法吗?

Thanks! 谢谢!

It should be 它应该是

#ifdef LCD_Professor
...
#endif

#ifdef LCD_Group3
...
#endif

Not #ifndef to choose which one of them you want to use, then you can do #define LCD_Group3 or LCD_Professor before you include the file, to enable one or the other. 不能#ifndef选择要使用的其中一个,则可以在包含文件之前#define LCD_Group3LCD_Professor ,以启用一个或另一个。

Must you use a macro? 您必须使用宏吗? An alternative is to put the two functions in separate files and build only the one you want. 一种替代方法是将这两个函数放在单独的文件中,然后仅构建所需的一个。 Or even build both to different executable files and select the one you want when you run it. 甚至将它们都构建为不同的可执行文件,然后在运行时选择所需的文件。

If you are not familiar with building such things, you need a Makefile (a file with the name Makefile). 如果您不熟悉构建此类事物,则需要一个Makefile(名称为Makefile的文件)。 For example, to build two applications 'prof' and 'stud' from a common file app.c and your two implementations in prof.c and stud.c :- 例如,要从一个通用文件app.c和prof.c和stud.c中的两个实现构建两个应用程序“ prof”和“ stud”:

all: stud prof
CFLAGS = -g -Wall
CC = gcc

prof: prof.o app.o
        $(CC) $^ -o $@

stud: stud.o app.o
        $(CC) $^ -o $@

Note that there is a TAB before each $(CC), not spaces 请注意,每个$(CC)前面都有一个制表符,而不是空格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM