Are preprocessor definitions defined in the library?

Are they compiled (preprocessor definitions) into a static / dynamic library? For example, the FBX SDK requires KFBX_DLLINFO . A library that uses the FBX SDK should include this. Now, as far as I can tell from my limited experiment, the client application should not declare the definition again.

Now I can’t come up with a more practical scenario, but what if the client application β€œneeds” to exclude the definition (for example, _CRT_SECURE_NO_WARNINGS compiled with the library, but what if I need these warnings?

+4
source share
3 answers

In short: no.

In the long: For the most part, you can think of the definition of a preprocessor as a textual replacement mechanism. They are processed before compilation (pre-compilation), so they convert the source code immediately before the compiler, translate it into machine code, intermediate files, or whatever its purpose. By the time you have the binary lib / obj / dll / exe / so file, the preprocessor definitions are long gone.

If you include a header in your code that was packaged as part of the library (for example, to reference methods, types, enumerations, etc., defined by the library), then you include the preprocessor definitions that the library defines in this header.

In your case, if you include the FBX header, you can also find the KFBX_DLLINFO preprocessor KFBX_DLLINFO . The FBX binary you are linking to was almost certainly built with the same header, so you are building against the same definition. This is a generic template with libraries written in C / C ++: generic, generic header files along with a static or dynamic lib to build against.

+16
source

Preprocessor definitions exist only at compile time. They no longer exist in the compiled binary, whether it be a static or dynamic library or an executable.

+2
source

As Chris explains, #defines is a textual replacement mechanism. The extension was traditionally performed as a stage of preliminary compilation, while the main C ++ compiler did not (or did not need) access to the text of the preliminary substitution. For this reason, #defines can do what is impossible with C ++ language restrictions, such as concatenating values ​​to form new identifiers. Nowadays, compilers tend to implement macro-processing functions and may include some information about pre-processor symbols in debug symbol tables compiled into executable files. It is not very desirable or practical to access this debugging information for some use by clients, since debugging formats and content can change between compiler versions, are not very portable, cannot be very well debugged: - /, and access to them can be slow and awkward .

If I understand you correctly, you are wondering if #defines from any of the lower-level libraries your library uses will be automatically available to the "application" programmer using your library. No, they will not. You need to either provide your own definitions for the values ​​that your library API provides for the application programmer (matching with the values ​​of the lower level library inside, if they differ), or send also the header of the lower level library.

Reassignment Example:

Your .h library:

  #ifndef INCLUDED_MY_LIBRARY_H
 #define INCLUDED_MY_LIBRARY_H

 enum Time_Out
 {
     Sensible
     None
 };

 void do_stuff (Time_Out time_out);

 #endif

Your library.c:

  #include "my_library.h"
 #include "lower_level_library.h"

 void do_stuff (Time_Out time_out)
 {
     Lower_Level_Lib :: do_stuff (time_out == Sensible? Lower_Level_Lib :: Short_Timeout,
                                                    : Lower_Level_Lib :: No_Timeout);
     LOWER_LEVEL_LIB_MACRO ("whatever");
 }

As shown, the use of the Lower_Level_Lib function was not detected in my_library.h, so the application programmer does not need to know or enable lower_level_library.h. If you find that you need / want to put lower_level_library.h in my_library.h in order to use its types, constant, variables or functions in this case, you will also need to provide the application programmer with this library header.

+2
source

All Articles