微创有关从C codeBase的用C无符号字符* ++和strchr激进的变化? [英] Minimally invasive change for strchr on unsigned char * in C++ from a C codebase?
问题描述
我试图编译C codeBase的为C ++,调整一些包括。它采用和strchr()
上unsigned char型指针,例如:
I'm trying to compile a C codebase as C++, tweaking some of the includes. It uses strchr()
on unsigned char pointers, e.g.:
#include <string.h>
#include <stdio.h>
// Cannot modify this file with any non-C-isms, but let's say an include can
// be changed (although all things being equal I'd rather not)
#include <SomethingICanChange.h>
int main(int argc, char* argv[]) {
unsigned char b = 'B';
unsigned char const * str = "ABC";
unsigned char const * pos = strchr(str, b);
if (pos) {
printf("position is %d\n", pos - str);
}
return 0;
}
这会导致错误的C ++(原因在别处解释)......甚至与 -fpermissive
。
That causes an error in C++ (for reasons explained elsewhere)...even with -fpermissive
.
test.c: In function ‘int main(int, char**)’:
test.c:6:33: warning: invalid conversion from ‘const char*’ to ‘const unsigned char*’ [-fpermissive]
test.c:7:46: error: call of overloaded ‘strchr(const unsigned char*&, unsigned char&)’ is ambiguous
test.c:7:46: note: candidates are:
In file included from test.c:1:0:
/usr/include/string.h:215:14: note: char* strchr(char*, int) <near match>
/usr/include/string.h:215:14: note: no known conversion for argument 1 from ‘const unsigned char*’ to ‘char*’
/usr/include/string.h:217:22: note: const char* strchr(const char*, int) <near match>
/usr/include/string.h:217:22: note: no known conversion for argument 1 from ‘const unsigned char*’ to ‘const char*’
当遇到这种事情我去
通常的和strchr,呸,只是摆脱它完全是的。但我不希望修改这些文件自己,这是C code,正在保持非常便携,以较旧的平台。他们不会乐意将强制转换为callsites安抚C ++ ......不过,如果一个struchr存在,并且是标准的,他们可能会使用它。
Usually when faced with this kind of thing I go "strchr, yuck, just get rid of it entirely". But I don't want to modify these files myself, and this is C code that is being kept very portable to rather old platforms. They wouldn't be happy putting casts at callsites to appease C++...though if a "struchr" existed and was standard they'd probably use it.
我也可以破解它周围的 SomethingICanChange.h
这种或那种方式,就像我可以添加:
I can also hack around it one way or another, like in SomethingICanChange.h
I could add:
unsigned char const * strchr(unsigned char const * s, int c) {
return (unsigned char const *)strchr((char const *)s, c);
}
这与 -fpermissive
的作品。但我无法检查过载到C codeBase的(当然,没有#ifdef来)。只是想知道如果任何人有一个更好的主意,我将场上加入 struchr
如果这就是人们在这种情况下缠完成(或任何名称是常见的,如果有一个共同的一个)。
That works with -fpermissive
. But I wouldn't be able to check that overload into the C codebase (well, without an #ifdef). Just wondering if anyone has a better idea, I will pitch adding struchr
if that's what people in this situation wind up doing (or whatever name is common if there's a common one).
推荐答案
的初始注的:我知道这也是一个很丑陋的事情,但它可能会帮助你(希望)更好的想法!
Initial note: I am aware that this is also quite an ugly thing to do, but it might help you with a (hopefully) better idea!
如何使用宏:
#define strchr(s, c) ((unsigned char const *)strchr((char const *)s, c))
您可以把它作为一个编译标志(-D)从你的Makefile /编译脚本。我想你有一个自定义的Makefile /编译脚本为您的C ++项目,所以你不需要这个宏添加到C code-基地。
You could include it as a compile flag (-D) from you Makefile/build script. I suppose you have a custom Makefile/build script for your C++ project, so you do not need to add this macro to the C code-base.
要使它的略的更好,你可以使用一个额外的文件,你从你的Makefile /编译脚本,这将增加这些宏在(更)结构化的方式来 CPPFLAGS
To make it slightly better, you can use an additional file, that you include from your Makefile/build script, which adds these macros in a (more) structured way to CPPFLAGS
这篇关于微创有关从C codeBase的用C无符号字符* ++和strchr激进的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!