Make a static link library yourself and use

Source: Internet
Author: User

Objective to hide the source code provided only to. A and. h files

/*
Aston.c
*/
#include <stdio.h>
void func1 (void)
{
printf ("Func1 in aston.c\n");
}
int func2 (int a,int b)
{
printf ("Func2 in aston.c\n");
return a+b;
}
Aston.h
void func1 (void);

int func2 (int a,int b);

Makefile
All
GCC Aston.c-o aston.o-c
AR-RC Libaston.a ASTON.O

Text.c

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

int main (void)
{
Func1 ();
int a = FUNC2 (4,5);
printf ("A =%d.\n", a);
}
One
[email protected]: ~/ccc/hello/4.6.11.archiveuse$ Make
GCC Aston.c-o aston.o-c
AR-RC Libaston.a ASTON.O
Two
[email protected]: ~/ccc/hello/4.6.11.archiveuse$ gcc Test.c-o test
/tmp/ccsez3m1.o:in function ' main ':
TEST.C: (. text+0xa): Undefined reference to ' func1 '
TEST.C: (. text+0x1e): Undefined reference to ' Func2 '
Collect2:error:ld returned 1 exit status
Three
[email protected]: ~/ccc/hello/4.6.11.archiveuse$ gcc Test.c-o Test-laston
/usr/bin/ld:cannot Find-laston
Collect2:error:ld returned 1 exit status
Four
[email protected]: ~/ccc/hello/4.6.11.archiveuse$ gcc Test.c-o Test-laston-L._l. To link the current directory
[email protected]: ~/ccc/hello/4.6.11.archiveuse$ ls
ASTON.C aston.h aston.o libaston.a Makefile test test.c
Five
[email protected]: ~/ccc/hello/4.6.11.archiveuse$./test
Func1 in ASTON.C
Func2 in ASTON.C
A = 9.

Make a static link library yourself and use

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.