I'm trying to create a bundle application using Xcode 10.3.
The main code is going to live inside a C++ file, which I added to the bundle project, and I also need to use some Cocoa related stuff.
and here is the catch, anytime I include
#include <Cocoa/Cocoa.h>
I immediately get like 20 errors
which are rather strange. Is there anything else to be added or set in Xcode so I can include Cocoa headers inside a CPP file?
Looks like the solution was easier than expected, you just have to change the compilation type for the cpp file from C++ Source to Objective-C++ Source and then everything compiles as needed.
Related
I have started a new solution under VS that has both a managed, UWP code project and a native project. The compiler compiles the native code and delivers a .lib file just fine. When compiling the managed code, the compiler compiles the native code again -- as managed code and spits out errors.
I have another solution that use to work and I have tried to replicate the settings. After a day of comparing the property settings, I cannot figure out why it's doing what it's doing.
Ideas for where to look?
_____ New below_____
I have started a new solution an project and replicated everything. Here's what I've learned.
First, the compile order is different -- there was a missing stdafx.h file and the errors went from infinite to just 25. They are now related to one file, MotionBase. The native project compiles just fine, then the managed project compiles and it barfs on MotionBase. This code sample gives errors "'MotionBase': is not a member of 'BallLib'" and"'input': unknown override specifier".
#pragma once
#include "stdafx.h"
#include "FiniteDiffHelpers.h"
#include "MotionBase.h"
#include "MultiVarSolver.h"
namespace BallLib {
class PathFinderHelper : public FiniteDiffHelper
{
public:
PathFinderHelper();
Line locs;
BallLib::MotionBase output;
MotionBase input;
.....
PathFinderHelper compiled fine in the native project. There are no errors in MotionBase. MotionBase is part of BallLib. Intelisense gives no errors in PathFinderHelper.
Ensure the stdafx.h files are properly inserted in the code. The build order is different so you may get trapped.
Include (#include) the native stdafx.h in the managed pch.h file.
Not all the relevant settings are in the project properties dialog. Also check the properties non-modal window, particularly with the project-to-project reference selected inside Solution Explorer.
There you will find a setting named "Use Library Dependency Inputs" which causes the main project to include the individual source files from the library project, instead of the static library. Make sure this is set to False.
I have a question about using C++ header files in Objective-C++ modules in Xcode. Specifically, why can I #include them in source files but not header files?
Here is a specific example.
I'm using Xcode 7.2.1 and have two projects. The first is a C++ framework I package into "myFramework.framework". It exposes "myFramework.h", which in turn pulls in "myLib.h". At the top of "myLib.h" is an "#include <string>".
The second project is an Objective-C iOS app which consumes the above framework. In this project, "myViewController.mm" (Objective-C++ source) has "#import "myFramework/myFramework.h" at the top and makes reference to things defined in that header file.
At this point all is well and good. It builds and runs with no issues.
When I move the "#import myFramework/myFramework.h" line to "myViewController.h", the compile fails because it cannot locate the "" header dependency.
It doesn't matter if I change the file type for "myViewController.h" to Objective-C++ header from plain old "C Header". Either way, Xcode's header search paths don't look for standard C++ headers.
So my main question is why does it behave this way? Why is a #include/#import treated differently just because it's in a header file?
My second question is if there's some way to make Xcode treat the #include/#import the same when it's in the header file instead of the source file?
Thanks much!
Are you sure that you get the error while compiling the myViewController.mm file?
Check if myViewController.h is imported into some other, non ObjC++ file (and that that one is the file that fails to compile).
I suspect the issue with including C++ headers inside other headers is that an Objective-C source file gets to see the C++ header file, which upsets it.
If you have mixed C++/Objective-C++/Objective-C then you are probably better off only exposing a pure Objective-C interface to other modules in the project and include any C++ header files in the Objective-C++ source files only.
Alternatively make everything Objective-C++ and then you don't need to worry about it at all.
Hopefully this answers your second question as well.
I included a custom header file, with a "test" function defined in it, as I am learning to include custom header files.
I was able to add the directory of the header file to the compiler "include" list, and Eclipse built the project fine.
However, when I try to run it, I get an error saying that it can't find the header file anymore.
use include like this:
#include "../HeaderFolder/header.h"
I'm getting include not found compile error in XCode. I have an iOS app project that i use Objective-c and c++ as mix.
Initially, i created one .h file and one .cpp file in my ios project. Then, I renamed the .cpp file to .mm file.
Here is my .h file;
TestLog.h
#ifndef CalculatorDemo_TestLog_h
#define CalculatorDemo_TestLog_h
#include <string>
using namespace std;
class TestLog
{
private:
string logString;
public:
void Log(string logMessage);
};
#endif
TestLog.mm
#include "TestLog.h"
void TestLog::Log(string logMessage)
{
//this->logString->append(logMessage);
}
What am I missing? Do I need to add std c++ library to my targetS? Something related to Search Header Paths?
I just need to use string type.
Thanks much for in advance
select project -> build setting -> apple LLVM compiler 5.1 -> language
In Compile Sources As change to Objective-C++
There's a quirk in XCode. I noticed it in 7.3. Most projects recognize .mm files and the STL, while one project I had did not. The fix was that I had to click on the top-left project icon, then click Targets > Build Phases > Link Binary with Libraries > and add in AppKit.framework. Next, I had to click Targets > Build Settings > search on "Compile Sources", and set it to "Objective C++" on all possible columns. Then, do a Clean and then a Build from the Product menu. This compiled properly then. Then, go back to that Compile Sources again and set it back to "According to File Type" on all possible columns. Then, click Build from the Product menu again. At that point, it compiled properly and allowed me to utilize the "according to file type" option, which I like better.
Oh, and if doing Cocoa stuff, don't forget to add the following header in your files:
#import <Cocoa/Cocoa.h>
And if doing command line stuff, don't forget to add the following instead of the Cocoa header:
#import <Foundation/Foundation.h>
i believe you need to include the whole path to the library. similarly to say "foundation" & "uiview" frameworks.
#import <Foundation/Foundation.h>
or
#import <UIKit/UIKit.h>
and yes, make sure you add the library to your target.
So I was having this issue with the Cocoapods library Bypass and none of these solutions did anything. The problem was with a file which Cocoapods creates called an umbrella header. This is located in <POD_NAME>/Support Files/<POD_NAME>-umbrella.h. Delete it, and it should build just fine.
Now for the explanation of why this is necessary: the umbrella header is mixing both C++ and Objective-C code directly in a header which is a big no-no apparently and ends up completely breaking the C++ imports. By removing it (which seems to have no effect?) this conflicting import which Cocoapods unknowingly created will go away.
Ran into this with xcode 12.4 with a project that is objective-c, but where I need one C++ modul. Solution: wrap the contents of the .h file in:
#if defined __cplusplus
declarations
#endif
Apparently xcode is not good at detecting a mix of sources.
see Expected ; after top level declarator, error in xcode
This often happens when Xcode doesn't understand that a C++ header file you've imported into Objective-C is actually for C++ code.
So you can solve this problem by finding the Objective-C file that imports C++ code, and simply change its extension from .m to .mm
I've been using a C++ library without problems on projects built with Xcode 3, but I'm now getting build problems on projects built with Xcode 4.
Drop the library into the Xcode 4 project and it builds fine, but as soon as I #include it, I get a "Lexical or Preprocessor Issue" error, more specifically " 'string' file not found, on line 4 of its main header file.
On closer inspection, the error specifies that 'string' file not found in ~/my project's directory/include/mainheader.h
I've tried the solutions listed here, but none worked.
So it thinks that header file is in my project directory, but it's obviously a C/C++ header… How can I tell Xcode to look for these C/C++ headers?
The problematic #include was at the top of my ViewController.mm, which I had already turned into Objective-C++ by giving it .mm as its extension. But ViewController.mm gets eventually imported by AppDelegate.m, which is Objective-C by default – and I had forgotten to make it Objective-C++, hence the problem.
So renaming AppDelegate.m to AppDelegate.mm solved the problem.
I think #include is a c++ class template..so you need to used using namespace std in your header file and also rename your source file .m format to .mm format.
it works for me :) try this...