在编译时选择一个函数
在我的 C 项目中,我有五个不同的函数(具有相同的名称),它们以不同的方式实现一种算法。在编译时我只需要选择这些函数之一。 我可以使用 #define
和一堆 #ifndef
来实现吗?有更好的方法来做到这一点吗?
in my C project I have five different function (with the same name) which implement one algorithm but in different ways. At compile time I need to select only one of these functions.
Can I implement this using a #define
and a bunch of #ifndef
? Are there better ways to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
也许更优雅的方法是使用 5 个不同的名称(例如 f1、f2、f3、f4、f5)来定义它们,但始终使用相同的不同名称(例如 f)来调用函数。然后使用类似以下内容进行编译:
这将对 f() 的任何调用编译为对 f1() 的调用。
Probably a more elegant approach would be to define them with 5 different names (e.g. f1, f2, f3, f4, f5), but always call the function using the same different name (e.g. f). Then compile with something like:
That will compile any calls to f() as a call to f1().
是的。你要做的就是在代码中这样说:
然后在编译时添加 gcc 选项 -DFUNCX 例如:
然后你可以通过更改 -D 选项中放入的值来选择要编译的函数。这是很常见的做法。
Yes. What you do is say this in the code:
And then when you compile add the gcc option -DFUNCX for example:
Then you can choose which function to compile in by changing the value you put in the -D option. This is very common practice.
一个翻译单元中不能定义两个具有相同名称的不同函数。
所以我猜你已经将不同源文件之间的函数分开了。只需在编译时包含您想要的文件...例如,使用 gcc
其中任何
bubble.c
、merge.c
、radix.c (和其他)有一个同名的函数:
You cannot have two different function with the same name defined within one translation unit.
So I guess you already have the functions separated between different source files. Just include the file you want when you compile ... for example, with gcc
Where any of
bubble.c
,merge.c
,radix.c
(and others) have a function with the same name:在 C 语言中,这是我所知道的唯一方法。 C++ 你会很幸运。
In C, that is the only way I know of doing this. C++ you would be in luck.
如果您在单独的文件中拥有函数的源代码(例如,在 f1.c 和 f2.c 中),则此方案有效:
然后我可以像这样编译:
注意 stringize 运算符和双宏的使用dance - 这是标准 C 预处理器的标准习惯用法。它允许我避免在 C 编译器命令行中添加引号;想想如果您必须编写以下内容,那么使用起来会有多困难:
运行 so1 和 so2 的结果是:
这显示了无需任何
#ifdef
行即可实现所需结果的替代方法。If you have the source for the functions in separate files (for my example, in f1.c and f2.c), then this scheme works:
Then I can compile like this:
Note the use of the stringize operator and the double-macro dance - this is a standard idiom for the Standard C Preprocessor. It allows me to avoid putting quotes in the C compiler command line; think how much harder it would be to use if you had to write:
The results of running so1 and so2 were:
This shows an alternative way to achieve the desired result without any
#ifdef
lines.ifndef 宏似乎是编译时唯一的可能性。
您可以在运行时尝试使用函数ptrs。
只是认为您可以在不同的文件中创建每个函数,并在需要时链接正确的函数。没有宏,只有一个 Makefile 开关。这样您就可以保留相同的签名并在链接时决定使用哪个实现。链接 algo1.o 或链接 algo2.o...
ifndef macros seem the only possibility at compile time.
You may try with function ptrs at runtime.
Just thought that you may create each function in different file and link the proper one whenever you want. No macros, just a Makefile switch. That way you keep the same signature and decide at link time which implementation to use. Link algo1.o or link algo2.o...
如果我是你,我会有 5 个不同的函数名称,然后使用 if 语句来确定在运行时调用哪个函数。
if i were you i'd have 5 different function name then use an if statement to figure out which one to call at runtime.
C 没有重载(运算符或其他),因此没有其他方法可以做到这一点。恐怕您被预处理器命令所困扰。
C does not have overloading (operator or otherwise) so there is no other way to do this. I am afraid you are stuck with pre-processor commands.
如果你一定要坚持使用C,你可以使用函数指针,但我建议你使用C++和多态性,这会让你摆脱你的麻烦:)
if you must insist on using C, you can use function pointers, but I would advice you to use C++ and polymorphism, this will get you of your troubles : )