Error message "could not build module 'Foundation'" - objective-c

I searched for this question and could not find much help.
Error:
could not build module 'Foundation'
#import <Foundation/Foundation.h>
What is the problem?

I was able to solve this using the solution provided in this Apple Support Communities thread:
The real problem here is at Build Settings in the session: Apple LLVM 5.0 - Language - Modules, we should set Enable Modules (C and Objective C) to NO

The suggested fix to set Enable Modules (C and Objective-C) did not solve this issue for me.
What did is renaming my someFile.c files to someFile.m. Even though those files contain just C functions (that do use Foundation types), naming them .c produces this error.

I found that if you use some external C / C++ code in your project, you have to remove all the #import in the prefix. That's quite a headache, but it's a true problem.

Cmd + Option + Shift + K and then Cmd + Option + K solved above error for me.

In my case I had a Precompiled Header where I had includes that included <Foundation/Foundation.h> The solution for me was to wrap the include in a
#ifdef __OBJC__
#include SomeIncludeWithFoundation.h
#endif
You may also see in your .pch files something like:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#else
#ifndef FOUNDATION_EXPORT
#if defined(__cplusplus)
#define FOUNDATION_EXPORT extern "C"
#else
#define FOUNDATION_EXPORT extern
#endif
#endif
#endif
Be sure your modules are included in the right place in your Precompiled Header

None of the given solutions worked for me.
I have a project with mixed source codes (C and Objective-C) and the problem was given by a .c which included a shared .h which in turn included the Foundation header:
#import <Foundation/Foundation.h>
In order to solve the problem I had to perform the following steps:
In the .c that was causing the problem add as first include/import statement: #import <Foundation/Foundation.h> You can keep the same import statement in the shared header if you need.
In the project settings select the entry under Targets then click
on Build Phases -> Compile Sources. In the Compiler Flags
add -x objective-c. This will force the compilation of the C source code as Objective-C to allow the linking with the Foundation framework and since Objective-C can embed C source code this is perfectly legal.
In the project settings select the entry under Targets then click on Build Phases -> Link Binary With Libraries. Add the Foundation.framework library in the list.

You can try this:
In your .pch file, write like this:
#ifndef PureStandard_PrefixHeader_pch
#define PureStandard_PrefixHeader_pch
#ifdef __OBJC__
#import "A.h"
#import "B.h"
#endif
#endif

I have resolved by change Build System to Legacy Build System
Open the ios/PROJECT_NAME.workspace file
Then in the top menu, select File → Workspace Settings
Then change Build System to Legacy Build System

From this answer:
Set Allow Non-modular Includes in Framework Modules to YES in target's Build Settings
I was seeing the issue on this line in a .m file that is being built as part of an extension:
#import <Foundation/Foundation.h>

So I had the same problem, but the errors would for some reason not cause the build to fail, so they were like fake errors, and I made them go away by quitting Xcode, and deleting ~/Library/Developer/Xcode/DerivedData/ProjectName

Just rename your Objective-C++ file from *.c to *.mm.
The errors are gone that way. It handles all the imports just fine this way.

Use the following:
Cmd + Alt + Shift + K and then Cmd + Alt + K
Menu File → Workspace Settings → Legacy Build System
Cmd + B
It works for me.

Related

Swift's canImport analogue in Objective-C

Swift 4.2 has a special condition canImport that helps developers to check whether a module can be imported in project. It was introduced in Swift 4.1.
Now I am working on iOS project written in Objective-C. I use modules, and for each target these modules are different. That's why I want to use something like that:
#if canImport(SomeModule)
#import SomeModule;
#endif
How can I solve this problem? Now I use different "Other C Flags" for each target, but I want to find more flexible solution.
This is a little late as an answer, but i came across this issue while working on a similar case.
I used the __has_include(<SomeModule/SomeModule.h>)
Importing your framework:
#if __has_include(<SomeModule/SomeModule.h>)
#import <SomeModule/SomeModule.h>
#define __HAS_SOME_MODULE_FRAMEWORK__
#endif
Later in your code :
- (void)doSomething {
#ifdef __HAS_SOME_MODULE_FRAMEWORK__
// with SomeModule framework
#else
// without SomeModule framework
#endif
}

Glew not compiling in Xcode

I tried using the Glew Library for shading and was trying to compile a simple program using the NSOpenGLView Class in Xcode 5. The program fails at the following code in glew.h
#if defined(__gltypes_h_)
#error gltypes.h included before glew.h
#endif
It says that Gltypes.h is included before glew.h.
My implementation file for OpenGLView has headers included in following fashion:
#include <glew.h>
#import "OpenGLView.h" // Includes Cocoa.h
#include "LoadShaders.h" // Includes <OpenGL/gl.h>
So, if glew.h is included in the beginning, why is the error coming here. None of the other headers are included before the inclusion of glew.h, so tests for them(like gl.h) are passing in glew.h. I am not able to find out here as to who is including Gltypes.h in this file.
Hope anyone has a clue to it here.
The precompiled header file (.pch) pulls Cocoa in. Edit it to include glew.h before Cocoa.h and you are good to go.

