26 Apr 2017

How do compile and run C program using Terminal?

Installing:

Install Xcode Command Line Tools
xcode-select --install

Click “Install” to download and install Xcode Command Line Tools.

Compiling:


Using Make utility:

Make is a utility tool that builds program from source code and libraries by reading a Makefile which is specification of a target. Makefile include information about how to compile and link a program.

To buld:
make HelloWorld

To run:
./HelloWorld

Using compiler:

clang HelloWorld.c or gcc HelloWorld.c for GCC compiler


CS50:


How do instal cs50.h library?

Test code:

#include <stdio.h>
#include <cs50.h>

int main() {
    printf("Your Name, please: \n");
    string s = GetString();
    printf("Your Name is: %s! \n", s);
    return 0;
}

Try to compile:

clang YourName.c -lcs50

-l means to link an objecting code into compiling phase. Hey, link the cs50 precompiled objecting code into target project. Put all -l parameters at the end of line.

This line will create an a.out file. Initially Clang compiler build the target program into a.out file. To rename that file into name of source file project we can use -o <fileName>

Compile one more time:

clang -o YourName YourName.c -lcs50 

for gcc compiler use:

gcc -o YourName YourName.c -lcs50

Run the program:

./YourName

Vita!


Miscellaneous:


Where are C include files stored on my Mac?
/usr/include
/usr/local/include
/usr/local/lib

Help me!
clang -help

Only run the preprocessor.
clang -E <fileName>

Only run preprocess and compilation steps. Compile to assembler.
clang -S <fileName>

Only run preprocess, compile, and assemble steps.
clang -c <fileName>

-lm link math
-Wall or -w enable all warnings
-Werror stop when get warning
-ggdb include debugger data

What is .a file?
.a files are static libraries


No comments:

Post a Comment

Popular Posts