How do you a preprocess statement for #include - preprocessor

The following seems to do nothing and I'm not sure why. I have the following lines at the top of my code with SOME_FUNCTION also defined of course. Can you do something like this for another directive of include?
#if SOME_FUNCTION(4,3,0)
#include "myfile.hh"
#else
#endif

You cannot call function in preprocessor! The only method to do something like this is to define pre-build command (.exe call) which may re-define .h or .c files.

Related

Conditional import in Objective-C

My app is in swift and has some files from other modules in objective-c. App has 2 targets, lets say Target-1 and Target-2. When I build the app, a file called as "Target(number)-Swift.h" is generated in build folder.
I need to import this file into the objective-c class. Since there are 2 targets and the file name depends on target name, I need to do conditional import based on which target I am running. I want something like:
if running Target1
#import "Target1-Swift.h"
endIf
if running Target2
#import "Target2-Swift.h"
endIf
If I directly add #import "Target1-Swift.h" it works fine when I build Target1, but fails when I build Target2. I tried the following:
Code I tried:
#ifdef TARGET1_SWIFT_H
#import "Target1-Swift.h"
#endif
With this I don't see errors on imports, but the I see errors like "Use of undeclared identifier 'class_name'" for the classes declared in "-Swift.h", so looks like "-Swift.h" file it not being imported.
Any idea how can I fix this.
You can use Preprocessor macros for this.
Assign a flag for only one target. And use that flag for a condition to determine which target you're running on.
You can use it in the swift file as follow.

C++ Builder include header file from other folder

I'm using C++Builder. I want to include a header file that is located in a separate folder from my project.
I tried to add the folder to the search path, and include the file in my project:
#include "GXWARE32\Include\gxutils.h"
but when I compile I have some errors in the file included
So, I tried to add all subfolders to the search path, and it works with a small folder but not with a big one.
According to the error log, maybe you missed some header file that gxutils.h relies on.
Without code, we can only guess... some libs need a specific #include order... some libs have hard-coded relative paths and by moving them you broke that... Some libs also need configuration macros defined before #include.
From the errors, you have #include'd some file more than once (and it's not protected by a header guard, like #pragma once or #ifndef file_id #define file_id ... #endif macros) and you are missing a previous #include for some datatype used.
Open the gxutils.h file and look around line 143 for the missing datatype. In the IDE, during compilation the cursor usually stops on the stuff directly. Then just search the files in your lib for the datatype, so you know what file to #include before...
All of these might happen sometimes if you include the wrong file... some libs need to include cpp instead of h...
Adding search paths will not do anything as the compiler is not complaining about files not found...

How to global import my own DLog macros without use pch file

I wrote my own macros to output detail message in development environment
#ifdef DEBUG
#define GCLog(fmt, ...) NSLog((fmt), ##__VA_ARGS__);
#else
#define GCLog(...);
I don't want to import this in every file, and I know the shortcoming of PCH file.
So what can I do with this?
You've got four options:
Import some file containing that macro in every file you want to use
it in
Put it in the PCH which is automatically imported for you
Put it in a file and import that file in the PCH
Include that macro in every file you want to use it (generally a bad plan)
Personally when I was writing Objective-C, I would go with option 2 when I wanted something available in every file.

Examine filetype in prefix.pch

I am thinking about that it would be a great idea for examining that the class type. In example I would like to do the following in my application prefix.pch file.
#if isViewController
#import "DeviceCompatibility.h"
#import "UIViewController+Utilities.h"
#endif
How could I do this.
Thanks for the ideas and your time
This won't work. The point of a pch file is that it is a "Pre Compiled Header" file. It's compiled once and then that is used in every other file.
What you are asking for would require that it not be pre-compiled since it would need to be evaluated for each file. What you want is what regular, non-pch files are for.

C++ precompiler conditionally include code

I have a few projects that share a lot of common code, but sometimes I need to not include certain parts of the common code depending on the project.
I've tried creating a separate file called project_names.hh, containing this:
// list of project names
#define FIRSTPROJECT 0
#define SECONDPROJECT 1
// PROJECT_NAME must be set to one of the above names in the project's main.cc file
#define PROJECT_NAME
Then in one of the projects' main files I do this:
#define PROJECT_NAME FIRSTPROJECT
The problem is that even though I include project_names.hh in another file, I can't seem to get this statement to compile:
#if PROJECT_NAME == FIRSTPROJECT
I get this error:
error: operator '==' has no left operand
Does anyone have a good way to do this?
Thanks!
Marlon
That's because you've defined PROJECT_NAME to be the empty string with your line
#define PROJECT_NAME
you want to change it to
#define PROJECT_NAME FIRSTPROJECT
This needs to be in a header file that all the files of that project #include.
Alternatively, you could get rid of the #define PROJECT_NAME and instead use
-DPROJECT_NAME=FIRSTPROJECT on the compiler command line for all the files in that project. Note that if the same file is used in multiple projects, you'll need to compile it mulitple times with different options, and have it put the output in different places...
If I were in your situation I would simply define either FIRSTPROJECT or SECONDPROJECT instead of setting PROJECT_NAME to either of those values. I would then use #ifdef to check whether that value is set.
rather than #define PROJECT_NAME FIRSTPROJECT,
use #define FIRSTPROJECT,
then check its existence with #ifdef FIRSTPROJECT
You should include project_names.hh in the file in which you're running the #if PROJECT_NAME == FIRSTPROJECT. The preprocessor might not have loaded and executed the statements setting PROJCET_NAME in the first place.
This is most probably because the PROJECT_NAME isn't set. You should check, which file is being compiled and check if the #define is set there.
It might help to set the define as a compiler option for the whole building process. For most compilers that I know (gcc, MSVC, clang, xlC), the compiler option would be
-DPROJECT_NAME=FIRSTPROJECT

Resources