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