Friday, March 9, 2018

C language with Mac: Hello World with Mac's Terminal

This post shows how to execute a hello world program in C with the following steps:

1. In terminal, create a C file called hello.c:

nano hello.c

2. Edit the hello.c file as:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

3. In terminal, execute the file with:

cc hello.c
./a.out

Result:


References

How to run C program on Mac OS X using Terminal? (StackOverflow)
How to print Hello World in C on Mac (YouTube)
C++ with Mac: Hello World with Mac's Terminal
Matlab: Run Hello World files in C/C++ with MEX command

Thursday, March 1, 2018

C++ with Mac: Install Boost Libraries

To install the Boost C++ libraries on a Mac

1. Download the latest Boost package, e.g. the boost_1_66_0.tar.bz2 file.

2. In terminal, move the the directory with the downloaded file and type:

tar --bzip2 -xf boost_1_66_0.tar.bz2

3. There are three types of Boost libraries:

(1) Header-only libraries. Just include these files without compilation. Most files belong to this type.
(2) Libraries required to be built. e.g. Boost.System, Boost.Thread, and Boost.Timer.
(3) Optional separately-compiled binaries, e.g. Boost Math, and Boost Random.

4. Follow the instructions in section 5.1 "Easy Build and Install" of Boost's getting started guide for unix-variants.

5. Follow the instructions in section 6 "Link Your Program to a Boost Library" of Boost's getting started guide for unix-variants. The main way A to link libraries may be like this command:

c++ -I ~your_path/boost_1_66_0/ test.cpp -o test ~your_path/boost_1_66_0/stage/lib/libboost_regex.a

Follow instructions in section 6.2. The result should be like this:



References

Boost libraries (boost.org)
Install boost on Mac OSX
Getting Started on Unix VariantsGetting Started on Unix Variants

Friday, February 23, 2018

C++ with Mac: Hello World with Mac's Terminal

This post shows how to execute a C++ hello world program with the following steps:

1. In terminal, create a C++ file called hello.cpp:

nano hello.cpp

2. Edit the hello.cpp file as:

#include <iostream>

using namespace std;

int main() {
    cout << "Hello, World!\n";
    return 0;
}

3. In terminal, execute the file with:

g++ hello.cpp
./a.out

Result:


References

Compiling simple Hello World program on OS X via command line (StackOverflow)
C language with Mac: Hello World with Mac's Terminal
Matlab: Run Hello World files in C/C++ with MEX command

Saturday, February 10, 2018

Saturday, September 23, 2017

UIButton Selector Error with Swift 4: Argument of '#selector' refers to instance method that is not exposed to Objective-C

Swift 4 (Xcode 9.0) does not work with the below Swift 3 code while drawing a UIButton:


The error message says that:

Argument of '#selector' refers to instance method 'click()' that is not exposed to Objective-C

Add '@objc' to expose this instance method to Objective-C

By clicking the "Fix" button, Xcode automatically fix this error by placing '@objc' in front of func click() as below:



The complete code to draw a button that prints a message while touching it:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
        button.center = view.center
        button.setTitle("Press", for: .normal)
        button.setTitleColor(.blue, for: .normal)
        button.setTitleColor(.cyan, for: .highlighted)
        button.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside)
        view.addSubview(button)
    }
    @objc func click() {
        print("Pressed!")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}


Saturday, August 19, 2017

Mac Technique: Slide show for pdf file with Preview

To display the slide show for a pdf file in Preview, open the pdf file with Preview and press the hotkey:

[command ⌘] + [shift] + [F] 

or select:

View -> Slideshow





Saturday, August 5, 2017

Run an Xcode project on an iOS device without US$99 Apple Developer account (Configuration for iPhone)

Since iOS 9, it is possible to install the app of an Xcode project to an iOS device without paying for a US$99 Apple Developer account. The steps to download the app to an iPhone/iPad are demonstrated with an iOS 10 device as below:

When press the build and run button of Xcode:

You may see a Could not launch "deviceName" dialog like this:


So follow the instruction on the Xcode dialog to select General -> Profiles & Device Management on the iOS device (iPhone/iPad).


Select the device.
Select "Trust (Apple ID)".
 Select "Trust".

Then the app should run on the iPhone / iPad.

For more information about Xcode configuration, see this: