I'm pretty new to using Xcode with C++ development. For some reason, the compiler isn't treating .h files correctly. It doesn't color code correctly, nor does it highlight compile-time errors. I've tried looking through the settings to see if I can change this, and I found that it treats .h files by default as c-headers. I changed this to C++ header file in one of them to see if this would help, and then closed the project and reopened. Nothing.
Maybe try to rename your files to .hpp ?
Related
In code blocks their default file is main.cbp so I usually change it to main.cpp. But there doesn't seem to be a difference between their performances. But then again I just began coding in C++ so I'd like to know if there are any differences before I get too deep.
.cbp is the extension for a codeblocks solution file. Usually the project file will contain the .cpp file. chp files dont contain the actual source code but the procedure for codeblocks to associate files.
In a nutshell, .cpp contains the source code while cbp files dont.
Why do you have to know about that?
Answer: when passing source code, cpp file is the only format that can be opened for IDE other than codeblocks eg. Dev C++
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 am very new to c++ and am doing a tutorial. I have copied the tutorial exactly but on compiling get this error:
'String file not found'
for the line #include <string>;
Could someone tell me how to amend this?
Ok, so I changed the name of my file from .C to .cpp and this particular issue seems to have gone.
You seem to have found a solution, I'm adding this to clarify why this is happening. Some compilers integrated with IDEs treat .c files as C source code an .cpp (or .cc, .c++, etc.) as C++ code. As you compile a .c file, C++ support isn't included and a C compiler is used instead of a C++ one. And C doesn't have <string> (although it does have <string.h>, but that contains entirely different stuff).
It looks like your compiler isn't correctly or fully installed. The compiler should be able to find its own headers without further effort on your part.
Ok, so I changed the name of my file from .C to .cpp and this particular issue seems to have gone. However, I now get 3 Apple Mach-o Linker (Id) errors (?)
As this is different to the original questions I will close this and open a new one
Thanks for all the help!
check the location c:/...../include
If exist string file should reinstall compiler
I wrangled with this problem for a good 5 or 6 hours, pulling my hair out until I finally found a solution. I wanted to post this (not sure if there is a specific place to post solutions to unasked questions) as a solution for others who may run into the same difficulty.
I am coding a C++ project in Netbeans 7.1.1 running on Linux Mint Lisa, and kept on getting an "Undefined reference to XXX" error when trying to use a static variable. Coming from a background in C# and thus not very familiar with header files and the like, I searched for hours expecting to find a problem with the way I declared my variable or my class. I couldn't find anything...
Go to your nbproject folder in the files explorer in Netbeans. Open up the configurations.xml file. Scroll down to the logicalFolder tag with the attribute name="SourceFiles". Make sure that all your .h and .cpp files are included as itemPath tags within the logicalFolder tag! Netbeans correctly added some, but not all of my cpp and h files, hence the problem. Not sure why there would be this inconsistency, as all class and header files were added through the same Netbeans wizard. Anyways, with the configurations file changed, the makefiles were then updated and written correctly on the next build and all functioned as expected. Hope this can help somebody!
Just to add more details to Levi's answer (for anyone newish to this like me), click the "Files" tab on the left of the screen, where your list of files and folders is. This gives you a different list of files than the "Project" tab does.
Expand your project folder if it isn't expanded already. One of the resulting folders is "nbproject." Expand the folder. This is where configurations.xml is.
In short:
Files tab > nbproject > configuration.xml
However, adding the .h files to configuration.xml did not work for me.
In my case, I only had to include the source files as #include lines within other source files to make it work ... I could compile the individual source files without error, but the project was another story. Once I added the #include lines in other source files that required certain methods to be found, then it all worked as planned. Hope this helps someone.
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...