[英]compiling on windows and linux
I am new to c, and I have some been given some source code that someone else has written that was compiled on windows. 我是c的新手,我得到了一些其他人在Windows上编写的源代码。
After trying to make in compile on linux I have errors because linux doesn't support DWORD, WORD, AND UINT32. 尝试在linux上编译后我有错误,因为linux不支持DWORD,WORD和UINT32。 I have 6 files for example. 我有6个文件例如。 Ah, Ac, Bh, Bc, Ch, Cc These keyword are in all the files. 啊,Ac,Bh,Bc,Ch,Cc这些关键字都在所有文件中。
So I am thinking of 2 possible solutions. 所以我在考虑两种可能的解决方案。 Which is better #define or typedef. 哪个更好#define或typedef。
1) 1)
typedef unsigned long DWORD;
typedef unsigned short WORD;
typedef unsigned int UNINT32;
2) 2)
#define DWORD unsigned long
#define WORD unsigned short
#define UINT32 unsigned int
For the second part I am wondering where should I put these declarations. 对于第二部分,我想知道我应该把这些声明放在哪里。 Should they go in the header files, or should they go in the source files? 他们应该进入头文件,还是应该进入源文件?
For example should I do something like this in the header files, or in the source files? 例如,我应该在头文件或源文件中执行类似的操作吗?
#ifdef WIN32
/* windows stuff */
#else
typedef unsigned long DWORD;
typedef unsigned short WORD;
typedef unsigned int UNINT32;
#endif
Many thanks for the above suggestions, 非常感谢上述建议,
You have found the solution yourself: 您自己找到了解决方案:
#ifdef WIN32
/* windows stuff */
#else
typedef unsigned long DWORD;
typedef unsigned short WORD;
typedef unsigned int UNINT32;
#endif
Put this in a separate header file (typedefs.h) and include it from everywhere. 将它放在一个单独的头文件(typedefs.h)中,并从任何地方包含它。 Typedef are always preferred over pre-processor macros. Typedef始终优于预处理器宏。
My recommendation: Do not use DWORD, WORD or other Win32 types. 我的建议:不要使用DWORD,WORD或其他Win32类型。 I usually prefer to use C99 standard types: uint_t, int_t or uint16_t, uint32_t 我通常更喜欢使用C99标准类型:uint_t,int_t或uint16_t,uint32_t
Typedefs are definitely nicer. Typedef肯定更好。 #defines are preprocessor macro's and can have unintended consequences, because basically the C preprocessor performs a global search-and-replace for defines. #defines是预处理器宏,可能会产生意想不到的后果,因为C预处理器基本上对定义执行全局搜索和替换。 Typedefs are instructions to the compiler, and much better suited for what you want to do. Typedef是编译器的指令,更适合您想要做的事情。
typedef would be better in this instance because #define is just a generic mechanism but typedef is for defining types which is what you are doing. 在这种情况下,typedef会更好,因为#define只是一种通用机制,但是typedef用于定义你正在做的类型。
I would say put your code: 我会说你的代码:
#ifdef WIN32
/* windows stuff */
#else
typedef unsigned long DWORD;
typedef unsigned short WORD;
typedef unsigned int UNINT32;
#endif
into a new header file (with #define guards/#pragma once), then include that from the header or source files as necessary. 进入一个新的头文件(#define guards / #pragma一次),然后根据需要包含头文件或源文件中的文件。
Using a typedef turns the result into an actual type that gets put into the syntax tree. 使用typedef会将结果转换为放入语法树的实际类型。 (In other words, the compilers knows about it and recognizes it as a part of the language.) (换句话说,编译器知道它并将其识别为语言的一部分。)
#define
, in contrast, is just a text-substitution. 相反,# #define
只是一个文本替换。 So the compiler never gets to know about it, it instead just sees whatever it is that gets substituted. 所以编译器永远不会知道它,它只是看到被替换的任何东西。 This can make finding compile errors harder. 这可以使查找编译错误更加困难。
For your case, I would probably recommend typedef. 对于你的情况,我可能会推荐typedef。 #define has it's place, but I can't see any reason why you wouldn't want to use typedef here. #define有它的位置,但我看不出你为什么不想在这里使用typedef。
Be aware that other libraries may have defined these types, so you may have collisions. 请注意,其他库可能已定义了这些类型,因此您可能会发生冲突。 If you really want to be cross-platform, you might think about defining types with your app's namespace somehow. 如果您真的想要跨平台,那么您可能会考虑使用应用程序命名空间以某种方式定义类型。 Like 喜欢
myapp_dword
myapp_word
in order to minimize collisions with other libraries. 为了尽量减少与其他库的冲突。
Finally, I would actually recommend against the entire approach you are taking. 最后,我实际上建议不要采取你正在采取的整个方法。 If at all possible, it is best to use only the typenames defined in the language and in the C standard library (like size_t, etc.) Your code will be more portable, and you will have less headaches. 如果可能的话,最好只使用语言和C标准库中定义的类型名(如size_t等)。您的代码将更具可移植性,并且您的头痛会更少。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.