Overlap between Objective-C and MATLAB/Octave file extensions - objective-c

Do Objective-C or MATLAB/Octave have source file extensions besides .m? I ask because I'm putting Hello World programs in a single folder and I can't have two hello.m files.

The only way I see is to create subdirectories for each of the programs and put your files there. You can tell Obj-C to treat other extensions as Obj-C, but that would not solve the problem for the other programs.
Update
To compile any extension as Objective-C, use -x objective-c on the command line or Select Objective-C from "Compile Sources As" in the target build settings. Default is "According To File Type", which means that only .m files are compield as Objective-C.
FWIW, there are many subtle differences between Objective-C++ and Objective-C. I myself would use subfolders to separate the .m files, as I already said above.

A workaround would be to use .mm for Objective-C. .mm is used for Objective-C++ source files and it is a superset of Objective-C so it should compile fine.
This is by no mean a clean solution. A much better way would be to reorganize your folder into subfolders as suggested by Ruddy Velthuis, or simply to call your source files hello-objective-c.m and hello-matlab.m.

MATLAB cannot find files unless they have the .m extension (very important!)
Now I know very little about Objective-C, I presume it's a superset of C (just like C++ is). If so, and similar to C/C++, you should be able to pass any file name as input to the compiler:
g++ -c myfile.m.txt

Related

Where is the implementation of a framework or library added to an Xcode project?

When I add a framework to a project, using Cocoapods or manually, they contain only header files. From Xcode, using "Jump to definition" there is no definition (that means that there are no .m files).
When a function in a library is called, how does the compiler get its implementation if the .m file is not in the project?
To put it in a somewhat simplistic way, building an executable usually goes through at least these two stages:
compiling, which turns source code into binary code;
linking, which "glues" chunks of binary code into an executable.
Frameworks may include implementations in binary form, already compiled. These are then linked with your compiled sources. The compiler does not touch them in any way, and only needs the .h files that describe them. It's the linker's job to stitch them together with your code.
If you'd like to know more on the subject, the answers to the following questions contain more detailed information:
How does the compilation/linking process work? (The question is about C++, but C and Objective C go through mostly the same process)
What is compiler, linker, loader?

xcode import filename collision

