How to compile libusb as shared/static library

本文详细介绍了使用Android NDK编译libusb库的过程,包括配置和make两步,最终生成静态库和动态库。提供了libusb.mk文件示例,展示了核心源文件和编译设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am trying to build libusb as shared library as follows.

Following are two steps. 1. Configure 2. Make.

默认会生成静态库和动态在 /libusb/.lib/目录下。

 

android ndk 编译动态和静态库的方法

libusb.mk

# Android build config for libusb
# Copyright © 2012-2013 RealVNC Ltd. <toby.gray@realvnc.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#

LOCAL_PATH:= $(call my-dir)
LIBUSB_ROOT_REL:= ../..
LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..

# libusb

include $(CLEAR_VARS)

LIBUSB_ROOT_REL:= ../..
LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..

LOCAL_SRC_FILES := \
  $(LIBUSB_ROOT_REL)/libusb/core.c \
  $(LIBUSB_ROOT_REL)/libusb/descriptor.c \
  $(LIBUSB_ROOT_REL)/libusb/hotplug.c \
  $(LIBUSB_ROOT_REL)/libusb/io.c \
  $(LIBUSB_ROOT_REL)/libusb/sync.c \
  $(LIBUSB_ROOT_REL)/libusb/strerror.c \
  $(LIBUSB_ROOT_REL)/libusb/os/linux_usbfs.c \
  $(LIBUSB_ROOT_REL)/libusb/os/poll_posix.c \
  $(LIBUSB_ROOT_REL)/libusb/os/threads_posix.c \
  $(LIBUSB_ROOT_REL)/libusb/os/linux_netlink.c

LOCAL_C_INCLUDES += \
  $(LOCAL_PATH)/.. \
  $(LIBUSB_ROOT_ABS)/libusb \
  $(LIBUSB_ROOT_ABS)/libusb/os

LOCAL_EXPORT_C_INCLUDES := \
  $(LIBUSB_ROOT_ABS)/libusb

#LOCAL_LDLIBS := -llog

LOCAL_MODULE := libusb1.0

#include $(BUILD_SHARED_LIBRARY)
include $(BUILD_STATIC_LIBRARY)

 

 

<think>我们正在处理一个具体的错误:在CentOS 8上使用Meson构建时遇到错误,提示找不到'asan' C共享或静态库。这个错误通常发生在尝试构建启用了AddressSanitizer(ASan)的项目时,但系统缺少必要的ASan库或开发文件。 AddressSanitizer(ASan)是GCC和Clang中的一个内存错误检测工具。在CentOS 8中,ASan库由`libasan`包提供。因此,解决这个错误的方法是安装相应的开发包。 ### 解决方案步骤 1. **安装libasan开发包**: 在CentOS 8中,运行以下命令安装libasan: ```bash sudo dnf install libasan ``` 注意:对于不同的编译器版本,可能需要安装特定版本的libasan。但通常情况下,安装`libasan`包会安装与系统默认编译器相匹配的版本。CentOS 8默认使用GCC 8.x,因此会安装`libasan`的对应版本。 2. **验证安装**: 安装完成后,可以检查ASan库是否已安装: ```bash ls /usr/lib*/libasan.so.* ``` 如果看到类似`/usr/lib64/libasan.so.5.0.0`的文件(具体版本可能不同),则表示安装成功。 3. **重新运行Meson构建**: 安装完依赖后,重新生成构建目录并构建: ```bash meson setup --reconfigure builddir meson compile -C builddir ``` ### 可能的问题扩展 - **如果安装libasan后问题仍然存在?** 可能是Meson配置中指定了错误的编译器或链接器路径。确保使用系统默认的编译器(gcc/g++)或明确指定编译器路径。另外,检查Meson构建文件(meson.build)中是否正确地设置了ASan选项。 在Meson中启用ASan通常是通过构建选项(如`b_sanitize`)来设置的。例如,在配置构建目录时: ```bash meson setup builddir -Db_sanitize=address ``` 确保在配置时没有其他冲突的选项。 - **如果项目需要其他Sanitizer(如TSan、UBSan)?** 类似地,对于ThreadSanitizer(TSan)需要安装`libtsan`,对于UndefinedBehaviorSanitizer(UBSan)需要安装`libubsan`。安装命令: ```bash sudo dnf install libtsan libubsan ``` ### 总结 该错误的根本原因是缺少AddressSanitizer的运行时库。在CentOS 8中,通过安装`libasan`包即可解决。如果项目需要其他Sanitizer,则安装对应的库。 §§相关问题§§ 1. 如何在CentOS 8中安装多个GCC版本并切换使用? 2. Meson构建系统中如何配置不同的Sanitizer选项? 3. 如何解决AddressSanitizer运行时的内存泄漏检测问题?</think>在 CentOS 8 上使用 Meson 构建时遇到 "c shared or static library asan not found" 错误,这通常表示系统缺少 AddressSanitizer (ASan) 所需的开发库。以下是完整的解决方案: ### 问题原因 `asan` 是 GCC/Clang 的 AddressSanitizer 工具,用于检测内存错误。错误提示表明: 1. 系统缺少 ASan 运行时库(`libasan`) 2. Meson 配置中启用了 ASan 但未安装依赖 3. 编译器工具链不完整 ### 解决方案 #### 1. 安装 ASan 开发库 ```bash # 安装核心 ASan 库 sudo dnf install libasan -y # 安装完整工具链(包含 ASan 支持) sudo dnf install gcc-toolset-10-libasan-devel gcc-toolset-10-gcc-c++ -y ``` #### 2. 配置编译器环境 ```bash # 启用开发工具集 source /opt/rh/gcc-toolset-10/enable # 验证 ASan 可用性 gcc -fsanitize=address -v ``` #### 3. 更新 Meson 配置 在 `meson.build` 文件中添加 ASan 选项: ```meson # 启用 ASan 支持 add_project_arguments('-fsanitize=address', language: 'c') add_project_link_arguments('-fsanitize=address', language: 'c') ``` #### 4. 重建项目 ```bash # 清除旧配置 rm -rf builddir # 重新配置并构建 meson setup builddir -Db_sanitize=address meson compile -C builddir ``` ### 验证修复 ```bash # 检查 ASan 库路径 ls -l /usr/lib*/libasan.so.* # 测试简单程序 echo 'int main(){return 0;}' | gcc -x c -fsanitize=address - ``` ### 替代方案:使用 LLVM 工具链 ```bash # 安装 LLVM 的 ASan sudo dnf install clang compiler-rt -y # 配置 Meson 使用 Clang meson setup builddir --prefix=/usr -Db_sanitize=address -Dc_compiler=clang -Dcpp_compiler=clang++ ``` > **关键提示**:CentOS 8 默认 GCC 8 对 ASan 支持有限,建议使用 `gcc-toolset-10` 或更高版本[^1]。若仍遇到问题,在 `meson.build` 中添加 `add_project_link_arguments('-static-libasan', language: 'c')` 强制静态链接[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值