简体   繁体   English

编译一个C ++库mingw

[英]compiling a C++ library mingw

I was trying to follow along with the C++ headers tutorial here , and as the tutorial says I have the files main.cpp, add.cpp, and add.h. 我试图用C跟随沿++头教程在这里 ,而作为教程说我有文件main.cpp中,add.cpp和add.h. The only is that up until now I haven't been using an IDE and compiling straight from the command line. 唯一的是到目前为止我还没有使用IDE并直接从命令行编译。

But I can't seem to figure out how I would compile add.h and and add.cpp into a library. 但我似乎无法弄清楚如何将add.h和add.cpp编译到库中。

As of now, if give the command: g++ -o main main.cpp add.h add.cpp , it compiles just fine and gives me a main.exe . 截至目前,如果给出命令: g++ -o main main.cpp add.h add.cpp ,它编译得很好,并给我一个main.exe But how would I make it so the library (containing add.h and add.cpp) would be precompiled, and saved as a dll? 但是我怎么做它所以库(包含add.h和add.cpp)将被预编译,并保存为DLL? Is this something that's relatively straight forward with the command line? 这是命令行相对直接的东西吗?

Thanks for any help guys, cheers. 感谢任何帮助,欢呼。

There are two types of libraries: static and dynamic libraries. 有两种类型的库:静态库和动态库。 Static libraries are linked together with the resulting program, so each program that uses that library will get its own copy of the library code. 静态库与生成的程序链接在一起,因此使用该库的每个程序都将获得自己的库代码副本。

A more memory-efficient way is to use shared libraries (on windows called DLL), which are loaded on demand from a location that is specific for each platform, but the advantage is that only one instance of the library code needs to be loaded to memory when different programs that use the library are running simultaneously, and the resulting binary code of those programs do not contain the library code. 一种更节省内存的方法是使用共享库(在称为DLL的窗口上),这些库是从特定于每个平台的位置按需加载的,但优点是只需要加载库代码的一个实例当使用该库的不同程序同时运行时,这些程序的结果二进制代码不包含库代码。 it resides in a separate file that needs to be shipped together with the application and installed to a proper location. 它驻留在一个单独的文件中,需要与应用程序一起发送并安装到适当的位置。

If you use unix-like build tools (even on a windows system), this could be a typical sequence of commands you would use to produce a library that contains the code in your add.cpp file: 如果您使用类似unix的构建工具(甚至在Windows系统上),这可能是您用来生成包含add.cpp文件中的代码的库的典型命令序列:

for a static library: 对于静态库:

g++ -c add.cpp 
ar crf libadd.a add.o
g++ -o main main.cpp -L. -ladd

the first will compile the add.cpp into add.o , the second will create a static library libadd.a from add.o file. 第一个将add.cpp编译成add.o ,第二个将从add.o文件创建一个静态库libadd.a If you want to include more object files into your library, add them to the end of that command line. 如果要将更多目标文件包含到库中,请将它们添加到该命令行的末尾。 The last command compiles your main.cpp program while linking it with the static library file libadd.a . 最后一个命令编译main.cpp程序,同时将其与静态库文件libadd.a链接 The -L. -L。 option instructs the linker to search for the library file in the current directory. option指示链接器在当前目录中搜索库文件。 Alternately, you may want to put the library file in some other directory and use the -Lyour_directory option. 或者,您可能希望将库文件放在其他目录中并使用-Lyour_directory选项。

for a shared library (a dll): 对于共享库(一个DLL):

g++ -shared -o libadd.so add.cpp 
g++ -o main main.cpp -L. -ladd

but to run it, the system must be able to locate the shared library. 但要运行它,系统必须能够找到共享库。 You can help it by adding the directory where your library is located by adding it to the LD_LIBRARY_PATH environment variable, for instance: 您可以通过将库添加到LD_LIBRARY_PATH环境变量中来添加库所在的目录来帮助它,例如:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

for the Windows platform, you may need to use a few more qualifiers, which are nicely explained in the mingw tutorial: http://www.mingw.org/wiki/sampledll 对于Windows平台,您可能需要使用更多限定符,这些在mingw教程中有很好的解释: http//www.mingw.org/wiki/sampledll

g++ -c main.cpp
g++ -c add.cpp
g++ - o x.dll main.o add.o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM