简体   繁体   English

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

[英]How do I build and link external cross platform libraries in C?

I am relatively new to C and I'm making a project that I wish to be cross platform. 我是C语言的新手,我正在做一个跨平台的项目。 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? 我想知道我应该如何将其包含在我的项目中?

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! 谢谢!

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依赖项即可。

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

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