Originally posted by kpedersen
View Post
Originally posted by kpedersen
View Post
Originally posted by kpedersen
View Post
When the pre-processor has removed something it is no longer part of the input that is passed to the C++ tokenizer.
This is neither magic nor rocket science.
If you open a text editor and do a search&replace with the replace being empty, then the result is the same document without the word you searched for.
Originally posted by kpedersen
View Post
Anyone who has built a Qt program and looked at the build output will have seen that each C++ source file is passed unaltered to the C++ compiler.
The MOC steps are additional, if needed at all (when a class declaration contains the Q_OBJECT or Q_GADGET macro)
Originally posted by kpedersen
View Post
The error is about missing symbols at the linking stage, because the person had used a macro to declare function in a header but forgot to provide the implementation.
Easiest way for that is to let moc generate those implementations but as with any other code generator there is always the option to write the code manually.
The build process wouldn't have gotten to linking if the compiler had not been able to build the various translation units first.
Anyway, as I said earlier there is no shame in falling for this misunderstanding.
Many people who come from languages without pre-processor don't have prior experience with the this type of automatic text replacement.
I guess you are coming from a JavaScript/TypeScript situation which requires a separate tool to transform the actual source into something the target interpreter can understand.
In C and C++ there is a built-in step that is essentially a search&replace.
The most common use is the "#include" statement which gets replaced by the content of the file that follows.
Qt uses this mechanism to provide input for a code generation tool while at the same time keeping the code consumable by a C++ parser.
Some of these macros get replaced with nothing, i.e. effectively removed. For example "emit" or "slots"
Some get replaced with a C++ key word. For example "signals" gets replaced with "public"
Some are more complex and expand into declarations of functions or types. For example "Q_OBJECT".
The C pre-processor is quite versatile in this regard.
C code usually sees more use than C++ code but even the latter regularly makes use of "#include", "#ifdef", "#define".
Many C/C++ APIS ship related "#define" statements as part of their headers.
On a Linux system you can easily look for those by searching for "#define" in /usr/include
Edit: unsurprisingly FLTK uses the same mechanism.
For example "FL_OVERRIDE" gets either replaced with C++ keyboard "override" (if the C++ compiler is new enought) or replaced with nothing.
FL/forms.H has several defines that expand into function definitions, e.g.
Code:
#define forms_constructor(type,name) \ inline type* name(uchar t,int x,int y,int w,int h,const char* l) { \ return (type*)(fl_add_new(t, new type(x,y,w,h,l)));}
These things are so common that I doubt there are C/C++ developers who have not used them, let alone seen.
Many, if not most, will even have written them.
Comment