Linux C programming often needs to link other functions, and other functions are generally placed in another. c file, or packaged in a library file, I need to call these functions in the main function, there are several methods:
1. When you need to call the number of functions less, you can directly include the file in the main function, such as a folder containing ADD.C and main.c files:
Method One:
File ADD.C defines a function that adds two integers, code as follows:
#include <stdio.h><math.h>intAdd (int A,int b) { int z; Z=a+b; return Z;}
The code for the main function main.c is as follows:
#include <stdio.h><math.h>#include"add.c"int Main () { int I, J, K; I=1; J=2; K=Add (i,j); printf ("I add j=%d\n", k);}
Compile build executable: Gcc-o main MAIN.C
Execution:./main
Method Two:
You can write a header file to declare the next function add, and then include the file in the main function, and then write a makefile file, link it well, to achieve the following:
New Fun.h, code as follows:
#ifndef _fun_h_ #define _fun_h_#endif#include<stdio.h>#include<math.h>int Add (int A,int b);
At this point the main function should contain the header file, and the main function should read as follows:
#include <stdio.h><math.h>#include"fun.h"int Main () { int I, J, K; I=1; J=2; K=Add (i,j); printf ("I add j=%d\n", k);}
Write a simple makefile, compile
main:main.o ADD.O -o mainmain.o:main.c fun.h add.c -C main.c-o main.oadd.o:add.c fun.h-C add.c-o ADD.O
Make to get the executable file main
./main, you get the results.
2. when the defined function is relatively long, it can be packaged in a library file, and then when compiling the main function to link the library file, the library file is divided into dynamic libraries and static libraries, the link method is as follows:
Like what:
Directory 1:/home/document/libraries_test/head//ADD.C MULTI.C (multi.c defined as two integers, not listed here)
Directory 2:/home/Document/libraries_test/test_head//There is a function inside, you need to include the header file in the directory MAIN.C
Static Library Links:
CD Catalog 1
Write a makefile file to generate a static library. Makefile content is as follows:
ADD.O:ADD.C -C ADD.C-o add.o multi.o:multi.c-C multi.c-o multi.o libhead.a:add.o multi.o ar crv LIBHEAD.A ADD.O MULTI.O
Then: Make
Generated the libhead.a file
CD Catalog 2
Write a makefile file to generate the executable file
MAIN:MAIN.C gcc main.c-i${catalog 1}$-o main-l${directory 1}$-lhead
Note:-I.. This path refers to the path of the header file (because there is no header file, you can remove the-i{directory 1})
-L ... This refers to the path of the library,-L. This refers to the library name if you need to reference-L. Two libraries below (e.g. LIBBLAS.A LIBCBLAS.A)
This time to write:-L ... -lcblas-lblas (Note: Although LIBCBLAS.A and LIBBLAS.A are used, when writing the path, simply write Cblas and Blas, i.e. omit lib. a)
Note!!! If you put the. A file under/usr/lib or/usr/local/lib (gcc default Library search path, because the/usr/lib library file is more, I generally put the common library under/usr/local/lib), at this time do not need to specify the path, Compile this time:
GCC Test1.c-o Test-lcblas-lblas (note that when compiling, be sure to use-L.) Specify which library to be)
Dynamic Library Links:
CD Catalog 1
Write a makefile file to generate the dynamic library. Makefile content is as follows:
ADD.O:ADD.C head.h
-C ADD.C-o ADD.O
MULTI.O:MULTI.C head.c -C multi.c-o MULTI.O
LIBHEAD.SO:ADD.O multi.o -o libhead.so-fpic add.o multi.o
Then: Make
Generated the libhead.so file
CD Catalog 2
Write a makefile file to generate the executable file
The makefile file is generated as follows:
main:main.c -i${directory 1}$-o main-wl,-rpath,${directory 1}$-lhead
Make to generate the executable file, main, and then execute it.
Linux C programming calls other functions, link header files, and library files