我正在将一个第三方项目集成到我的应用程序中(确切地说是LittleFS)。我试图将该项目编译为一个静态库,然后将其链接到我的应用程序中。我对链接和正确获取包含路径没有问题,我的问题是如何处理编译时的配置。
例如,项目在其标题中有一行,如下所示:
struct lfs_config {
...
#ifdef LFS_THREADSAFE
int (*lock)(const struct lfs_config *c);
int (*unlock)(const struct lfs_config *c);
#endif
...
};
我将使用以下标志编译第三方项目/静态库以启用此功能:
gcc ... -DLFS_THREADSAFE
但是,当我将这个库链接到我的应用程序中时,为了使其正常工作,我还需要****确保我的应用程序设置了-DLFS_THREADSAFE
标志。
这意味着我必须跟踪哪些定义设置在两个不同的地方,这似乎会变得有点容易出错和麻烦后,一段时间。
我的问题是,如何确保静态库和应用程序共享相同的配置?
2条答案
按热度按时间46qrfjad1#
There is no definitive and portable way to do this. In fact, most coding standards I've worked with will not permit any structure that is defined differently based on macros, particularly if that structure is used as part of an API. This would be considered an API design flaw if an interface structure is not consistently defined.
Rather than having the structure contents actually change based on the compile time configuration, it is better to keep it consistent but rather simply set the values to something to indicate they are not used. That is, in your example, keep the function pointers unconditionally, without any "ifdef":
In the event that
LFS_THREADSAFE
is undefined, these pointers can be set NULL. Assuming you use c99 style initializers, this can just work, i.e.In this case, if
LFS_THREADSAFE
is defined, thelock
andunlock
items will be set accordingly. But ifLFS_THREADSAFE
is undefined, these initializers will be omitted, and per the C99 standard, the unspecified members will be assigned value 0 (NULL, in the case of a pointer). Inside the library, it can be made to simply skip the lock/unlock call if the function pointer value is NULL.Alternatively, one could also always set the pointer to a valid function, but have it be a no-op if the
LFS_THREADSAFE
is undefined. That is:And similar for unlock.
To summarize, its always wise to keep API structures as consistent as possible, and not make them dependent on macros. Hope this helps!!
e4yzc0pl2#
我已经查看了littlefs的源代码。
LFS_THREADSAFE
是 * 唯一 * 的(即,您不需要设置LFS_MIGRATE
)。只需删除
lfs.h
中的#ifdef/#endif
即可。但是,要回答你的问题一般来说:只需通过一个“顶级”makefile来构建您项目和littlefs: