gpt4 book ai didi

c - 当 C 中有多个实现共享同一接口(interface)时,如何编写 CMakeLists.txt 或 Makefile 以避免冲突?

转载 作者:行者123 更新时间:2023-11-30 15:14:37 26 4
gpt4 key购买 nike

我有一个 C 项目,其目录如下:

./I.h
./config.h
./one/{I.h, I.c}
./two/{I.h, I.c}

接口(interface)在I.h中定义,两种不同的实现位于两个文件夹onetwo中,我们可以在config中定义一些宏来选择我想要的实现。所以,在 I.h 中,它看起来像:

#if   defined(USE_ONE)
#include "./one/I.h"
#elif defined(USE_TWO)
#include "./two/I.h"
#else
#error ...
#endif

我想知道如何编写 Makefile 或 CMakeLists.txt 来避免编译项目而不会发生冲突。

谢谢!

最佳答案

您可以定义自己的变量,然后在调用 cmake 时为其设置值.

CMakeLists.txt 中定义变量:

SET(IMPL "one" CACHE STRING "Choose I implementation") # 'one' is default value

然后检查值:

IF ("${IMPL}" EQUAL "one")
# Set sources including files in folder one
ELSEIF ("${IMPL}" EQUAL "two")
# Set sources including files in folder two
ENDIF ("${IMPL}" EQUAL "one")

然后你可以运行cmake -DIMPL:STRING=<one|two>

关于c - 当 C 中有多个实现共享同一接口(interface)时,如何编写 CMakeLists.txt 或 Makefile 以避免冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34009428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com