How to Link Agains Link Against Ws232lib Makefile

How to Link Static Library in C/C++ using GCC compiler?

Unlike Dynamic Link Library (DLL), the static library are pre-compiled and linked to the binary executables. In one case built into the final executables, the static library cannot be shared amongst the others. The static library is part of the binary code and that means information technology is loaded as the program starts i.eastward. DLL files can be loaded at runtime whenever needed.

static-library-linked-into-executable How to Link Static Library in C/C++ using GCC compiler? c / c++ gcc compiler programming languages tutorial

static-library-linked-into-executable

How to Create Static Library in C/C++?

It turns out that it is very uncomplicated to create static library in C/C++. A static library is basically an archive (like a cipher file) of object files, which are compiled from the *.c/*.cpp source code. Each source code contains the exported functions. For example, permit's create ii source files: test1.c and test2.c that contains two functions respectively.

1 2 3 4                              
                                // test1.c                                int                                test1(                                int                                a)                                {                                render                                a                                +                                1                                ;                                }                              
// test1.c int test1(int a) {         return a + ane; }

There is no need to define entry master as these are not programs but function libraries.

1 two 3 4                              
                                // test2.c                                void                                test2(                                int                                *c)                                {                                *c                                =                                *c                                +                                i                                ;                                }                              
// test2.c void test2(int *c) {         *c = *c + ane; }

At present, we tin compile these into *.o object files.

The -c switch ways: Compile and gather, but exercise not link. Nosotros will accept 2 files: test1.o and test2.o and they are object files:

1 ii 3                              
$                                file                                *.o test1.o: ELF                                64-bit LSB relocatable, x86-64, version                                1                                (SYSV), non stripped test2.o: ELF                                64-bit LSB relocatable, x86-64, version                                1                                (SYSV), not stripped
$ file *.o test1.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped test2.o: ELF 64-fleck LSB relocatable, x86-64, version i (SYSV), not stripped

Now, every bit mentioned to a higher place, we can apply ar to put these object files together into a single static library.

1 2 3 4                              
$                                ar                                rsv testlib.a test1.o test2.o ar: creating testlib.a a - test1.o a - test2.o
$ ar rsv testlib.a test1.o test2.o ar: creating testlib.a a - test1.o a - test2.o

The switch for ar command: r – supervene upon, s – create an archive, v – verbose. Now the testlib.a contains test1.o and test2.o which we can verify by:

1 ii three 4 five                              
$                                file                                testlib.a testlib.a: current                                ar                                archive $                                ar                                -t                                testlib.a test1.o test2.o
$ file testlib.a testlib.a: electric current ar archive $ ar -t testlib.a test1.o test2.o

How to Use Static Library in C/C++?

Let'south create a test file and invoke these two methods from the static library.

one 2 iii 4 5 vi 7 eight 9                              
                                #include <stdio.h>                                int                                main(                                )                                {                                int                                c                                =                                three                                ;                                test2(                                &c)                                ;                                printf                                (                                "%d\due north"                                ,                                c)                                ;                                printf                                (                                "%d\n"                                ,                                test1(c)                                )                                ;                                render                                0                                ;                                }                              
#include <stdio.h>  int chief() {         int c = 3;         test2(&c);         printf("%d\n", c);         printf("%d\n", test1(c));         render 0; }

To compile it, use the following command (pass the static library testlib.a every bit the source file, so gcc compiler knows to link it afterwards.

1                              
                                gcc                                -o                                test.out test.c testlib.a
gcc -o test.out test.c testlib.a

Alternatively, you could use the explicity linking options to link the static library (-L switch specifies the static library path and -l followed by the name of the static library):

1                              
                                gcc                                -o                                test.out test.c -Fifty.                                -ltestlib                              
gcc -o test.out examination.c -L. -ltestlib

However, information technology will output warnings:

1 2 3 4 five 6                              
test.c: In                                function                                'master': warning: implicit declaration of                                function                                'test2'                                [-Wimplicit-function-declaration]                                test2(                                &c);   ^ alarm: implicit proclamation of                                office                                'test1'                                [-Wimplicit-function-announcement]                                printf                                (                                "%d\due north", test1(c)                                );
examination.c: In office 'main': warning: implicit declaration of function 'test2' [-Wimplicit-function-declaration]   test2(&c);   ^ alarm: implicit declaration of function 'test1' [-Wimplicit-role-declaration]   printf("%d\north", test1(c));

However, the compiler still generates the binary code and y'all can run it:

The warnings are to warn you that the compiler is not sure the office declarations of test1 and test2. To make the gcc compiler happy, you lot need to declare the two functions before usage:

1 2                              
                                int                                test1(                                int                                c)                                ;                                void                                test2(                                int                                *c)                                ;                              
int test1(int c); void test2(int *c);

That is why the static library is distributed with a function declaration header files *.h, so that you know how to invoke them and the compiler takes intendance of them eastward.chiliad. linking *.a static libraries into your executables.

–EOF (The Ultimate Calculating & Technology Weblog) —

GD Star Rating
loading...

791 words
Last Mail service: How to Get Minimum Moves to Equal Array Elements?
Next Post: How to Download Tumblr Videos?

The Permanent URL is: How to Link Static Library in C/C++ using GCC compiler? (AMP Version)

pringleonfiess.blogspot.com

Source: https://helloacm.com/how-to-link-static-library-in-cc-using-gcc-compiler/

0 Response to "How to Link Agains Link Against Ws232lib Makefile"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel