C_MAKEFILE

GCC

gcc hello.c -o hello

Course resources 12. Compilation and Makefiles

https://cse.msu.edu/~cse251/

code.c

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h> 
#include <math.h>

#define NumIter 5

int main() {
int i;
for (i=1; i<=NumIter; i++)
printf("PI^%d is %f\n", i,i);
return 0;
}

Preprocessing

  • Removes preprocessor directives (commands that start with #)

  • Producescode.i Don’tusedirectly

1
gcc -E  code.c >  code.i

output: code.i

Compiling

  • Convertssourcecodetomachinelanguagewithunresolveddirectives

  • Producesthecode.obinary

1
gcc -o code.o -c code.i

output: code.o

Linking

  • Createsmachinelanguageexectutable

  • Producesthecodebinarybylinkingwiththemathlibrary(‐lm)

1
gcc -lm -o code code.o

output: code

-c (Compile only)

i think it equals gcc -E and gcc - o

  • Purpose: Tells GCC to compile the source code into an object file (.o or .obj), but not to link it.

  • Usage: This is used when you want to compile source files without producing an executable, typically to be linked later in a separate step.

  • Example

    :

    1
    bash

    Copy
    gcc -c myfile.c

1
This will produce an object file 

myfile.o

1
without linking.

Makefile

https://www.gnu.org/software/make/manual/make.html

Make/make.md · 无限十三年/CPP - Gitee.com

https://www.bilibili.com/video/BV1Bv4y1J7QT

hello.cpp

1
2
3
4
5
6
7
#include <stdio.h>

int main() {
// Print "Hello, World!" to the console
printf("Hello, World!\n");
return 0;
}

MakeFile

1
2
hello: hello.cpp
g++ hello.cpp -o hello

make


C_MAKEFILE
https://noteforme.github.io/2025/02/04/C-MAKEFILE/
Author
Jon
Posted on
February 4, 2025
Licensed under