Create and call a Linux C library.
Create and call a library
If you have a huge project and put hundreds of megabytes or even several GB of code in an application, future modifications will make you very complicated, every time you use this function, if you copy this code again, it will greatly waste the storage disk of the Code. If you place different function codes in several linked libraries to share these functions, you can update the application without re-generating or installing the entire program, which greatly facilitates programming.
For example, the library directory contains three directories: file1, file2, file3, and LIB:
Library -- file1 -- file1.c hello. h hello. c
-- File2 -- file2.c hi. c hi. h
-- File3 file3.h file3.cpp
-- LIB
Generate a library call in the current directory
[Root @ centos-64-min file1] # cat file1.h
Void play1 ();
[Root @ centos-64-min file1] # cat file1.c
# Include <stdio. h>
# Include "file1.h"
Int main (void)
{
Play1 ();
Return 0;
}
[Root @ centos-64-min file1] # cat play1.c
# Include <stdio. h>
Void play1 (void)
{
Printf ("hello file1 \ n ");
}
Question 1:
[Root @ centos-64-min file1] # gcc -- shared play1.c-o libplay1.so
/Usr/bin/ld:/tmp/ccX7Elnx. o: relocation R_X86_64_32 against '. rodata' can not be used when making a shared object; recompile with-fPIC
/Tmp/ccX7Elnx. o: cocould not read symbols: Bad value
Collect2: ld returned 1 exit status
Solution:
[Root @ centos-64-min file1] # gcc-fPIC -- shared play1.c-o libplay1.so
Question 2:
[Root @ centos-64-min file1] # gcc-o file1 file1.c libplay1.so
[Root @ centos-64-min file1] #./file1
./File1: error while loading shared libraries: libplay1.so: cannot open shared object file: No such file or directory
This is mainly because the play1 library cannot be found in the execution file, resulting in no link. You can specify the location of the library during compilation.
Solution:
[Root @ centos-64-min file1] # gcc-o file1 file1.c./libplay1.so
[Root @ centos-64-min file1] #./file1
Hello file1
[Root @ centos-64-min file2] # cat file2.c
# Include <stdio. h>
# Include "hi. h"
Int main (void)
{
Hi ();
Return 0;
}
[Root @ centos-64-min file2] # cat hi. h
# Include "hi. c"
Void hi ();
[Root @ centos-64-min file2] # cat hi. c
# Include <stdio. h>
Void hi (void)
{
Printf ("hi file2 \ n ");
}
Running result:
[Root @ centos-64-min file2] # gcc-o file2 file2.c
[Root @ centos-64-min file2] #./file2
Hi file2
Put Inventory generated by file1 and file2 in LIB
[Root @ centos-64-min file2] # cp libfile2.so ../LIB/
[Root @ centos-64-min file1] # cp libplay1.so ../LIB/
[Root @ centos-64-min LIB] # ls
Libfile2.so libplay1.so
Compile the c ++ file in file3 and call the library in LIB.
[Root @ centos-64-min file3] # cat file3.cpp
# Include <iostream. h>
Int main (void)
{
Play1 ();
Hi ();
Return 0;
}
[Root @ centos-64-min file3] # cat file3.h
# Ifdef _ cplusplus
Extern "C"
{
# Endif
Void hi ();
Void play1 ();
# Ifdef _ cplusplus
}
# Endif
[Root @ centos-64-min file3] # g ++-o file3 file3.h file3.cpp ../LIB/libfile2.so ../LIB/libplay1.so
[Root @ centos-64-min file3] #./file3
Hello file1
Hi file2
The library contains the library generated by C compilation and the library generated by C ++ compilation. The following describes how C calls the C ++ library and how C ++ calls the C library.