有没有办法解决由供应商介绍的#define? [英] Is there a way to workaround a #define introduced by a vendor?
问题描述
因此,我们使用的供应商提供了一个库(主要用于C,支持一些C ++),它具有以下功能:
#ifndef int64_t
#define int64_t s_int64
#endif
#ifndef int32_t
#define int32_t s_int32
#endif
#ifndef int16_t
#define int16_t s_int16
#endif
#ifndef int8_t
#define int8_t s_int8
#endif
在他们的库里面的一个标题。现在的问题是,一旦他们的库包含在简单的C ++ 11代码中,如:
#include< iostream>
#include< vendor / library.h>
int main(void)
{
std :: int32_t std_i = 0;
return std_i;
}
立即出现编译错误( s_int32
不在 std ::
)。所以问题是,没有怠慢供应商这个修复这是,有无论如何解决这个在我们的代码? (btw。我试过的东西, #include< cstdint>
之前的标题,没有运气; extern C
包装器,没有运气。头文件安装在 / usr / include /
,所以无法控制包含的顺序, 。)
没有看到文件中还有什么是很难找到一个可以100%工作的解决方案。 >
但是,一个想法,你可以尝试将是以下(注意 - 我试过这个vaguelly在我的编译器,但我不保证它将工作在任何其他
创建一个包含其定义,但为typedefs的文件:
typedef s_int64 int64_t;
...
然后一些编译器允许你指定一个文件,在命令行上...例如,gcc有 -include
开关,因此您指定包括此文件。
然后,也可以在命令行中定义映射到自己:
-Dint64_t = int64_t
甚至将定义放在上述文件中:
#define int64_t int64_t
, #defines
不会干扰任何其他代码(他们不应该做任何事情 - 因为我不知道你是否可以结束了递归扩展?)。由于您已将 typedef
创建为正确的类型,供应商代码仍然可以使用
将typedefs放入强制包含的文件的原因是,您想要确保它包含在任何其他文件之前。
badger供应商更新他们的代码以使用 typedef
。
所有的理论思想。
So, a vendor that we use has provided a library (primarily for C, with some C++ support) that does the following:
#ifndef int64_t
#define int64_t s_int64
#endif
#ifndef int32_t
#define int32_t s_int32
#endif
#ifndef int16_t
#define int16_t s_int16
#endif
#ifndef int8_t
#define int8_t s_int8
#endif
In one of their headers deep inside their library. Now the problem is that once their library is included in simple C++11 code such as:
#include <iostream>
#include <vendor/library.h>
int main(void)
{
std::int32_t std_i = 0;
return std_i;
}
There is immediately a compiler error, (s_int32
is not in std::
). So question is, short of nagging the vendor to this fix this, is there anyway to workaround this in our code? (btw. things that I have tried, #include <cstdint>
before their headers, no luck; extern "C"
wrapper, no luck. The headers are installed in /usr/include/
so no control over order of inclusion I guess as well...)
Without seeing what else is in the file it's tricky to find a solution that would work 100%.
However, one idea that you could try would be the following (note - I've tried this vaguelly in my compiler, but I make no guarantees that it'll work in any other one).
Create a file containing their defines, but as typedefs:
typedef s_int64 int64_t;
...
Then some compilers allow you to specify a file to include before any others on the command line... for example, gcc has the -include
switch, so you specify to include this file.
Then, also on your command line, you can make defines that map to themselves:
-Dint64_t=int64_t
or even put the defines in the above file:
#define int64_t int64_t
The result is, in theory, the #defines
won't interfere with any other code (they shouldn't do anything - tho' I'm not sure if you could end up with recursive expansion?). And as you've created a typedef
to the correct type, the vendors code should still work.
The reason you put the typedefs into a file that's force-included, is that you want to ensure it gets included before anything else.
Then you should badger the vendor to update their code to use typedef
. You're a customer, they want to sell you a product.
All theory mind.
这篇关于有没有办法解决由供应商介绍的#define?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!