1

I am a c# programmer, hobbyist actually, with deep interest in programs both low-level and high-level. Many a time I come across C/C++ projects that have huge amounts of code like this ->

#if defined(LUA_COMPAT_GETN)
LUALIB_API int (luaL_getn) (lua_State *L, int t);
LUALIB_API void (luaL_setn) (lua_State *L, int t, int n);
#else
#define luaL_getn(L,i)          ((int)lua_objlen(L, i))
#define luaL_setn(L,i,j)        ((void)0)  /* no op! */
#endif

#if defined(LUA_COMPAT_OPENLIB)
#define luaI_openlib    luaL_openlib
#endif


/* extra error code for `luaL_load' */
#define LUA_ERRFILE     (LUA_ERRERR+1)

I find it difficult to wrap my head around all this, I can understand and program c# well. I can program in C/C++ but I dont understand the need for such declarations and syntax. Am I missing something? Is there a book or website that I can learn the use and need for such statements?

Note : I am an electrical engineer, so have limited exposure to programming in college. Most of what I can do is self-learnt.

  • it all depends on what you want to do. What you see here are C macros and function declarations. Check out [this](http://stackoverflow.com/questions/19418908/creating-a-portable-library-to-run-on-both-linux-and-windows). You can't get around [reading some on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Dmitry Ledentsov Feb 19 '14 at 06:46
  • These are # defines which correctly handle taking a symbol and mapping to a function declaration. If the user is building the the flag LUA_COMPAT_GETN then lual_getn calls that function other wise it maps it to another function. – rerun Feb 19 '14 at 06:48

1 Answers1

1

These are C++ Macros. I believe C# also has similar preprocessor statements like #if, #elif, #else, #endif
http://msdn.microsoft.com/en-us/library/ed8yd1ha.aspx
The purpose of some of the macros in C++ is to compile your code depending on various build conditions.

Mukt G
  • 101
  • 3