Compile Objective-C with clang on macOS Sierra - objective-c

I'm trying to figure out how to compile this snippet of code on macOS Sierra.
#import <Foundation/Foundation.h>
int main() {
NSLog(#"Hello World");
return 0;
}
On El Capitan, I could compile with this command.
clang -x objective-c -framework Foundation main.m
However, when I try that command on Sierra, I see these errors.
In file included from main.m:1:
In file included from /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10:
In file included from /System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5:
/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:44:12: error: unknown property attribute 'class'
#property (class, readonly) BOOL supportsSecureCoding;
This is the version of clang that I'm using.
$ clang -v
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin16.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
I have Xcode version 7.3.1 (7D1014) and Xcode version 8.0 beta 4 (8S188o) installed. The version of Sierra I have is 10.12 beta (16A254g).
Note: I want to compile this in the terminal with clang, not inside of Xcode.

Make sure you have the Xcode 8 version of the command-line tools selected.

Objective-C gained support for class properties within the new version of Clang and Xcode. Here is and article where you may find some useful information: Objective-C Class Properties.
To solve this problem you should simply install Xcode 8.
UPD
Forgot to mention:
After installation make sure that you have switched command line tools to the recent Xcode:
$ clang --version
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ xcode-select -print-path
/Applications/Xcode.app/Contents/Developer
$ sudo xcode-select -switch /Applications/Xcode-beta.app/Contents/Developer/
$ xcode-select -print-path
/Applications/Xcode-beta.app/Contents/Developer
$ clang --version
Apple LLVM version 8.0.0 (clang-800.0.33.1)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Related

How to change compiler, which is detected by meson?

On my pc I have compiler gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0.
I would like to compile gstreamer source code, where is meson.build.
In meson.build, there is line:
cc = meson.get_compiler('c')
In user's path I set CC to gcc. When I start meson using command:
meson /path/to/directory I see that meson select gcc (gcc 8.1.0 "gcc (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0"). How can I change that?
I tried cc=gcc too.
My cpu is AMD 64bit.

Objective C compiling with clang on Linux Foundation/Foundation.h linking: file not found

I'm trying to compile a program in Objective C on Ubuntu, I have installed GNUstep, set all the GNUSTEP_* environment variables, and installed clang, because I read that gcc can't compile Objective-C code with blocks (Objective-C 'anonymous' functions, just to be clear: ^ { }).
What I get when I run this command:
clang hello.m -o hello
Is:
hello.m:1:9: fatal error: 'Foundation/Foundation.h' file not found
#import <Foundation/Foundation.h>
^
1 error generated.
I tried this also:
clang -L '/usr/include/GNUstep/Foundation/Foundation.h' hello.m -o hello
Where /usr/include/... is the path to the Foundation.h file on my system; but I still get the same result.
How can I link those header files automatically and compile Objective-C?
Thanks!
You can use gnustep-config:
clang `gnustep-config --objc-flags` `gnustep-config --objc-libs` hello.m -o hello

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.

Discrepancy with issues found in scan-build vs. xcode

I am using scan-build (checker-258) from the command line to do static analysis on my iOS project and find that uncovers far fewer issues than xcode (about 60% less). If I set xcode 4.2 to use scan-build from checker-258 it finds all the issues (and more). This may be because the command line version us using the old (not modern) run time as it is finding issues like:
error: synthesized property 'foo' must either be named the same as a compatible ivar or must explicitly name an ivar
#synthesize foo;
^
Here is the command I'm using to run the analysis:
scan-build --use-cc=`which clang` -k -o scan-reports xcodebuild -target MyTarget -project myproject.xcodeproj -sdk iphonesimulator5.0 -configuration Debug clean build
Thanks in advance.
Yes, the version of the static analyzer that ships with Xcode 4.2 is older than the version on the clang website. There are instructions here on how to use the newer version within Xcode: http://clang-analyzer.llvm.org/xcode.html
Try to use this command: scan-build -k -V -o scan-reports xcodebuild clean build -configuration Debug -sdk iphoneos5.0 -xcconfig="myConfig.xcconfig"
Where myconfig contains the CODE_SIGNING_IDENTITY="", PROVISIONING_PROFILE=""

How to compile objc code on Linux?

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