Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compiling on windows and linux

Tags:

c

I am new to c, and I have some been given some source code that someone else has written that was compiled on windows.

After trying to make in compile on linux I have errors because linux doesn't support DWORD, WORD, AND UINT32. I have 6 files for example. A.h, A.c, B.h, B.c, C.h, C.c. These keyword are in all the files.

So I am thinking of 2 possible solutions. Which is better #define or typedef.

1)

typedef unsigned long DWORD;
typedef unsigned short WORD;
typedef unsigned int UNINT32;

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,

like image 564
ant2009 Avatar asked Jul 20 '09 14:07

ant2009


Video Answer


4 Answers

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. Typedef are always preferred over pre-processor macros.

My recommendation: Do not use DWORD, WORD or other Win32 types. I usually prefer to use C99 standard types: uint_t, int_t or uint16_t, uint32_t

like image 105
kgiannakakis Avatar answered Oct 19 '22 13:10

kgiannakakis


Typedefs are definitely nicer. #defines are preprocessor macro's and can have unintended consequences, because basically the C preprocessor performs a global search-and-replace for defines. Typedefs are instructions to the compiler, and much better suited for what you want to do.

like image 29
Emiel Avatar answered Oct 19 '22 11:10

Emiel


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.

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.

like image 2
danio Avatar answered Oct 19 '22 11:10

danio


Using a typedef turns the result into an actual type that gets put into the syntax tree. (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. 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. #define has it's place, but I can't see any reason why you wouldn't want to use typedef here.

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.

like image 1
Christopher Avatar answered Oct 19 '22 13:10

Christopher