C ++:Extern C NameSpace中的函数 [英] C++ : Extern C Functions inside a NameSpace

查看:833
本文介绍了C ++:Extern C NameSpace中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须链接两个库,例如A和B.一些文件在两个库中是通用的。
所以,我在一个命名空间中声明库A中的函数,说abc。
因此,在A和B中,函数func如下所示:

I have to link two libraries, say A and B. Some of the files are common in both libraries. So, I declare functions in library A inside a namespace, say abc. So, in A and B, a function func looks like below:

[ in A]

    namespace abc {
    extern "C" void func();
    }


[in B]

    extern "C" void func();

在构建项目时,编译器会抛出链接错误,说明函数func的多个定义。不是函数func在A里面的命名空间或者有一些问题,externC函数。如果有,那么我如何区分它们呢?

While building the project, compiler throws linking errors saying multiple definitions of function func. Isn't the function func in A inside the namespace or is there some problem with extern "C" functions. If there is, then how can I differentiate them both?

推荐答案

当你使用ExternC时,你关闭名称调整,所以你失去了命名空间信息,因为C没有这样的概念。这会导致重复的定义。

When you use Extern "C" you are turning off name mangling so you lose the namespace information as C has no such concept. This causes a duplicate definition.

这篇关于C ++:Extern C NameSpace中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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