简体   繁体   English

如何使用“静态链接”和“动态链接”与gcc和Visual Studio构建C / C ++程序?

[英]How can I build a C/C++ program using `static linking` & `Dynamic linking` with gcc & Visual studio?

A library can be used in an application in two ways: 可以通过两种方式在应用程序中使用库:

  1. Statically-linked 静态链接
  2. Dynamically-linked 动态链接

But how to do that using both Visual Studio (windows) & GCC? 但是如何使用Visual Studio(windows)和GCC?

I know libraries are distributed only in these 4 ways: 我知道库仅以这四种方式分发:

  1. Source 资源
  2. header-only libraries 仅限标头的库
  3. *.lib files for windows. * .lib文件的Windows。 *.a for linux * .a for linux
  4. *.dll (windows) & *.so (linux). * .dll(windows)&* .so(linux)。

Source distribution is just compiled."header-only libraries" are nothing but a source distribution. 源代码分发只是编译。“仅头文件库”只是源代码分发。

Now if the desired library is distributed in *.lib form. 现在,如果所需的库以* .lib形式分发。 Inorder to use it. 为了使用它。

On Visual Stuido : Visual Stuido上

  1. We add directory path containing headers(*.h) to Configuration Properties > General > Additional Include Directories 我们将包含标题(* .h)的目录路径添加到“ 配置属性”>“常规”>“其他包含目录”
  2. we add each *.lib file to Configuration Properties > Linker > Input > Additional Dependencies 我们将每个* .lib文件添加到配置属性>链接器>输入>附加依赖项
  3. we add directory path of *.lib files to: Configuration Properties > Linker > Additional Library Directories 我们将* .lib文件的目录路径添加到: 配置属性>链接器>其他库目录

How to do the same thing for GCC/MingW? 如何为GCC / MingW做同样的事情? I don't know how to build my application when the library is distributed as *.dll or *.so too. 当库分发为* .dll或* .so时,我不知道如何构建我的应用程序。 Can someone tell me what do I need to do in these situations for both Visual studio (windows) and GCC(linux)/mingw(windows) 有人可以告诉我在Visual Studio(windows)和GCC(linux)/ mingw(windows)这些情况下我需要做什么

On GCC, for static linking, you'll include the library in the command line. 在GCC上,对于静态链接,您将在命令行中包含库。 Lets say you've glib-2.0.lib and your program that uses GLib library is my_prog.c, then you invoke GCC as gcc my_prog.c -L<library_dir_here> -lglib-2.0 . 让我们说你有glib-2.0.lib和你使用GLib库的程序是my_prog.c,然后你调用GCC作为gcc my_prog.c -L<library_dir_here> -lglib-2.0

As for the dll and so, dynamic libraries are something you don't link to your programs by passing them to your linker. 至于dll等,动态库是指通过将它们传递给链接器而无法链接到程序的东西。 Instead the operating system gives you a function to load them when it's required, at run time. 相反,操作系统为您提供了在运行时根据需要加载它们的功能。 Thats the reason it's called dynamic. 这就是它被称为动态的原因。 In Windows you've LoadLibrary and on Linux you've dlopen . 在Windows中你有LoadLibrary ,在Linux上你已经dlopen Both these functions get a string (which is the dll or so's name) and load it if it's avaiable on the machine. 这两个函数都得到一个字符串(这是dll左右的名字)并加载它,如果它在机器上可用。 Once it's loaded, the function you require from the library is looked-up by passing its name to GetProcAddress on Windows and dlsym on Linux; 加载后,通过将其名称传递给Windows上的GetProcAddress和Linux上的dlsym来查找库中所需的函数。 both returns a function pointer, with which you can call that function. 两者都返回一个函数指针,您可以使用它来调用该函数。 Since you're not directly calling the functions provided by the libraries directly, but thru' function pointers, there'll be no need for you to link them statically (ie pass them to the linker) when you build your app. 既然你没有直接调用库提供的函数,而是直接调用函数指针,那么在构建应用程序时,你不需要静态地链接它们(即将它们传递给链接器)。

For DLL distributions the scenario is similar to that of .lib files. 对于DLL发行版,该场景类似于.lib文件。 (your #3) (你的#3)

You will have to configure your project to build a DLL. 您必须配置项目以构建DLL。 The project will build LIB and DLL files. 该项目将构建LIB和DLL文件。

Depending on your needs/architecture/design you can either 根据您的需求/架构/设计,您也可以

  • Link against the LIB file just as you do in your #3 above. 链接LIB文件就像在上面的#3中一样。 Note that the DLL file will have to exist on the target machine at run-time otherwise the application will not load. 请注意,DLL文件必须在运行时存在于目标计算机上,否则将无法加载应用程序。

  • Call "LoadLibrary()" from the client app and skip the linking part/no need to have the LIB used in the client application. 从客户端应用程序调用“LoadLibrary()”并跳过链接部分/无需在客户端应用程序中使用LIB。

I can't help you with the gcc specific questions. 我无法帮助你解决gcc的具体问题。

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

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