Artículo Actualizado: 03/23/2020
Directorio artículo
- En primer lugar, compilar manualmente un enlace solo fichero fuente C
- En segundo lugar, el enlace compilar manualmente varios archivos de origen C
- 1, cree dos archivos de código fuente C
- 2, la compilación de dos archivos de origen
- 3, para generar un archivo ejecutable
- En tercer lugar, el uso Makefile para compilar automáticamente enlace
- 1, crear un archivo fuente de C
- 2, crear el Makefile
- 3, hacen un archivo ejecutable
- 4, añadir parámetros para limpiar archivos intermedios (opcional)
- 四, Enjoy!
En primer lugar, compilar manualmente un enlace solo fichero fuente C
1, crear un archivo fuente de C
Nota: Crear llamado aquí hello.c
el archivo de origen.
#include<stdio.h>
int main()
{
printf("hello,world!\n");
return 0;
}
2, compile el archivo fuente
gcc -c hello.c
3, para generar un archivo ejecutable
Nota: aquí result
desea emitir nombre del archivo ejecutable.
gcc -o result hello.o
En segundo lugar, el enlace compilar manualmente varios archivos de origen C
1, cree dos archivos de código fuente C
Nota: La creación llamado aquí hello.c
y hello2.c
los dos archivos de origen.
#include<stdio.h>
int main()
{
printf("hello,world!\n");
testfun(); //调用另一个源文件中的函数
return 0;
}
#include<stdio.h>
void testfun()
{
printf("\nThis is in hello2!\n");
}
2, la compilación de dos archivos de origen
Nota: Aquí también se puede utilizar gcc -c *.c
para compilar varios archivos de origen C.
gcc -c hello.c hello2.c
3, para generar un archivo ejecutable
gcc -o result hello.o hello2.o
En tercer lugar, el uso Makefile para compilar automáticamente enlace
1, crear un archivo fuente de C
Nota 1: El fichero fuente C, donde la continuación de dos del ejemplo anteriormente descrito.
Nota 2: Crear el nombre hello.c
y hello2.c
dos archivos de origen.
#include<stdio.h>
int main()
{
printf("hello,world!\n");
testfun(); //调用另一个源文件中的函数
return 0;
}
#include<stdio.h>
void testfun()
{
printf("\nThis is in hello2!\n");
}
2, crear el Makefile
Nota 1: A continuación hello.o
, hello2.o
los dos archivos objeto que se creará.
Nota 2: El result
nombre del archivo ejecutable que se va a emitir.
Nota 3: La gcc
parte frontal de líneas en blanco se Tabgenera claves.
main: hello.o hello2.o
gcc -o result hello.o hello2.o -lm
3, hacen un archivo ejecutable
4, añadir parámetros para limpiar archivos intermedios (opcional)
Nota 1: Si no desea producir el intermedio *.o
de archivos, se pueden añadir parámetros para limpiar en el Makefile.
Nota 2: La última línea también se podría escribir rm -f *.o
.
main: hello.o hello2.o
gcc -o result hello.o hello2.o -lm
clean:
rm -f hello.o hello2.o