hi I'm new to c++ coding and I'm asking how to create and include a header file in a c++ program on Android, I've looked online on how to create the header file but whenever I include it with the complete path the compiler gives me a fatal error file not found message,whether I'm using termux emulator or CXXDROID IDE, I don't know why this is the case.
please help me because I'm stuck with this problem for days and I need to solve it in order for me to write meaningful programs as I'm using Programming Principles and Practice Using C++ Second Edition by Bjarne Stroustrup which uses a lot of user defined functions for the projects.
thanks in advance.
I've included the complete path of the header files and created the proper headers,
This is how to do it in Termux. (And any other system/app where you have a c++ compiler)
Let's assume you have these files in your project.
main.cpp lib.h
Inside lib.h you have this code
int myfunction(int agr1);
Then inside your main.c file you can reference it like this
// Note the quotes
// Local header files need to be
// referenced like this with their
// relative path
#include "lib.h"
int main(){
...
}
int myfunction(int arg1){
// Implement your function here
}
You can now access your functions defined in your lib.h file from your main.cpp file.
I have a question and I could not find a solution, so I ask here :)
I want to create an Foundation-based terminal application/script using Xcode-->Mac-->New-->Command Line Tool-->Foundation
This works and all, but then I want to ADD header.h files to my project.
One of this header.h files does the following:
#include <iostream>
This fails with the error: iostream file not found.
For a test I make a new c++ based terminla script and it does exactly the same:
#include <iostream>
But for some unknown reason it does not fail with an error.
Can anyone tell me, why the c++ script works to include and the objective c not?
objective c: .h file
c++: .cpp file
I renamed the .h to .cpp but then it does not find the NSString and such things.. any solution to use iostream and objective c?
I really need that, thanks
If you want to use both Objective-C and C++ (called Objective-C++) from within the same source module, then use the .mm file extension.
I have a objective c/c++ project under iOS, moving it from OS/X and I get a 'file not found' error on
#include <string>
It's a clean project, and I've just added the files from the old project. Are the STL includes set up in XCode? A find produces a number of possibilities e.g.
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/usr/include/c++/4.2.1/debug/
but adding this to the search path just threw up more errors. Any suggestions?
(apart from don't use string - it's in house code I'm porting)
xcode 4.2.1, ios5.0 running on OS/X 10.7.3 and it's in a .cpp file, the code works fine on OS/X
Are you really sure <string> is included only from a .cpp file?
I just tested on a fresh project, by adding a .cpp file and including <string>, and it works, just as expected (same Xcode version, same SDK version).
But if I include <string> in a .m file, then of course I got a «file not found» compiler error.
So double-check this, as the error may come from here.
Do you include <string> from a .cpp file only, or from a .h file, intended to be used by a .cpp implementation?
Then maybe it's also included by a .m file, hence the error.
Also check your precompiled headers, if any, to see if you include some C++ stuff there...
Also remember, in that later case, that you can rely on the __cplusplus macro, when needed.
If you include a header in an ObjC file and it includes <string> then you hit errors like this. For all .m files XCode uses a C compiler (clang or llvm-gcc). For all .mm files it will use (clang++ or llvm-g++).
I suggest going through and renaming all your .m files to .mm. Including main.m to main.mm.
For me the reason was
MyHeader.h (which includes #include ) target was public. Changed it to project and it compiled.
For cocoa pod:
s.public_header_files = 'MyProject/Classes/**/*.h'
s.project_header_files = 'MyProject/Classes/MyHeader.h'
I'm trying to implement AQRecorder.h class from SpeakHere Apple Xcode project example, but even I rename my implementation class to ext. *.mm and put line with #import "AQRecorder.h" still getting error "Unknown type name 'class'; did you mean 'Class'?" and many others.
Which according to me means that it is not recognized as C++ class.
Any help will be appreciated.
I've just had this exact problem. I had a view controller using the AQRecorder class from AQRecorder.mm.
When I included AQRecorder.h in my view controller these errors occurred. It appeared to me because my straight objective-c view controller (named as a .m file) was including C++ header files the compiler was throwing spurious errors.
There are two solutions. The quickest is to rename the view controller class including AQRecorder.h to be a .mm file, in my case UIRecorderViewController from .m to .mm.
Or, move the following includes:
#include "CAStreamBasicDescription.h"
#include "CAXException.h"
Out of AQRecorder.h into AQRecorder.mm. This means that straight C++ style header files will no longer be included (by reference) in your plain Obj-C source.
Hope that helps, and makes sense.
In my case, this error was caused by cyclical "Import" statements in two classes: the header file for each class included the header of the other class, resulting in the Unknown type name 'ClassA'; did you mean 'ClassB'? error:
This is how my import statements were configured when I got this error. In ClassA.h:
Import "ClassB.h"
In ClassB.h:
Import "ClassA.h"
To fix it, I used the #class forward declaration directive to forward-declare ClassA in ClassB.h (this promises the pre-compiler that ClassA is a valid class, and that it will be available at compile time). For example:
In ClassA.h:
Import "ClassB.h"
In ClassB.h:
#class ClassA;
This fixed the Unknown type name 'ClassA' error, but also introduced a new error: ClassB.m: Receiver type 'ClassA' for instance message is a forward declaration. For example:
To fix this new error, I had to import ClassA.h at the top of the implementation file of ClassB (ClassB.m). Both errors are now resolved, and I get zero errors and warnings.
For example, I now have:
In ClassA.h:
Import "ClassB.h"
In ClassB.h:
#class ClassA;
In ClassB.m:
Import "ClassA.h"
Both error messages are now resolved.
i met the same error with you, hope my solution may help you. The Xcode compiler could compile objective-c & c++ in the "*.mm" file, so you may change all your filename which import "AQRecorder.h"(all direct & indirect) file with ".mm" postfix. But you may not do that, you may find that the relationship between SpeakHereController and SpeakHereViewController is some tricky, i just learned how he used it, that create the SpeakHereController object in a nib file, so SpeakHereViewController file is not have to import the "AQRecorder.h" file. my English is stupid, i hope my answer may help you.
IMPORTANT: Select "Compile Source As" variable in compiler settings and set its value to "Objective-C++".
It looks like this problem is impossible to resolve.
If it is possible to shift #include "myC++.h" into *.mm file then it works.
But if You need to use it from your objectiveC.h file it fails.
I guess this is bug from apple. There is a way to specify *.mm instead of *.m
but there is nothing similar to *.hh instead of *.h
I fixed this problem today.If you #include or #import a C++ *.h file or C++/OC mixed *.h file in YourHeader.h,you MUST have a YourHeader.mm . If not,then all your C++ file and C++/OC mixed file will show compile errors.
Using XCode, it's possible to use the "language" Objective C++, which allows you to mix Objective C and C++.
My solution maybe looks ridiculus, but in xcode 4.2,
To add Apple Audio examples codes, I moved all files individually, and it is worked as a charm!
I mean not dragging whole folder, drag one by one all individual file in a spesific folder.
I resolved this issue the following:
Originally, the files were placed inside the project, but not inside the the correct file structure.
YES, I am aware it is not an actual structre as it is only for visual issues, BUT when i moved the Header and the CPP file inside the PROJ folder all worked.
This problem can be solved by changing the following build settings:
Under Apple LLVM Compiler 4.2 - Language
C++ Language Dialect (Gnu++11 works for me)
C++ Standard Library (Libc++ works for me)
It is also necessary to name the .m files as .mm, if you are letting Xcode use file extension to decide how to compile each file.
From my own experience, following things should be taken care.
Select "Compile Source As" variable in compiler settings and set its value to "Objective-C++" i.e Build Settings->Apple LLVM 5.1 - Language->Compile Source As->Objective-C++ (in Xcode 5.1.1)
Change the relevant files which include a C++ header file from .m to .mm (sometimes you need to change all .m to .mm). This was answered by #Diziet above.
If you face incompatible type errors, explicitly do type casting of the required type. These errors might not have shown up when it was .m file.
Specifically, if you're compiling a NDK C/C++ code, and got:
Unknown type name 'jclass'; did you mean 'class'?
It is very likely that the file containing 'jclass' is #include-ed in .c/.cpp files (directly or indirectly) which are to be built into a library. And the solution is: remove that #include.
This is one of the common mistakes done : Circular dependencies.
Consider an example :
File : B.h
#import "A.h"
File : A.h
#import "C.h"
File : C.h
#import "B.h"
This introduces Circular dependency.
Just redeclare in C.h as :
#class B;
I keep having issues with including basic headers such as cmath. It is most prevalent when using example projects.
Example:
#include <cmath>
for instance gets a file not found, even though I can verify that the SDK I'm using has it:
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/usr/include/c++/4.2.1/tr1/cmath
I can sometimes work around the issue by importing directly to the file, but this doesn't always work.
#include </usr/include/c++/4.2.1/cmath>
What is the extension of your sourcecode file? .m or .mm? If it's .m, the compiler will assume you have a regular objective-C file, whereas .mm would imply an objective-C++ file. If its not a .mm file, the compiler may not be looking for C++ includes.