xCode 4.4 does not get all the .pch file headers imports?

This is my .pch file -
//
// Prefix header for all source files of the 'English Club' target in the 'English Club' project
//
#import <Availability.h>
#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "Helper.h"
#import "Animations.h"
#import "Constants.h"
#import "SoundPlayer.h"
#import "UAirship.h"
#import "UAStoreFront.h"
#import "UIIMagesNames.h"
#import "UIView+Sizes.h"
#import "HelpButton.h"
#import "SoundPlayer.h"
#import <RestKit/RestKit.h>
#import "UIHelpSoundFiles.h"
#endif
Still it is very frequent that Xcode will give me errors that he can not find those classes. (Helper for example).
The thing is that the project will be compiled and work fine but I have hundreds of errors that disturb me looking for the real ones.
Any idea why?
This appears to be a known issue in Xcode 4.4 (as seen in the release notes)
It is suggested to Delete the PCH index folder to workaround this issue.
To locate this folder, in Xcode, open the Organizer and select the Projects tab, then select the project in the left-hand pane. You should see the Derived Data path for this particular project with a small arrow to Show in Finder. If you click on this arrow, you will be taken to the right location in Finder. If you then navigate to the Index subfolder and delete PrecompiledHeaders folder, you should be all set.
Xcode should re-index, and re-create this folder, but the errors should be gone.
I found that just updating your pch file (add and delete space) and rebuild will cause xcode to fix those problems
I know this is an old post but I wanted to share my experience.
Im running Xcode 6.1 the fix was to delete the Tests target. for example MyProjectNameTests.
Also check if both your app.pch and the app_Test.pch have the required imports. Sometimes you might be staring at the main pch, but the errors are for the unit tests pch.
Xcode 5 Update:
You can now delete derived data straight from Xcode Organizer. Delete, clean, and build solved the problem for me.
Also one of the problem could be that .pch file is not set for a target. To do that do the following:
1. Go to the target settings.
2. Option Precompile Prefix Header set to YES.
3. For Prefix Header add a project relative path or an absolute path.

How to put macros and constants into .pch file in an iOS project?

I heard that the .pch is good for putting macros and constants definitions, so for its default content:
#import <Availability.h>
#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
where should the definitions be put in? It is strange that it seems to be importing UIKit.h, but ViewController.h also import that same file (I thought the .pch is imported by all files by default? So ViewController.hshouldn't need to import UIKit.h again)
Precompiled headers are meant to improve overall compile times for projects, so the design of precompiled headers is entirely driven by performance concerns.
The use case for precompiled headers is relatively simple: when there is a common set of headers that is included in nearly every source file in the project, we precompile that bundle of headers into a single precompiled header (PCH file).
The .pch file allows you to import files like UIKit.h and Foundation.h(rather than importing them in every .h of your project) If you have those files imported in the .pch, in your own classes don't need to import them.
The significance of #ifdef OBJC is so that you don't import headers containing objective-c code if you don't have the compiler set to build objective c code (hence avoiding lots of compiler errors).
Any extra imports I usually put in with:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
You are correct, there is no need to import one of those again, but there is no harm in doing so.

Adding pre compiled headers in Xcode 4 Objective-C

This question is related to this:
Constants in Objective-C
I would like to add pre compiled headers to my project to store app constants. I want to this as an alternative to having a constants.h file and importing it wherever it's needed. Is my thinking correct here?
On the above thread a guy mentioned modifying "YourAppNameHere-Prefix.pch".. I'm not sure this is a good approach.
How exactly can I create my own *.pch file and add it to my Xcode project so I can store application level constants?
I have tried googling/searching here for this but I'm just getting Objective-C++ and Clang stuff.. I'm not sure this is relevant.
Cheers,
Conor
My answer probably not just in time, and especially for Xcode 6 but i hope i'll help other, who already want's to create it's own constant.h file and set it on all of project objects.
So
Create constant.h file by NSObject.
Put all of constants you need in .h file before #interface:
#import <...>
#define kSomeFirstConstant 1 //where 1 is an integer value
#define kSomeSecondConstant 2 //where 2 is an integer value
#interface Constant: NSObject
Create precompiled header file .pch (eg: precompiledFile.pch)
Put #import "constant.h" in .pch file right between #define and #endif:
#define ..._pch
#import "constant.h"
#endif
Configure prefix name and some other options in Project navigator, Building Settings of your project target:
Apple LLVM 6.0 Language
Increase sharing of Precompiled headers - NO
Precompile prefix header - YES
Prefix Header - TargetName/precompiledFile.pch (eg: MyTarget/precompiledFile.pch)
After that, run building process and all of your constant in constant.h file will be accessible in all of your project objects.
Also read this: http://qualitycoding.org/precompiled-headers/