How do I write and compile Objective-C code? - objective-c

How do I write and compile Objective-C code in Linux?
I heard about Clang, but I couldn't find any example/tutorial about Clang/LLVM/Objective-C.

sudo apt-get install build-essential gobjc gobjc++ gnustep gnustep-devel libgnustep-base-dev -y
vi *.m
gcc `gnustep-config --objc-flags` *.m -lgnustep-base -lobjc

You download and build LLVM as the compiler infrastructure; then you download and build the CLANG front-end to compile C/C++ and Objective C/C++.
I guess you could try following this quick start guide. Mind that Clang could actually replace GCC when working with these languages.

Take a look at http://www.gnustep.org/ they have tools for developing with Objective-C 2.0 for LINUX. The source files and hints on how to install it on several LINUX distros are here: http://www.gnustep.org/resources/sources.html

Just run the following script shown on this page:
https://gist.github.com/nicerobot/5652802
Pay attention: During the first compiling process you will get an error. Solution is just to recompile it... It works !!! Here is the blog entry from the step by step installation but I really recommend the script!
http://blog.tlensing.org/2013/02/24/objective-c-on-linux-setting-up-gnustep-clang-llvm-objective-c-2-0-blocks-runtime-gcd-on-ubuntu-12-04/#comment-54284
regards

Related

Install gfortran using cmake

I would like install gfortran (or gcc or python for example) using cmake.
I know that I can write in my terminal:
sudo apt-get install gfortran
But I have to install a lot of programs in several computers and I would like write something like a "script" but in cmake in order to install all the programs "automatically".
I have looked for in tutorials and forums but I have not found anything.
Thanks

How to install and use libnet in mac os?

I want to program in C with libnet in Mac OS.
When I type in gcc *.o -o network -lnet, there's an error:
library not found for -lnet.
And when I use homebrew install linnet, is says:
Warning: libnet-1.1.6 already installed.
It is the problem of library PATH.Flow the steps to solve this problem.
First, find where you install the library. For my case, the libnet is located in /usr/local/Cellar/libnet/1.1.6.
Second, run /usr/local/Cellar/libnet/1.1.6/bin/libnet-config.
Third, use -L to add the path when you link.
gcc *.o -L/usr/local/Cellar/libnet/1.1.6/lib -lnet.

cmake for GNU Radio tutorial

I was trying to follow a tutorial on building the signal processing block on the gnuradio website: http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules.
However, when it comes to the step where it requires me to use CMake, the terminal is complaining that
package cppunit is not found checking for module 'cppunit'
-- package 'cppunit' not found
-- Could NOT find CPPUNIT (missing: CPPUNIT_INCLUDE_DIRS)
CMake Error at CMakeLists.txt:101 (message):
CppUnit required to compile howto
I have tried command such as sudo apt-get cppunit.
You need libcppunit-dev, which is a library for C++ unit testing:
sudo apt install libcppunit-dev
In fedora, try:
sudo dnf install cppunit-devel
In ubuntu, try:
sudo dnf install cppunit-dev
As #moggi mentioned, you need to install the cppunit development package for your distribution.
What's more important, that guide is currently becoming outdated; I'd advise you to follow the new Guided Tutorials. Also, if you build GNU Radio from source yourself by using PyBombs, it will a) automatically build the newest and greatest version of GNU Radio from source, and will b) automatically install all the build dependencies for you. Please make sure to uninstall all GNU Radio (and uhd, if any) packages that you've installed using your distribution, first.
If you using Debian, then
will output usefull info
apt-get cache search libcppuint
libcppunit-1.12-1 - Unit Testing Library for C++
libcppunit-subunit-dev - SubunitTestProgressListener for CPPUnit - Development headers
libcppunit-subunit0 - SubunitTestProgressListener for CPPUnit - C++ shared library
libcppunit-1.13-0 - Unit Testing Library for C++
libcppunit-dev - Unit Testing Library for C++
libcppunit-doc - Unit Testing Library for C++
That tutorial is very out of date! I suggest following https://wiki.gnuradio.org/index.php/Tutorials

How to make GNUstep libobjc2 to work?

