compiling on windows and linux

前端 未结 4 1811
闹比i
闹比i 2021-02-20 11:50

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 e

4条回答
  •  旧巷少年郎
    2021-02-20 11:52

    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.

提交回复
热议问题