Is #define local (in xcode)? - objective-c

Can I put #define MY_VAR 1 in a file and #define MY_VAR 2 in a different file without any problem?
In other words, is #define a local or a global definition?

The answer depends on what kind of file you are talking about:
If it is a header file, then your #define-d constant is visible in all files including the header
If it is a .c/.m/.mm./.cpp file, then #defined constant is limited in scope to the file where it is defined.

Related

Expanding a variable, #cmakedefine, and generator expression in template file

I have the following template
config.c.in
#define QPID_DISPATCH_VERSION "${QPID_DISPATCH_VERSION}"
#define QPID_DISPATCH_LIB "$<TARGET_FILE_NAME:qpid-dispatch>"
#cmakedefine01 USE_MEMORY_POOL
And I wish to obtain the following, by expanding the variable, #cmakedefine, and generator expression.
config.c
#define QPID_DISPATCH_VERSION "1.6.0-SNAPSHOT"
#define QPID_DISPATCH_LIB "libqpid-dispatch.so"
#define USE_MEMORY_POOL 1
The problem is, there does not seem to be a CMake function which can expand all three.
What I can do is
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h.tmp)
file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/config.h INPUT ${CMAKE_CURRENT_BINARY_DIR}/config.h.tmp)
Is there a way to accomplish this without creating a temporary file? Cleaning up the file is tricky, because file GENERATE does not run immediately.
Based on #Tsyvarev's comment, I now have
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/config.h.in" CONFIG_H_IN)
string(CONFIGURE "${CONFIG_H_IN}" CONFIG_H_TMP)
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/config.h" CONTENT "${CONFIG_H_TMP}")

CMake configure_file replace ON/OFF with 1/0

Suppose I have two libraries ABC, XYZ and two variables ABC_FOUND XYZ_FOUND in CMakeLists.txt. In a config.h.in file, I have the following lines
#define __USE_ABC__ #ABC_FOUND#
#define __USE_XYZ__ #XYZ_FOUND#
If I do configure_file(config.h.in config.h), the output config.h file will be changed to
#define __USE_ABC__ ON
#define __USE_XYZ__ OFF
My question is how can I convert the ON/OFF options into 1/0, in other word, my desired output config.h should be
#define __USE_ABC__ 1
#define __USE_XYZ__ 0
Define ABC_FOUND to either 0 or 1 with
#cmakedefine01 #ABC_FOUND#
Use an intermediate variable in either environment to change it to USE_ABC.
Likely, you should use #cmakedefine instead of hard defines (see examples e.g. here)
But please remember that in the case of #cmakedefine you get a configuration header where "enabled features" (in terms of CMake if() command) are #define-d to a supplied value, while "disabled features" are #undef-ined. So you should test them not with #if <something> but rather with #ifdef <something>.

Difference between #import header file with <filename> and "filename" [duplicate]

