Linux below C language multi-file compilation

Source: Internet
Author: User

Just started to learn Linux under the C language development, previously only in Windows to write a simple C language code, so the linux below the C language to write inadvertently understand, these days to see the relevant articles, finally have a smattering.

First Open the Life Terminal (ALT+CTRL+T), with vim to write three files main.c, STACK.C, stack.h

Main.c

#include <stdio.h>
#include "Stack.h"

void Main ()
{
int elem;
Stack stack;
Push (&stack, 1);
POPs (&stack, &elem);
}

Stack.h

typedef struct STACK {
int *base;
int top;
}stack;

extern void push (Stack *stack, int elem);
extern void POPs (Stack *stack, int *elem);

Stack.c

#include <stdio.h>
#include "Stack.h"
void push (Stack *stack, int elem)
{
printf ("This is Push function\n");
}

void POPs (Stack *stack, int *elem)
{
printf ("This is pop function\n");
}

All three files are under the same folder, and you can use the command: GCC main.c stack.c stack.h-o Mian compiles directly on the command line, but in order to learn to use makefile write a file for compiling

Makefile File:

MAIN:MAIN.O STACK.O
GCC MAIN.O stack.o-o Main

MAIN.O:MAIN.C stack.h
Gcc-c MAIN.C

STACK.O:STACK.C stack.h
Gcc-c STACK.C

This is done directly in the terminal to run make can be compiled ...
If the compiled file is not under the same folder, specify the path to the source file in the makefile file ...

Recommended Reading :

C + + implicit class type conversion implicit Class-type conversions http://www.linuxidc.com/Linux/2013-01/78071.htm

An analysis of C-language variable-length arrays http://www.linuxidc.com/Linux/2013-07/86997.htm

C language needs to pay attention to the problem http://www.linuxidc.com/Linux/2013-05/84301.htm

The use of C language bit domain and its attention point http://www.linuxidc.com/Linux/2013-07/87027.htm

Simple for loop and floating-point variable http://www.linuxidc.com/Linux/2013-08/88514.htm in C language

C language from getting started to mastering. (Wang Di, Han Xu). [PDF] + DVD video disc file  http://www.linuxidc.com/linux/2013-10/91775.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.