Build the. A file (Linux static Library) with makefile and compile into the executable file __linux

Source: Internet
Author: User

File list:
Code Files

/* The following code is saved to Plus.cpp *

/int my_plus (int x,int y)  
{return  
    x + y;  
}  
/* The following code is saved to minus.cpp
/int my_minus (int x,int y)  
{return  
    xy;  
}  
/* The following code is saved to MY_MATH.H *
/int my_plus (int x,int y); 
int my_minus (int x,int y);
/* The following code is saved to Lib_test.cpp * * *
#include "my_math.h"  
#include <stdio.h>  
int main ()  
{  

    printf ("[My_plus] =%d\n", My_plus (3,5));
    printf ("[My_minus] =%d\n", My_minus (3,5));
    return 0;
} 
Makefile File
/* The following content is saved to Lib.mak * *
SHELL =/bin/sh  
CC = g++
ar = ar  

cflag =-o2-w-wall  

= i.  
Lib_target =./libmath.a  
plus_obj = plus.o plus_src =./plus.cpp min_obj  

= minus.o 
min_src =./minus.cpp All

: $ (lib_target)  
$ (plus_obj): $ (PLUS_SRC) $ (  
    CC) $ (cflag) $ (include_path)-C $ (PLUS_SRC)  

$ (MIN _obj): $ (MIN_SRC) $ (  
    CC) $ (cflag) $ (include_path)-C $ (MIN_SRC)  

$ (lib_target): $ (plus_obj)  $ (min_obj) c17/>$ (AR) CQ $@ $^  

    rm-f $ (plus_obj)
    rm-f $ (min_obj) Clean

:  
    rm-f $ (plus_obj)
    rm-f $ (lib_targ BTN
/* The following content is saved to App.mak/
SHELL =/bin/sh  
CC = g++  

include_path =-I.  
Test_target =./test  
test_src =./lib_test.cpp  
ldflags: =./libmath.a all  
:
    $ (CC)-G $ (TEST_SRC) $ ( Ldflags)-O $ (test_target) $ (include_path) Clean  

:
    rm-f $ (test_target)
Compiling Connections

Compile build Static library :
Use commands on Linux terminals:

Make-flib.mak

The effect of the following figure

compiling the build executable file
To use the command:

Make-fapp.mak

The effect of the following figure

Run:

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.