Objective-C ARC was enabled in PCH file but is currently disabled - Error - objective-c

I am trying to convert my Objective-C Project to ARC. Once I add the -fno-objc-arc compiler flag to one or more individual files in Build Phases, my MyProject-Prefix.pch file shows the following error:
Objective-C automated reference counting was enabled in PCH file but is currently disabled.

You have to make sure that :
Compiler is set to Apple LLVM compiler 4.x
Project property : CLANG_ENABLE_OBJC_ARC is enabled (change it to YES)
You have cleaned your project (Product -> clean)
Following the steps above you will ensure that you have switched to ARC using the LLVM/clang compiler, if none of the above will solve the problem you can try disabling precompiled prefix unchecking Precompile Prefix Header (GCC_PRECOMPILE_PREFIX_HEADER) in XCode Build phases. The use of the PCH is not mandatory since is used to increase compilation time.

Related

How to enable -fcxx-modules flag in Xcode?

According to discussion here, I used .mm in my Xcode project and I need to enable -fcxx-modules flag.
I have Xcode8.3 and how to enable -fcxx-modules flag.
There are a number of possibles methods:
In the Target->Build Phases -> Compile Sources add
-fcxx-modules to the Compiler Flags column of the source files where modules are used
If it is always required, consider adding it to the Target->BuildSettings->Other C++ Flags

Build Objective-C Library with CMake with ARC enabled

I am trying to build an Objective-C ARC enabled library using CMake. When using the "Unix Makefiles" generator I run into a warning:
method possibly missing a [super dealloc] call
I don't run into this warning when using the XCode generator. Is there a flag I can pass to CMake to make sure that the command line build also recognizes this to be an ARC build and not have that warning generated?
Thanks
You need to let CMake know that you want to build the project with ARC. Otherwise, it will show the warning.
Option 1
However, CTSetObjCArcEnabled is only available only if we have cmake-toolkit installed. If it is not installed, you can use the following:
set_property (TARGET target_name APPEND_STRING PROPERTY
COMPILE_FLAGS "-fobjc-arc")
Option 2 (deprecated since 3.0)
Use CTSetObjCARCEnabled. Reference is available here:
Enables or disables Objective-C Automatic Reference Counting on a per-directory, per-target or per-source basis.
CTSetObjCARCEnabled(<value>
<DIRECTORY | TARGETS targets... | SOURCES sources... >)
Useful Tip
Also, as recommended from this answer, it is helpful to use the following to make sure the project is compiled with ARC enabled:
#if ! __has_feature(objc_arc)
#error "ARC is off"
#endif
An alternative approach is to specify per-target compiler flags. This could be considered more inline with modern CMake:
target_compile_options(target_name PUBLIC "-fobjc-arc")
Note: using PUBLIC will transitively forward this compiler flag to other targets depending on this one. Replacing by PRIVATE will prevent this propagation.
XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC works for me. See https://github.com/forexample/testapp/blob/master/CMakeLists.txt
set_target_properties(
${APP_NAME}
PROPERTIES
MACOSX_BUNDLE YES
XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES
)
Another option if you want all Objective-C(++) files to be built with ARC:
set(CMAKE_OBJC_FLAGS "-fobjc-arc")
set(CMAKE_OBJCXX_FLAGS "-fobjc-arc")

iOS xCode how to purge Objective C++ from a project?

I got a demo project that had a single Objective C++ source file. I removed the file and renamed all files that touched it from .mm to .m . However, xCode still thinks that there is Objective C++ code in my project and refuses to refactor class names, etc:
I tried deleting the file I want to refactor from xCode project using Delete > Remove references, then re-added it. The file has .h and .m, nowhere does it import any .mm files. xCode still would not refactor, complaining about objective C++.
How can I disable/ purge Objective C++ from an xCode 5.1 project if I already removed and renamed all files? Is there some build setting I need to flip?
Apple Update Objective-C version 2.0 then they enable feature to allow C++ language variant with GNU & clang Compiler with certain restriction
Anyway
Apple & development team know why remove Objective-C++
But in Current Objective-C++ project not open And you Stuck So simply Go to your Project Folder And Rename Objective-C++ project file extension from .mm to .m , still work as well
What Objective-C++ Remove

Adding C file causes pch error

I have an XCode project with objective-C files. If I add a new (empty) C file and try to build, I get a large number of errors right away while building a precompiled header, in ProcessPCH step. The errors are "fatal error: Could not build module 'Foundation'", and things in Foundation.h not being found. Remove the C file and it builds again. What is going on, and how to fix it?
(XCode 5.0, OSX 10.8.4)
EDIT I have tried Clean and Clean build folder, no effect.
EDIT Setting Precompile prefix header = No results in a bunch of syntax errors instead, in stuff like NSObject.h (and other Foundation framework header).
EDIT User Cy-4AH figured it out: there needs to be #ifdef __OBJC__ around the whole pch file.
Surround #import's with preprocessor directive #ifdef __OBJC__ #endif

How to compile specific files in objective-c++ and the rest of the project in objective-c

I'm currently busy on a project where I need to use an external accessory to read Mifare 1k tags.
The accessory was provided with an SDK, written in (Objective ?)C++ and I followed the instructions provided to set XCode to "Compile sources as: Objective-C++" and added "-Obj-C++" in "Other linkers flags.
The SDK compiles fine then, but trouble is I am already using several libraries in the project (such as ASIHTTPRequest, JSONKit, ...) and I get compilation problems because of those new settings in those libraries. If I switch back to the previous settings, I get compilation problems in the reader's SDK
The question is: is there a way to compile only the class from the SDK as C++ and the rest of the project as objective-c ?
Edit: the SDK files consists only of .h (and a linked library)
thanks for your help,
Mike
Select the file you want to compile as Objective C++ from the file navigator, and then select the File Type in the file inspector view. This is in Xcode 4, but there is a similar mechanism in Xcode 3.
Try renaming the files where you are including the library headers to myClass.h for interface and myClass.mm for implementation files. This forces the files to be compiled as objective-c++.
I have resolved this problem:
You should set "According to file type" to "Complile Sources As",
Set "-ObjC++" to the "Other Linker Flags"
The last,you should modify the files's suffix to .mm that reference
the library method
well, in Build phases tab, there is a section Compile sources. For file you want to use Objective-C++ compiler add flag: -xobjective-c++
tested in Xcode 12.5