如何在我的gcc编译器中链接第三方库bzip2? [英] How can I link the 3rd party library bzip2 in my gcc compiler?
问题描述
我是C语言的新Python开发人员,需要在Windows和Linux上运行Windows上的C代码。
I'm a python developer new to C and developing C code on Windows that needs to work on Windows and Linux.
为此,我下载了 MSYS2 ,并使用pacman安装gcc和 bz2 。
To that end, I downloaded MSYS2 and used pacman to install gcc and bz2.
我的问题是:如何在我的C代码中使用bzip2。
My question is: How do I use bzip2 in my C code.
当我尝试编译此C代码时:
When I try to compile this C code:
#include <bzlib.h>
使用命令 gcc test.c -lbzip2 -o test.out
我收到以下错误:
using the command gcc test.c -lbzip2 -o test.out
I get the following error:
test.c:1:10:致命错误:bzlib.h:没有这样的错误文件或目录
我包括正确的头文件吗?我可以正确链接吗?
Am I including the correct header file? Am I linking it correctly?
不使用第三方库时,一个简单的 hello world程序可以编译并很好地执行。
When not using 3rd party libraries a simple "hello world" program compiles and executes fine.
推荐答案
简短版本:假设您正在使用MSYS目标 pacman -S libbz2-devel
。
Short version: assuming you are using the MSYS target, pacman -S libbz2-devel
.
长版本:在MSYS2中,您可以使用以下命令找到包含文件的软件包:
Long version: In MSYS2 you can find which package contains a file using:
pacman -F bzlib.h
$ b答案是:
$ b
to which the answer is:
mingw32/mingw-w64-i686-bzip2 1.0.8-1 [installed]
mingw32/include/bzlib.h
mingw64/mingw-w64-x86_64-bzip2 1.0.8-1 [installed]
mingw64/include/bzlib.h
msys/libbz2-devel 1.0.8-1 (development)
usr/include/bzlib.h
要解释此输出,首先要了解MSYS2安装支持三种不同的开发目标:
To interpret this output, first understand that an MSYS2 installation supports three different development targets:
-
mingw32
(构建本机Win32 ap -
mingw64
(使用mingw-w64构建本机Win64应用程序) -
msys
(使用自定义的GCC端口和运行时库构建依赖于MSYS DLL和运行时环境的Win32或Win64应用程序,并支持许多POSIX功能)。 / li>
mingw32
(builds native Win32 applications using mingw-w64)mingw64
(builds native Win64 applications using mingw-w64)msys
(builds Win32 or Win64 applications that depend on MSYS DLLs and runtime environment, using a custom GCC port and runtime library, and supports a lot of POSIX functionality).
在安装MSYS2时,您将在开始菜单中获得三个启动脚本,每个环境对应一个。
When you install MSYS2 you will get three startup scripts in the Start Menu , one for each of those environments.
上面的 pacman -F
的输出告诉我们,对于目标 mingw32
和 mingw64
,软件包 bzip2
包含使用bzip进行开发所需的文件。但是,在 msys
目标上,需要软件包 libbz2-devel
。
The output of pacman -F
above told us that for targets mingw32
and mingw64
, the package bzip2
contains the files required to do development with bzip. However, on the msys
target, the package libbz2-devel
is required.
这是 msys
和各种* nix软件包管理器(MSYS2 pacman是ArchLinux pacman的端口)中常见的软件包布局:
This is a common package layout in msys
and in the various *nix package managers (MSYS2 pacman is a port of ArchLinux pacman):
-
bzip2
是在外壳程序中使用bzip2的二进制文件 -
libbz2
是共享库二进制文件(DLL) -
libbz2-devel
是将bzip2链接到程序中所需的头文件和静态库。
bzip2
is the binaries for using bzip2 in your shelllibbz2
is a shared object binary (DLL)libbz2-devel
is the header files and static libraries that you need to link bzip2 into your program.
您可以使用以下命令列出每个软件包的文件: pacman -F --list libbz2-devel
等。
You can list the files for each package with pacman -F --list libbz2-devel
etc.
mingw32 / mingw64目标通常具有单个软件包,其中包括所有这三件事都放在一个包中,例如 pacman -F --list mingw64 / mingw-w64-x86_64-bzip2
。
The mingw32/mingw64 targets typically have single packages that include all of those three things in the one package, e.g. pacman -F --list mingw64/mingw-w64-x86_64-bzip2
.
我假设您正在使用 msys
目标,否则将不会出现此问题。
I assume you are using msys
target as otherwise this question would not have arisen .
这篇关于如何在我的gcc编译器中链接第三方库bzip2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!