如何使用C ++将旧的C代码与保留关键字链接起来? [英] How to link old C code with reserved keywords in it with C++?

查看:116
本文介绍了如何使用C ++将旧的C代码与保留关键字链接起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拥有10多年历史的C库,我相信它曾经在过去很不错,但是前一天,当我尝试将其与C ++源代码(包含主要功能)一起使用时,我遇到了一些困难.

I have a 10+ years old C library which -- I believe -- used to work just fine in the good old days, but when I tried to use it with a C++ source (containing the main function) the other day I ran into some difficulties.

为清楚起见,C库使用gcc编译就很好,并且生成了目标文件old_c_library.o.应当以某种方式使用该库 ,以便在您的main.c C源文件中#include d存放C头文件old_c_library.h.然后,应编译您的主要C源文件,并通过gcc将其与old_c_library.o链接在一起.在这里,我想改用C ++源文件main.cpp,并用g++进行编译/链接.

to clarify, the C library compiles just fine with gcc, and it generates an object file old_c_library.o. This library was supposed to be used in a way so that the C header file old_c_library.h is #included in your main.c C source file. Then your main C source file should be compiled and linked together with old_c_library.o via gcc. Here I want to use a C++ source file main.cpp instead, and compile/link it with g++.

在C ++源文件的编译过程中,发生了以下三个问题:

The following three problems occurred, during the compilation of the C++ source file:

  1. C库的头文件之一包含C ++保留字new(它是整数的名称),这导致致命错误;和
  2. C库的头文件之一包含一个calloc调用(缺少明确的类型转换),这导致致命错误;和
  3. C库的各种文件都包含代码,在这些代码中会比较有符号和无符号整数,从而产生警告.
  1. one of the header files of the C library contains the C++ reserved word new (it is the name of an integer), which resulted in fatal error; and
  2. one of the header files of the C library contains a calloc call (an explicit typecast is missing), which resulted in fatal error; and
  3. various files of the C library contain code where comparison of signed and unsigned integers happen, which resulted in warnings.

我试图按照注释中的建议使用#extern "C" { #include "obsolete_c_library.h" }技巧",但这并不能解决我的任何问题.

I tried to use the #extern "C" { #include "obsolete_c_library.h" } "trick", as suggested in the comments, but that did not solve any of my problems.

我可以通过重命名保留字的所有实例并将它们替换为(基本上)其他任何东西来解决问题1.我可以通过类型转换calloc调用来解决问题2.我可能会尝试根据此处建议的想法来整理警告:

I can sort out problem 1 by renaming all instances of the reserved words and replacing them by -- basically -- anything else. I can sort out problem 2 by typecasting the calloc call. I might try to sort out the warnings by ideas suggested here: How to disable GCC warnings for a few lines of code.

但是我仍然想知道,有没有办法以一种优雅,高级的方式来克服这些困难,而 实际上并没有涉及原始库?

But I still wonder, is there a way to overcome these difficulties in an elegant, high-level way, without actually touching the original library?

相关: 哪里C不是C ++的子集?我是否强制转换malloc的结果?推荐答案

通常来说,如果c3头文件不是出于预期这种头文件的目的而构建的,则将这些头文件转换为C ++源代码是不安全的.在某些情况下,它可以工作,但是您需要准备修改标头或为要访问的函数和全局变量编写自己的声明.

Generally speaking, it is not safe to #include C header files into C++ sources if those headers were not built in anticipation of such usage. Under some circumstances it can be made to work, but you need to be prepared to either modify the headers or write your own declarations for the functions and global variables you want to access.

至少,如果C标头声明了任何函数,而您没有在C ++中重新编译这些函数,则必须确保在C ++代码中为声明分配了C链接. C头通过条件编译指令自动解决这一问题并不少见,但是如果不这样做,则可以通过将包含的内容包装在C链接块中来在另一端进行处理:

At minimum, if the C headers declare any functions and you are not recompiling those functions in C++ then you must ensure that the declarations are assigned C linkage in your C++ code. It's not uncommon for C headers to account for that automatically via conditional compilation directives, but if they do not then you can do it on the other side by wrapping the inclusion(s) in a C linkage block:

extern "C" {
#include "myclib.h"
}

如果C标头声明的全局名称与C ++关键字冲突,并且您不需要引用,那么您 可能 能够使用预处理器重新定义它们:

If the C headers declare globals whose names conflict with C++ keywords, and which you do not need to reference, then you may be able to use the preprocessor to redefine them:

#define new extern_new
#include "myclib.h"
#undef  new

不能保证一定可以使用,但是值得一试.如图所示,不要忘记在包含C标头后#undef这样的宏.

That's not guaranteed to work, but it's worth a try. Do not forget to #undef such macros after including the C headers, as shown.

您可以使用宏玩一些有趣的技巧,以使特定的标头适应C ++,但有时在主C ++源代码中或仅复制/重写所需的声明(以及仅这些声明)更有意义.在您自己的C ++标头中.请注意,这样做并不需要声明C链接-该要求来自由C编译器而不是C ++编译器编译的库.

There may be other fun tricks you can play with macros to adapt specific headers to C++, but at some point it makes more sense to just copy / rewrite the needed declarations (and only those), either in your main C++ source or in your own C++ header. Note that doing so does not remove the need to declare C linkage -- that requirement comes from the library having been compiled by a C compiler rather than a C++ compiler.

这篇关于如何使用C ++将旧的C代码与保留关键字链接起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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