如何在C中构建和链接外部跨平台库?

时间:2022-10-17 12:09:14

I am relatively new to C and I'm making a project that I wish to be cross platform. I want to use an external library that has sworn to be cross platform as well. I am wondering how I should include this within my project?

我是C的新手,我正在制作一个我希望跨平台的项目。我想使用一个已经宣誓成为跨平台的外部库。我想知道如何在我的项目中包含这个内容?

From what I can understand, I have two options. Build the libraries before hand on every platform I wish to use, then handle which library is linked within my build process (not very platform independent). Or I can include the entire source of the library and build it along with my project.

根据我的理解,我有两种选择。在我希望使用的每个平台上预先构建库,然后处理在我的构建过程中链接的库(不是非常独立于平台)。或者我可以包含库的整个源代码并将其与我的项目一起构建。

Are these my only options for handling cross platform libraries? Maybe I am not understanding how the process completely works yet, so any help would be awesome!

这些是我处理跨平台库的唯一选择吗?也许我不明白这个过程是如何完成的,所以任何帮助都会很棒!

(PS I'm using CMake to build my project.)

(PS我正在使用CMake来构建我的项目。)

Thanks!

1 个解决方案

#1


There are several factors and their combinations that may affect the answer.

有几个因素及其组合可能会影响答案。

  • The external library can be either static or shared.
  • 外部库可以是静态的,也可以是共享的。

  • The build is intended for internal testing or for release.
  • 该构建旨在用于内部测试或发布。

  • The application source code with autoconf stuff is distributed or binary application is distributed.
  • 具有autoconf内容的应用程序源代码是分布式的或二进制应用程序是分布式的。

  • The external library source code with own autoconf stuff is distributed or binary shared library is distributed.
  • 具有自己的autoconf内容的外部库源代码是分布式的或分布式二进制共享库。

When you want to distribute binary application then it is better to use the static external library. In this case you only have the options written above. You can either build the library before hand for each platform or incorporate the library build to your building process.

当您想要分发二进制应用程序时,最好使用静态外部库。在这种情况下,您只有上面写的选项。您可以为每个平台预先构建库,也可以将库构建合并到构建过程中。

If the external library and the application are open source and they are distributed with own autoconf stuff then you do not need to distribute the external library and you can just add an autoconf dependency.

如果外部库和应用程序是开源的,并且它们使用自己的autoconf内容进行分发,那么您不需要分发外部库,只需添加autoconf依赖项即可。

#1


There are several factors and their combinations that may affect the answer.

有几个因素及其组合可能会影响答案。

  • The external library can be either static or shared.
  • 外部库可以是静态的,也可以是共享的。

  • The build is intended for internal testing or for release.
  • 该构建旨在用于内部测试或发布。

  • The application source code with autoconf stuff is distributed or binary application is distributed.
  • 具有autoconf内容的应用程序源代码是分布式的或二进制应用程序是分布式的。

  • The external library source code with own autoconf stuff is distributed or binary shared library is distributed.
  • 具有自己的autoconf内容的外部库源代码是分布式的或分布式二进制共享库。

When you want to distribute binary application then it is better to use the static external library. In this case you only have the options written above. You can either build the library before hand for each platform or incorporate the library build to your building process.

当您想要分发二进制应用程序时,最好使用静态外部库。在这种情况下,您只有上面写的选项。您可以为每个平台预先构建库,也可以将库构建合并到构建过程中。

If the external library and the application are open source and they are distributed with own autoconf stuff then you do not need to distribute the external library and you can just add an autoconf dependency.

如果外部库和应用程序是开源的,并且它们使用自己的autoconf内容进行分发,那么您不需要分发外部库,只需添加autoconf依赖项即可。