I'm wondering what decides whether you're allowed to use <Header.h> or "Header.h" when you're importing files in Objective-C. So far my observation has been that you use the quote marks "" for files in your project that you've got the implementation source to, and angle brackets <> when you're referencing a library or framework.
But how exactly does that work? What would I have to do to get my own classes to use the brackets? Right now Xcode will not allow me to do that for my own headers.
Also, by looking in some frameworks headers, I see that the headers reference each other with <frameworkname/file.h>. How does that work? It looks a lot like packages in Java, but as far as I know, there is no such thing as a package in Objective-C.
Objective-C has this in common with C/C++; the quoted form is for "local" includes of files (you need to specify the relative path from the current file, e.g. #include "headers/my_header.h"), while the angle-bracket form is for "global" includes -- those found somewhere on the include path passed to the compiler (e.g. #include <math.h>).
So to have your own headers use < > not " " you need to pass either the relative or the absolute path for your header directory to the compiler. See "How to add a global include path for Xcode" for info on how to do that in Xcode.
See this MSDN page for more info.
In C, the convention is that header files in <> bracket are searched in 'system' directories and "" in user or local directories.
The definition of system and local is a bit vague, I guess. I believe it looks in system directories in include path or in CPPFLAGS for <header.h>, and local directory or directory specified with -I to compiler are searched for "header.h" files.
I assume it works similarly for Objective-C.
To import your own classes using "< >" you have to put the header files (*.h) in the lib folder of compiler or set a SYSTEM VARIABLES ponting to your lib folder.
#import <> vs ""
<Name.h> - Angle brackets tells to preprocessor to search in a special pre-designated system's directories. For example you import systems headers like <UIKit/UIKit.h> or added frameworks
"Name.h" - Quotation marks tells to preprocessor to search in a current directory. If a header was not found the preprocessor try to use <Name.h>. Usually you should use it with your project's files
Just stumbled upon the same problem, there are 2 types of search paths is Xcode:
User Header Search Paths
Header Search Paths
If you add your own include folders into Header Search Paths, you can use angled brackets without any problem.
Or set Always Search User Path to YES so you can use angle brackets.
With angle brackets e.g. <Foundation/Foundation.h> you import system files.
You use double quotes "Person.h" to import local files (files that you created) and to tell the compiler where to look for them.
If this is an Xcode project and you want to include it in a framework, have the header file you want to included open. Then, open Xcode's rightmost tab and under "Target Membership", click on the framework you want your file to available from.
e.g. If your framework is AlphaTools and your header, AceHeader, then you'll select AlphaTools on Target Membership so you can access < AlphaTools/AceHeader.h
WHAT IS HEADER FILE ?
Header files contain definitions of functions and variables which can be incorporated into any C program by using the pre-processor #include statement. Standard header files are provided with each compiler, and cover a range of areas, string handling, mathematical, data conversion, printing and reading of variables.
Ex- #include it contain the information about input like scanf(),and out put like printf() function and etc in a compiler.
INCLUDE
1) #INCLUDE:-
It is a pre-processor that process before process of main function.
The main work of pre-processor is to initialize the environment of program i.e that is the program with the header file.
2).h:-
(Header file) A header file is a file with extension .h which contains C function declarations and macro definitions and to be shared between several source files.
Q) There are two types of header files: the files that the programmer writes and the files that come with your compiler ?
A)In a angular brackets
Angular-bracket form is for "global" includes -- those found somewhere on the include path passed to the compiler (e.g. #include)
It is used for using of library function which is all ready define in compiler.
In C the convention is that header files in <> bracket are searched in 'system' directories 
B) Quote marks:- “header.h”
quoted form is for "local" includes of files (you need to specify the relative path from the current file, e.g. #include "headers/my_header.h")
In C the convention is that header files in " " are searched in user or local directories.
In it one file to be included in another .(FILE INCLUSION).
It can be used in two cases:
Case 1: If we have a very large program, the code is best divided int several different files,each containing a set of related functions.
Case 2: There are some functions and micros definitions that we need at most in all programs that we write.
Ex

Generating a VERSION_STRING from major/minor/patch/build defines for use in a plist

I have a plist that is processed with a precompiled header file and in it there is the "variable" VERSION_STRING used a few times in such fields as CFBundleGetInfoString, ie the value for the key CFBundleGetInfoString is: MyProduct VERSION_STRING Copyright © 2009 MyCorp
In MyHeader.h (which is the set as the Info.plist prefix header I would like to be able to build VERSION_STRING into the form MAJOR.MINOR.PATCH.BUILD where I have
#define MAJOR 1
#define MINOR 0
#define PATCH 0
#define BUILD 23
For some reason I seem to be incapable of doing this. I might just be having one of those moments
Turns out there is actually an Apple Tech Note on this and a solution to the very problem I was having. So, for anyone that may come across this and is having the same problems I was check out Technical Note TN2175: Preprocessing Info.plist files in Xcode Using the C Preprocessor
Take your plist file and rename it with an extra extension (perhaps a P?). Add #include "MyHeader.h" to the beginning of the file, and preprocess it in your build with the C preprocessor (usually cpp). You may need to filter out extra # lines, but I don't see why it wouldn't work.
By default, cpp should output to stdout, so adding a command like this might work fine:
cpp myplist.plist.P | grep -v '^#' > myplist.plist

#import using angle brackets < > and quote marks " "

I'm wondering what decides whether you're allowed to use <Header.h> or "Header.h" when you're importing files in Objective-C. So far my observation has been that you use the quote marks "" for files in your project that you've got the implementation source to, and angle brackets <> when you're referencing a library or framework.
But how exactly does that work? What would I have to do to get my own classes to use the brackets? Right now Xcode will not allow me to do that for my own headers.
Also, by looking in some frameworks headers, I see that the headers reference each other with <frameworkname/file.h>. How does that work? It looks a lot like packages in Java, but as far as I know, there is no such thing as a package in Objective-C.
Objective-C has this in common with C/C++; the quoted form is for "local" includes of files (you need to specify the relative path from the current file, e.g. #include "headers/my_header.h"), while the angle-bracket form is for "global" includes -- those found somewhere on the include path passed to the compiler (e.g. #include <math.h>).
So to have your own headers use < > not " " you need to pass either the relative or the absolute path for your header directory to the compiler. See "How to add a global include path for Xcode" for info on how to do that in Xcode.
See this MSDN page for more info.
In C, the convention is that header files in <> bracket are searched in 'system' directories and "" in user or local directories.
The definition of system and local is a bit vague, I guess. I believe it looks in system directories in include path or in CPPFLAGS for <header.h>, and local directory or directory specified with -I to compiler are searched for "header.h" files.
I assume it works similarly for Objective-C.
To import your own classes using "< >" you have to put the header files (*.h) in the lib folder of compiler or set a SYSTEM VARIABLES ponting to your lib folder.
#import <> vs ""
<Name.h> - Angle brackets tells to preprocessor to search in a special pre-designated system's directories. For example you import systems headers like <UIKit/UIKit.h> or added frameworks
"Name.h" - Quotation marks tells to preprocessor to search in a current directory. If a header was not found the preprocessor try to use <Name.h>. Usually you should use it with your project's files
Just stumbled upon the same problem, there are 2 types of search paths is Xcode:
User Header Search Paths
Header Search Paths
If you add your own include folders into Header Search Paths, you can use angled brackets without any problem.
Or set Always Search User Path to YES so you can use angle brackets.
With angle brackets e.g. <Foundation/Foundation.h> you import system files.
You use double quotes "Person.h" to import local files (files that you created) and to tell the compiler where to look for them.
If this is an Xcode project and you want to include it in a framework, have the header file you want to included open. Then, open Xcode's rightmost tab and under "Target Membership", click on the framework you want your file to available from.
e.g. If your framework is AlphaTools and your header, AceHeader, then you'll select AlphaTools on Target Membership so you can access < AlphaTools/AceHeader.h
WHAT IS HEADER FILE ?
Header files contain definitions of functions and variables which can be incorporated into any C program by using the pre-processor #include statement. Standard header files are provided with each compiler, and cover a range of areas, string handling, mathematical, data conversion, printing and reading of variables.
Ex- #include it contain the information about input like scanf(),and out put like printf() function and etc in a compiler.
INCLUDE
1) #INCLUDE:-
It is a pre-processor that process before process of main function.
The main work of pre-processor is to initialize the environment of program i.e that is the program with the header file.
2).h:-
(Header file) A header file is a file with extension .h which contains C function declarations and macro definitions and to be shared between several source files.
Q) There are two types of header files: the files that the programmer writes and the files that come with your compiler ?
A)In a angular brackets
Angular-bracket form is for "global" includes -- those found somewhere on the include path passed to the compiler (e.g. #include)
It is used for using of library function which is all ready define in compiler.
In C the convention is that header files in <> bracket are searched in 'system' directories 
B) Quote marks:- “header.h”
quoted form is for "local" includes of files (you need to specify the relative path from the current file, e.g. #include "headers/my_header.h")
In C the convention is that header files in " " are searched in user or local directories.
In it one file to be included in another .(FILE INCLUSION).
It can be used in two cases:
Case 1: If we have a very large program, the code is best divided int several different files,each containing a set of related functions.
Case 2: There are some functions and micros definitions that we need at most in all programs that we write.
Ex