Let's say I'm trying to use one and only one 3rd-party library in an xcode project - a pretty typical scenario, seemingly harmless.
I plug in the path location of my include files to the project's "header file search path" setting. I haven't even modified any code to make use of the code in the library yet.
It turns out that the project couldn't compile.
Xcode complained something like "Cannot find interface declaration for NSObject", which is pretty absurd. By examining the Build Result, the complained error comes from a header file of the 3rd-party library - it looks something like
So it is indicated in the Build Result that xcode is mistaken that Foundation.h is referring to the assert.h of that 3rd-party library instead of the iOS' built-in assert.h (4th sub-item)
Is there a way to fix the collision of the file names of #import include files?
(Needless to say, I'm new to obj-c -___-)

How do I add a call to FORTRAN code to a project in Xcode 4.5?

My company has a bunch of Fortran code. Traditionally, we compiled the code we needed into a .dll and called that .dll when we needed a calculation done. We are now trying to create an iPad app, which unfortunately means we can't just call a .dll anymore.
One of my coworkers have managed to make a simple Command Line Tool project, where we call a Fortran file to write "Hello, World" in the debugger. However, when I try to get it to work on view based iPad app, I get a bunch of linker errors saying the symbols I'm using cannot be found. I know that the Command Line Tool uses a .cpp file to actually run the Fortran, and I've seen many threads concerning calling .cpp files in an app, but all the ones I've seen are outdated, directly contradict each other, and their fixes don't work for me.
My question is, is there a more direct way to call Fortran straight from a .m file? If not, what do I have to do to take the working Command Line Tool and get it into an app?
UPDATE 1: following the tutorials posted in the comments, I have been able to create a .o file from my Fortran code. I can do a File-Add Files to add it in easily enough, but now how do I actually call it?
UPDATE 2: Okay, baby steps. I found out you can make a .a static library (I'll call it "new_library") from .o files ("source_file.o") using the Terminal command ar crv new_library.a source_file.o (you can do it for multiple .o files by just adding source_file2.o source_file3.o for as many .o files as you want - NOTE: make sure you use cd to get to the folder where the .o files are located). I'm pretty sure Xcode will work with .a files, but it seems a .h file is still needed to let the other files in the project (like the view controllers) make calls to what's in the .a file. I know I can make a new .a file from Xcode itself (New Project -> iOS -> Framework & Library -> Cocoa Touch Static Library), but when I've done it that way in the past, I just write normal .m and .h files, and when I build the new library it just mashes all the .m files into 1 .a. When I want to use the code in that .a in another project, I just import the .a and all the .h files into the new project, and I can call the methods in the .a file just as if I had imported all the separate .m files. So the next question is, do I still need a .h when my .a is made with the terminal instead of Xcode? If so, how would I make a Fortran header file? If not, how do I call my Fortran code in the .a?
Let's try to sum up things:
You need a FORTRAN cross compiler that can produce ARM code. GCC is probably a good choice. But you will need to build it yourself from the source downloads.
Compile your FORTRAN code and put it into a static library. This task is perform outside XCode, probably from the command line.
Write C header files for those FORTRAN routines that you wand to call directly from C. You might want to look at Fortran and C/C++ mixed programming or Tutorial: Using C/C++ and Fortran together. These pages explain how to map the C data types to FORTRAN data types and vice versa.
Add the C header files and the static library to the XCode project.
Import the C header files into your Objective-C files (.m) where required and call the FORTRAN routines as if they were C functions.
Build the iOS app with XCode.

Is every single file compiled in Objective-C?

I would like to know, which code is compiled when i build the project in Objective-C - every single line of code in my project, or only those, that are called from the main.c and then from the ones that are called from them?
I mean, does the compiler separate the project to the simply connected domains and compiles the one that is linked to the main, or it just compiles it all?
Thank you, guys!
The compiler does not perform semantical analysis on your code. It compiles exactly what you tell it to compile -- Xcode generally invokes the compiler in a way that it compiles every file into your application. However, it's unnecessary to compile/link the files from which no classes/functions are used; although not compiling files from which you use classes/functions results in a linkage error (that is, the compiler won't be able to find some symbols in the binary file while putting together the object code for the final executable).
All files in your project get compiled, except for the header files that are not included from any of the .m files, or headers the inclusion of which is suppressed conditionally.
A Xcode project consists of one or more targets.
For each target you can define, what *.m-files get compiled
if you add a new file to the project, you can specify to what target it will be added. (actually this is a place, where I often see, that the main target is not selected — beware)

Adding GLM to project in Xcode 4

I am trying to add GLM to a project in Xcode 4, but I cannot get it to compile. I have added the glm files to my project through the add files dialog.
I get a lexical/preprocessor issue and Xcode cannot find the file <cmath>.
I am not sure what I need to tweak to get this to build.
I have seen How do I add OpenGL Mathematics (GLM) to Xcode 4? already.
All you need to do is add the files to the project and #import "glm.hpp" (for Objective-C++; for simple C++ it should just be #include "glm.hpp").
A couple things to be careful of:
The OpenGL Mathematics library, when you download it, comes with a bunch of stuff you don't need (test code, extraneous utilities). Adding these to your project will result in compile errors which I could not get rid of. The only directory you need is the glm/ directory; you can delete the test/, util/, doc/, and bench/ directories. If you were trying to follow "How do I add OpenGL Mathematics (GLM) to Xcode 4?" and were still having problems, this may be the thing which was tripping you up.
The OpenGL Mathematics documentation tells you to include the or files. In Xcode 4, you should include them like "glm.hpp" or "*.hpp". Xcode will find the files no matter where in the project they are. Supposedly you can add a user-defined build setting "USE_HEADERMAP" and set it to "NO" to disable this, but I didn't have any luck with that.
And, just in case, note that your code files using the OpenGL Mathematics library must be Objective C++ files (ending in ".mm"), not the default/plain Objective C files (ending in ".m"). It is very much a C++ library after all... :-)
I hope that help. I was just working through this myself, and I haven't had the chance to really push this (e.g. I've basically just added a mat4 object or two and made sure things still compiled), but it seems to be working.
I ran into the same problem and I solved it remaning my ViewController.m to ViewController.mm. Change the extension to .mm tells XCode that the file may contain C++ code inside. The article Write Object-C Code explains this in the Classes and Objects section.