C ++中的动态和静态库 [英] Dynamic and Static Libraries in C++

查看:129
本文介绍了C ++中的动态和静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我学习C ++的过程中,我遇到了动态和静态库。



我通常会得到它们的要点:编译代码包含在其他程序中。



但是,我想了解一些关于他们的信息:




  • 是否正常写C ++程序,减去 main()函数?

  • 编译的程序如何图书馆?它显然不是一个可执行文件,所以我怎么转,说'test.cpp'成'test.dll'

  • 一旦我得到它的格式,另一个程序?

  • 有标准的地方放置它们,以便无论编译器/链接器需要他们可以轻松找到它们吗?

  • 动态和静态库之间的差异(技术上和实际上)?

  • 如何在我的代码中使用第三方库(我盯着 .dylib .a MySql C ++连接器的文件)



我发现与图书馆有关的一切似乎都是针对那些已经知道如何使用它们的人。我,但是,不。



(我还要注意,我使用的是Mac操作系统X,虽然更喜欢保持IDE中性或面向命令行,我使用QtCreator / Netbeans)

解决方案


正在编写它们与正常C ++程序不同,减去main()函数?


p>


编译好的程序如何成为一个库?它显然不是一个可执行文件,所以我怎么转,说'test.cpp'成'test.dll'。


-dynamiclib 标志。 (结果的名称仍然默认为 a.out 。在Mac OS X上,应将动态库命名为 lib ***。dylib ,在Linux上, lib ***。so (共享对象))



< >

一旦我得到它的格式,我如何将它包括在另一个程序?


make一个头文件,以便其他程序可以 #include 知道你的dylib中可以使用什么函数。



其次,链接到你的dylib。如果你的dylib命名为 libblah.dylib ,你将 -lblah 标志传递给gcc。


有标准的地方放置它们,所以无论编译器/链接器需要它们都可以轻松找到它们?




/ usr / lib / usr / local / lib


动态和静态库之间有什么区别(技术上和实际上)?


基本上,对于静态库,整个库嵌入到链接的文件中。


如何在我的代码中使用第三方库(我在盯着MySql C ++连接器的.dylib和.a文件)


查看第三个答案。


In my quest to learn C++, I have come across dynamic and static libraries.

I generally get the gist of them: compiled code to include into other programs.

However, I would like to know a few things about them:

  • Is writing them any different than a normal C++ program, minus the main() function?
  • How does the compiled program get to be a library? It's obviously not an executable, so how do I turn, say 'test.cpp' into 'test.dll'?
  • Once I get it to its format, how do I include it in another program?
  • Is there a standard place to put them, so that whatever compilers/linkers need them can find them easily?
  • What is the difference (technically and practically) between a dynamic and static library?
  • How would I use third party libraries in my code (I'm staring at .dylib and .a files for the MySql C++ Connector)

Everything I have found relating to libraries seems to be targeting those who already know how to use them. I, however, don't. (But would like to!)

Thanks!

(I should also note I'm using Mac OS X, and although would prefer to remain IDE-neutral or command-line oriented, I use QtCreator/Netbeans)

解决方案

Is writing them any different than a normal C++ program, minus the main() function?

No.

How does the compiled program get to be a library? It's obviously not an executable, so how do I turn, say 'test.cpp' into 'test.dll'?

Pass the -dynamiclib flag when you're compiling. (The name of the result is still by default a.out. On Mac OS X you should name your dynamic libraries as lib***.dylib, and on Linux, lib***.so (shared objects))

Once I get it to its format, how do I include it in another program?

First, make a header file so the the other program can #include to know what functions can be used in your dylib.

Second, link to your dylib. If your dylib is named as libblah.dylib, you pass the -lblah flag to gcc.

Is there a standard place to put them, so that whatever compilers/linkers need them can find them easily?

/usr/lib or /usr/local/lib.

What is the difference (technically and practically) between a dynamic and static library?

Basically, for a static lib, the whole library is embedded into the file it "links" to.

How would I use third party libraries in my code (I'm staring at .dylib and .a files for the MySql C++ Connector)

See the 3rd answer.

这篇关于C ++中的动态和静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