链接中包含具有同等功能的签名几个C目标文件 [英] Linking in several C object files that contain functions with equivalent signature

查看:159
本文介绍了链接中包含具有同等功能的签名几个C目标文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有一个C ++程序,我想打电话给从C对象文件功能吧。约束是,我不能使C源代码文件进行任何更改。

Lets say I have a C++ program and I want to call functions from a C object file in it. The constraint is that I'm not allowed to make any changes to the C source files.

在这种情况下,我将包括C头为的externC的在我的C ++文件,调用功能,采用的的gcc -c 的编译C对象,结果传递给的 G ++ 的实际C ++源代码一起。

In this case I would include the C header as extern "C" in my C++ file, call the function, compile the C object using gcc -c, and pass the result to g++ along with the actual C++ sources.

我想我也会做的是把C-头什么的的#include 的在C ++文件名为命名的块内,以保持局部命名空间干净(谁知道还有什么其他花哨的东西没有在的.h 的文件)。

I guess what I would also do is to put the C-header #include inside a named namespace block in the C++ file, as to keep the local namespace clean (who knows what other fancy stuff there is in the .h file).

如果我需要从两个C对象文件调用的函数现在该怎么办。比方说,他们有不同的签名。但也有在目标文件一些重复的功能。会不会我的链接在这一点炸毁,如果我试图在同一时间通过两个对象,以G ++?同为全局变量。

What now if I need to call functions from two C object files. Lets say that they have different signatures. But there are also some duplicate functions in the object files. Wouldn't my linker blow up at this point if I tried to pass both objects to g++ at the same time? Same for globals.

还是那句话:我不能的C程序源代码改变的任何。否则,我只想重新命名.c文件到.CXX,里面包裹的内容和相应的头文件的内容的命名空间喇嘛{} 的和饲料一切的 G ++

Again: I'm not allowed to change ANY of the C sources. Else I would just rename the .c files to .cxx, wrap their contents and the contents of the corresponding headers inside namespace bla { } and feed everything to g++.

(是的,C是不是C ++的一个子集,已经在评论中提及了)

推荐答案

您最后一段有一个很好的解决方案 - 为什么不能让一个C ++文件是这样的:

Your last paragraph has a good solution - why not make a C++ file something like this:

namespace c_namespace_1 {
    #include "CFile1.c"
}

namespace c_namespace_2 {
    #include "CFile2.c"
}

等等....那么你可以编译该文件作为C ++,而不必修改原始来源。

And so on.... then you could compile this file as C++ and not have to modify the original sources.

这篇关于链接中包含具有同等功能的签名几个C目标文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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