[Ubuntu] Use g++ to generate dynamic libraries

This post describes the process of using g++ to generate a dynamic library. In the end, the functions written by yourself can be called by linking the dynamic library just like the C++ library functions. It is no longer necessary to put the function.csum function.hfile in the project directory.

1. Generate the dynamic library file libxxx.so

1. The advantages of dynamic libraries

Suppose I have two files: function.hand function.c, which are the declaration and definition of a function funtion .

If the dynamic library is not generated, then when I main.cwant to call a function in another file, I function()must copy the function.hsum function.cto the main.csame path. If I use functions in 10 projects, I have function()to copy function.hand function.ccopy 10 times, which is not only troublesome, but also a waste of memory.

If you have a dynamic library, then every time you want to call a function function(), you only need to add an option when compiling with g++ , which means linking to the function library. In this way, no matter how many projects there are, we only need one dynamic library.main.c-lfunction

2. Generate the dynamic library file libxxx.so

In the directory containing function.hand function.c, open the Linux terminal and execute the following command:

g++ function.c -I ./ -fPIC -shared -o libfunction.so

Here is an explanation of this command:

  • function.c is the target source file we want to operate
  • Because function.cof it #include "function.h", we have to give the location of the file when compiling with g++function.h . In the g ++ command, with the -Ioption to identify .h文件our position, here function.hand function.cin the same path, with the -I command ./ ** , which ./** represents the current location path.
  • -fPIC means that position-independent code is generated (involving theory, please Baidu by yourself)
  • -shared indicates that the goal of this command is to generate a dynamic library
  • -o is used to specify the name of the output file, -o libfunction.so means the file name is generatedlibfunction.so

Why is the generated dynamic link library named libfunction.so? Is it a fixed format?

It is indeed a fixed format.
The dynamic link library in the linux system must be named libxxx.so, where xxx is defined by humans and is generally the library name.
In this way, when we link with g ++ library, it can be used g++ main.c -lxxxto link. Among them, main.c is the source file to call the function library xxx, and -lxxxmeans to link to the xxx library.

2. Move the dynamic library file libxxx.so to the system directory

In order for g++ to find libxxx.so , we must move libxxx.so to the specified system directory. **g++** automatically goes to this directory to find library files at runtime by default.

Execute the following commands:

sudo cp libxxx.so /lib/x86_64-linux-gnu/

In this way, libxxx.somoved to the directory /lib/x86_64-linux-gnu/, g++ will /lib/x86_64-linux-gnu/automatically find library files in the directory by default .

In addition, it needs to be function.hplaced in a designated location.

Execute the following commands:

sudo mkdir /usr/include/function
sudo cp function.h /usr/include/function 

In this way, put it function.hin /usr/include/function, this directory is also the default directory of g++, which will automatically find the header files. At the same time, most of the header files of the library are stored in /usr/include/this directory.


At this point, our work is over. If used after the function function in a program main.c, just when compiling main.c, execute g++ main.c -lfunctionthe command can be, without then function.hand function.cmove around.




Today, while visiting the Ubuntu forum, I saw the wisdom of answering a post from 2006, which talked about the norms when answering other people’s questions. In contrast to current posts or blogs, few people can write with such norms and mentality. I have to sigh for the talent, wisdom and humble attitude of the predecessors, and I will use this standard to standardize my writing and learning in the future.

1. Don't answer questions to which you don't know the answer(不回答自己不知道答案的问题)

2. Explain yourself (解释给自己)
如果自己是提问者,你的回答是否能让自己明白?


3. Give as little assistance as necessary (尽可能的给最少的帮助)
有的时候启发性的回答更为有效.

4. Show your workings (展示你的做法)

5. Use humour judiciously (明智地使用幽默)
有的时候因为不同语境/语言的问题,你的俏皮话可能会让提问者更加困惑。

6. If you can't say something nice don't say anything atall(如果你不能说出有用的内容,就别说)

7. Avoid jargon, baffling acronyms and idiolects(避免行话、令人困惑的缩写词、习惯用语)

8. Never never never just respond with RTFM. Not ever.(永远永远永远不要回复RTFM)
这里的 RTFM 代表"Read The Fucking Manual", "去读该死的手册".
另外一个常见的是: STFW --Search The Fucking Web,
"搜索该死的网络",或者友好一点的 "Google 一下".
对于中文论坛上,我觉得还有一个尽量不要说 "RPWT" --人品问题

9. Meditate on eternity (永远的深思熟虑)
回答的问题,可能在不久以后会被别人搜索到,看到,甚至是被你将来的老板看到。一
个欠缺思索的回答无疑会降低你在其他技术人员心目中的形象。

10. Keep your newbie mind (保持自己的"新手"思维)
学无止境

保持谦卑。回答并不意味着你是"给予", 可能你也在学习.
不要认为回答了一些问题自己就成了 Guru 了.

---
把问题先交给google,然后再交给你的朋友。。。

Guess you like

Origin blog.csdn.net/qq_39642978/article/details/114138342