How to compile objc code on Linux? - objective-c

Assuming you have your .h and .m ready on a Linux server, which command would you issue to GCC to have it compiled?

The relevant parts:
gcc -c -Wno-import List.m
gcc -o prog -Wno-import List.o main.o -lobjc
. . . make sure that the Objective-C library and header files (objc/Object.h) were installed when gcc was built.
Note that when linking Objective-C with gcc, you need to specify the Objective-C library by using the -lobjc switch.
See this link for more information.
Additional link with possible solution to the missing compiler issue:
Try installing either gobjc++ or gobjc
sudo apt-get install gobjc++

gcc -x objective-c file.m -o out
Google is your friend

Related

How can I properly configure the g++ include path with mingw64?

I have installed msys2/mingw64 because I need the g++ compiler. Now, I want to compile some c++ oce which requires openblas. I have installed the package using pacman -S mingw-w64-x86_64-openblas. However, compiling the code fails with
fatal error: cblas.h: No such file or directory
Clearly, the include path does not contain the headers from openblas which are located at C:\msys64\mings64\include\openblas. This is easy to fix by passing -I<include path> as an additional argument to g++.
Now, I was wondering whether there is an automated way to include include files/headers of installed packages in the g++ include path. The same problem also holds for libraries.
For example, pacman might be able to atomatically append these paths onto some environment variable which g++ checks.
The standard way to get compilation and linking options for a library on MSYS2 and other Unix-like systems is to run this command:
pkg-config --cflags --libs openblas
If you're just compiling, use --cflags by itself.
If you're just linking, use --libs by itself.
Here's an example Bash command you could use to compile a single-file program:
g++ foo.cpp $(pkg-config --cflags --libs openblas) -o foo

How do I get GLFW Vulkan surface creation working with mingw-w64

Problem
I'm trying to build and run a vulkan + glfw program with g++ on Windows. I installed gcc, glfw3, and vulkan using msys2's pacman.
When my program calls glfwCreateWindowSurface(instance, window, nullptr, &surface); it returns with VK_ERROR_EXTENSION_NOT_PRESENT.
What I did
Install msys2 and use pacman to install:
mingw-w64-x86_64-glfw
mingw-w64-x86_64-vulkan-headers
mingw-w64-x86_64-vulkan-loader
mingw-w64-x86_64-vulkan-validation-layers
Download part 5 of the vulkan-tutorial.com tutorial and build it using:
g++ -std=c++17 -O2 -g -c 05_window_surface.cpp -o main.o -lglfw3 -lvulkan
g++ -std=c++17 -O2 -g main.o -o main -lglfw3 -lvulkan
Following the tutorial's setup for Visual Studio works flawlessly however I would prefer to use a different development enviroment.
After looking into it more and finding this github issue: https://github.com/glfw/glfw/issues/1810, I have fixed my problem by changing my -lvulkan flag to -L/path/to/VulkanSDK/1.2.176.1/Lib -l:vulkan-1.lib

Problems when compiling Objective C with Clang (Ubuntu)