I downloaded and built GNUstep libobjc2 1.6.1,
svn co http://svn.gna.org/svn/gnustep/libs/libobjc2/1.6.1/ rep
cd rep
make
and got this error when I tried to compile a Objective-C code.
hoon#ubuntu:~/work/objc2$ clang -fobjc-nonfragile-abi -fobjc-arc -fblocks *.m *.a -l pthread; ./a.out
Objective-C ABI Error: Loading modules from incompatible ABIs while loading
a.out: loader.c:38: __objc_exec_class: Assertion `objc_check_abi_version(module)' failed.
Aborted (core dumped)
It seems I need to build libobjc.a with some different configurations. What is the problem and what should I do to fix this error?
Env: Ubuntu 12.04 LTS
This is a good question because LLVM and CLang and GNUstep don't claim to be hard or complicated to use. They do ask their users to read (and reread) their documentation though. I've just discovered this combination for Ubuntu myself and it is very intriguing. And there are a lot of moving parts at the moment.
If the OP got an answer on one of the gnustep mailing lists, then it would be nice to hear here how it was resolved.
Without suggesting the normal route of starting over and only installing the latest from LLVM and GNUstep directly, here's my take.
The OP is trying to use ARC and blocks with the runtime provided by the gunstep/libobjc2 project.
libobjc2 should probably be built with clang version 3.2 - or maybe the top of tree.
clang -v will tell you what version you have on your machine. I've found even Ubuntu 12.10 doesn't provide clang-3.2. So I download it from the LLVM website. They have prebuilt binaries for Ubuntu 12.04 LTS. To get make to use clang for this build step, I've seen instructions to set and export CC=clang and CXX=clang++.
Once libobjc2 is built, care must be taken where it gets installed. If the system already has an older libobjc.so.x.y library (this provides the runtime environment for Obj-C), clang or the linker may be picking up the wrong one when you start compiling your own source. I found libobjc.so.4.6.0 built from the libobjc2 source was installed to /usr/local/lib both on Ubuntu 12.04 and 12.10 when I ran the make install step. This path was not setup by the libobjc2 install step.
To get this library picked up, I had to add /usr/local/lib to the env variable LD_LIBRARY_PATH. It wouldn't hurt to try 'locate libojbc.so' to see if there are other versions on the system.
Finally, the libobjc2 readme, https://github.com/gnustep/gnustep-libobjc2#readme, says this new version of the gnustep library supports two ABIs, and each ABI supports ARC and blocks. The advice I've seen about compiling with libobjc2 is to also provide the runtime version expected to be compiling and linking with: -fobjc-runtime=gnustep. Clang on Ubuntu still defaults to the older ABI. -fobjc-nonfragile-abi may accomplish the same thing but I've seen it mentioned that flag was or will be deprecated.
The OP error may actually come from the step where a.out is being run, rather than where clang was compiled. Perhaps the dynamic loader is picking up the legacy libobj.so. Again, I would use locate to see if the system even has more than one.
This is just a guess, but I'm gonna go out on a limb and guess that GNUstep is going to build with GCC by default. A good first step might be to reconfigure the GNUstep build to use Clang. I can't say what variety of issues that will expose, but...
Alternately, you could try building your application with GCC instead of Clang.
This is a fairly late answer, but I have found a way to get everything working (on Elementary OS specifically so I imagine it should be fine on Ubuntu and similar distros).
As mentioned, it is better/easier to build everything from source because you get to pick the prefix, build options, etc. The general method:
Install cmake (if it's not already installed) --> sudo apt-get install cmake
Download, build and install llvm and clang as per the instructions here (or see attached build script). For speed, use the --enable-optimized flag. Tests are "optional" ;-)
The default build scripts for compiler-rt do not include the Blocks runtime. You could modify the build scripts, or rejoice that someone else has already done it (particularly if, like me, your cmake isn't great). Download, build and install libBlocksRuntime from this git repo (it is essentially a clone of the latest compiler-rt Blocks runtime code with custom build scripts). FOLLOW THEIR COMPILATION AND TESTING METHOD.
Download, build and install libobjc2 from GNUStep. The instructions for correct building are actually inside the now-deprecated makefile.
Run ldconfig (if required - though there's no harm in running it even if it isn't).
As #WeakPointer you may still need to edit the LD_LIBRARY_PATH environment variable to pick up your new libraries. Another option is to make some quick and dirty symlinks so clang can find your new libraries.
Final note: I haven't done any comparisons between GNUStep's libobjc2 and GCC's libobjc, the latter of which DOES contain some Objective-C 2.0 features... Be careful with your library selection if you keep both on your system at the same time!
The following script should do it all (untested!):
#!/bin/bash
# Temporary base directories
basedir="./clangllvm_temp"
basedir2="./libBlocksRuntime_temp"
basedir3="./libObjc2_temp"
# Clang, llvm, compiler-rt
mkdir $basedir
cd $basedir
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
cd ../..
cd llvm/tools/clang/tools
svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
cd ../../../..
cd llvm/projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
cd ../..
mkdir build
cd build
../llvm/configure --prefix=$PREFIX --enable-optimized
make
sudo make install
cd ../..
# libBlocksRuntime
git clone https://github.com/mackyle/blocksruntime.git $basedir2
cd $basedir2
sudo ./buildlib
sudo ./checktests
sudo ./installlib
# Test!
clang -o sample -fblocks sample.c -lBlocksRuntime && ./sample
cd ..
# libobjc2
https://github.com/gnustep/gnustep-libobjc2.git $basedir3
cd $basedir3
mkdir Build
cd Build
cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
sudo make && sudo -E make install
cd ../..
# Cleanup
rm -rf $basedir
rm -rf $basedir2
rm -rf $basedir3
# ldconfig
sudo ldconfig
# Additional symlink/LD_LIBRARY_PATH stuff here if required.
# EOF
Key difference was using Clang for build. Take care not to use GCC which is system default compiler. I removed gobjc and g++ packages from system to avoid this issue.
export CC=clang
export CXX=clang++
svn co http://svn.gna.org/svn/gnustep/libs/libobjc2/1.6.1/
cd 1.6.1
make
This answer was hinted from this mailing list thread:
http://lists.gnu.org/archive/html/discuss-gnustep/2012-12/msg00036.html
Described script is posted here:
http://wiki.gnustep.org/index.php/GNUstep_under_Ubuntu_Linux
Now it compiles simple ARC code.
It seems GCC and Clang generated codes are incompatible.
Update
I wrote scripts to setup libobjc2 on FreeBSD and CentOS.
The scripts are mostly copied from a blog.

kdevelop4 and C++ requires cmake

I want to use kdevelop4 for c++ programming but when I try to run the application kdevelop4 wants cmake binary file! How can I solve this problem?
It sounds like you need to install CMake. If you are on a Linux system, aptitude install cmake (for Debian/ubuntu) or yum install cmake (for Red Hat/Fedora) should do the trick. On Windows, you may need to download and install CMake yourself.