C模块中的名称空间或包 [英] namespace or packages in c modules

查看:62
本文介绍了C模块中的名称空间或包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是来自高级语言的初学者级c程序员,觉得c具有扁平的结构.有没有一种模拟包的方法,因此我可以拥有一个干净的名称空间而无需前缀.

I'm a beginner level c programmer coming from high level languages and feeling like c is of a flat structure. Is there a sort of way to simulate packages so I can have a clean namespace without requiring prefixes.

嵌套结构是我正在寻找的结构.

Nested structures is one what I'm looking at.

它如何与第三方外部库一起使用,发生名称冲突或分离时会发生什么情况.

How it works with 3rd party external libraries out there, what happens when there's a name conflict or is it sort of separate.

如果我要构建与库链接的库或多模块应用程序,则每个模块可以具有与另一个模块相同的变量,函数或结构名称.

In case I've to build a library or a multi modular application linked with libs so each module can have a variable, function or struct names that are same as in another module.

如果您要链接两个名称冲突的第三方库(.a文件等),怎么办?

Also what if you are linking against two third party libs (.a file etc.) who have conflicting names, how do you resolve such conflicts.

推荐答案

好吧,从技术上讲,没有办法使用与C ++完全相同的名称空间.不幸的是,这导致了诸如SDL2之类的事情,在每个函数前面都加上了"SDL_".关于解决外部库之间的冲突,有一种解决方案.假设我们有一个库"libfoo",它具有功能

Ok, technically no, there is no way to have namespaces in the exact same way that C++ does. Unfortunately this leads to things like SDL2, which prefixes every function with "SDL_". About resolving conflicts between external libraries, there is one solution. Let's say we have library "libfoo", which has the function

void do_foo(void *);

已定义.您尝试编译并链接程序,但事实证明,您正在使用的另一个库"libfu"也具有此功能.现在,我要做的是创建某种适配器库,并使用适当的前缀将函数重命名.因此,我们将拥有一个定义函数的文件"libfoo_f.h"

defined. You try to compile and link your program, but it turns out that the other library you are using, "libfu", also has this function. Now what I would do would be to create some sort of adaptor library that renamed the function with an appropriate prefix. So we would have a file "libfoo_f.h" that defines the function

void FOO_do_foo(void *);

和"libfoo_f.c"中

and in "libfoo_f.c"

#include"libfoo_f.h"
#include<libfoo.h>
void FOO_do_foo(void *data)
{
    do_foo(data);
}

此函数的全部作用是提供一种在不破坏库的情况下访问libfoo的do_foo的方法.坦白地说,由于大多数库都具有结构良好,名称合理的接口,因此不太可能与其他库发生冲突,因此我无需这样做.

All this function does is provide a way to access libfoo's do_foo without breaking your libraries. Honestly, I have never had to do this due to the fact that most libraries have well-structured, well-named interfaces unlikely to conflict with other libraries.

这篇关于C模块中的名称空间或包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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