I'm learning Objective-C language. Since I don't have a Mac, I'm compiling and running my code within Ubuntu 11.04 platform.
Until now, I was using gcc to compile. I've installed GNUStep and all was working. But then I started to try some Objective-C 2.0 features, like #property and #synthesize, that gcc does not allow.
So I tried to compile the code with Clang, but it seems that it is not correctly linking my code with the GNUStep libraries, not even with a simple Hello world program.
For example, if I compile the following code:
#import <Foundation/Foundation.h>
int main(void) {
NSLog(#"Hello world!");
return 0;
}
The output of the compiler is:
/tmp/cc-dHZIp1.o: In function `main':
test.m:(.text+0x1f): undefined reference to `NSLog'
/tmp/cc-dHZIp1.o: In function `.objc_load_function':
test.m:(.text+0x3c): undefined reference to `__objc_exec_class'
collect2: ld returned 1 exit status
clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
The command I'm using to compile is
clang -I /usr/include/GNUstep/ test.m -o test
with the -I directive to include the GNUStep libraries (otherwise, Clang is not able to find Foundation.h).
I've googled my problem, and visited both GNUStep and Clang web pages, but I haven't found a solution to it. So any help will be appreciated.
Thanks!
The problem was that the library gnustep-base was not being used by the linker. So the solution to this was using the option -Xlinker, that sends arguments to the linker used by clang:
clang -I /usr/include/GNUstep/ -Xlinker -lgnustep-base test.m -o test
The statement "-X linker -lgnustep-base" made the magic. However, I had problems with this command related to the class that represents a string in Objective-C:
./test: Uncaught exception NSInvalidArgumentException, reason: GSFFIInvocation:
Class 'NXConstantString'(instance) does not respond to forwardInvocation: for
'hasSuffix:'
I could solve it adding the argument "-fconstant-string-class=NSConstantString":
clang -I /usr/include/GNUstep/ -fconstant-string-class=NSConstantString \
-Xlinker -lgnustep-base test.m -o test
In addition, I've tried with some Objective-C 2.0 pieces of code and it seems to work.
Thank you for the help!
You can try gcc compiler:
First of all install GNU Objective-C Runtime: sudo apt-get install gobjc
then compile: gcc -o hello hello.m -Wall -lobjc
You are not able to use ObjC 2.0 features because you're missing a ObjC-runtime supporting those. GCC's runtime is old and outdated, it doesn't support ObjC 2.0. Clang/LLVM doesn't have a acompanied runtime, you need to install the ObjC2-runtime from GNUstep (which can be found here: https://github.com/gnustep/libobjc2 ) and reinstall GNUstep using this runtime.
Here are some bash scripts for different Ubuntu versions, that do everything for you:
http://wiki.gnustep.org/index.php/GNUstep_under_Ubuntu_Linux
And please don't try to reinvent GNUstep make, instead, use it:
http://www.gnustep.org/resources/documentation/Developer/Make/Manual/gnustep-make_1.html
If you really don't think so, here is some excerpt from there:
1.2 Structure of a Makefile
Here is an example makefile (named GNUmakefile to emphasis the fact that it relies on special features of the GNU make program).
#
# An example GNUmakefile
#
# Include the common variables defined by the Makefile Package
include $(GNUSTEP_MAKEFILES)/common.make
# Build a simple Objective-C program
TOOL_NAME = simple
# The Objective-C files to compile
simple_OBJC_FILES = simple.m
-include GNUmakefile.preamble
# Include in the rules for making GNUstep command-line programs
include $(GNUSTEP_MAKEFILES)/tool.make
-include GNUmakefile.postamble
This is all that is necessary to define the project.
In your case replace all occurrences of simple with test and you're done
1.3 Running Make
Normally to compile a package which uses the Makefile Package it is purely a matter of typing make from the top-level directory of the package, and the package is compiled without any additional interaction.

g++ and gcc differences

I am trying to compile CPP code on an Ubuntu machine. I read somewhere that g++ is included in gcc. so in CodeBlocks I included the GNU GCC compiler. Codeblocks returned an error saying that g++ was not found. Is g++ another seperate compiler?
g++ is for compiling C++, gcc is for compiling C. Two different compilers for two different languages!
I'm not very familiar with g++ but g++ is a C++ compiler and C++ is an extension of the C language so all C code can be compiled with a C++ compiler. So you could say that g++ contains a C compiler but saying that g++ contains gcc isn't correct I think.
Both g++ and gcc programs are from the same free software project, GCC. However, on Ubuntu you have several different packages for them, so install the g++-4.6 or the g++ package with gcc-4.6 or gcc one. (if you don't install both, you won't be able to compile both C & C++).
Both programs can compile C and C++ files, assuming the C files are suffixed with .c and the C++ ones with .cc or .cpp (etc..).
But they won't do exactly the same things, in particular, they won't link the same default libraries.
To understand what they do, you can run
gcc -v -Wall -g myprog.cc -o myprog
and
g++ -v -Wall -g myprog.cc -o myprog
and you'll see the differences. The -v flag often means "verbose".

How to convert Obj-C code into a library

I have 3 apps written in Obj-C that I want to modify and convert to libraries, so I can use them in a Monotouch app.
Where do I find docs that tell me how to take Obj-C code and turn it into libraries?
Imagine you have a file called lib1.m
You will first have to compile it as object code. For instance:
gcc -Wall -framework Cocoa -o lib1.o lib1.m
That will create lib1.o
Then you'll have to decide wether you want a static or dynamic library.
To build a static library, you'll need a library object first:
glibtool --quiet --mode=compile gcc -o lib1.lo -c lib1.c
Then you can create the static library from the library archive:
glibtool --quiet --mode=link gcc -o lib1.la -c lib1.lo
To build a dynamic library:
libtool -dynamic -flat_namespace -lSystem -undefined suppress -macosx_version_min 10.6 -install_name /usr/local/lib/lib1.dylib -o lib1.dylib lib1.o
Note that for dynamic libraries, you must provide the install path when creating the library.