You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux环境下C项目引入libsndfile与PortAudio的方法

项目情况

我当前正在开发一个TUI WAV播放器,处于读取并播放WAV音频的初始阶段。
我接触C语言仅4周,是纯新手,未使用任何IDE,通过cc命令编译代码(编译命令为cc main.c -o main -lm)。

已完成操作

我通过yay/pacman安装了PortAudio,从官网下载了libsndfile源码,并按照文档完成编译安装(执行命令./configure && make && make install)。
libsndfile源码目录与我的main.c代码位于同一文件夹下。
ls -a命令的输出如下:

.    c-econio  'Goran Bregović - Kalashnikov.mp3'   kalasnikow.wav      main.c       test.c
 ..   .git      'Goran Bregović - Kalashnikov.wav'   libsndfile-1.0.28   random.mp3

c-econio和libsndfile-1.0.28是我计划使用的库目录(econio将在后续使用)。如前所述,PortAudio已通过包管理器安装。
我不确定#include <portaudio.h>是否生效,因为没有libsndfile无法进行测试。

核心问题

请问如何在代码中引入PortAudio和libsndfile,以及如何进行测试?编译命令(cc)需要添加什么参数?我使用的是Linux系统。

代码

main.c

#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <portaudio.h>

int listwav() {
    int i = 1;
     DIR *d;
    struct dirent *entry;
    d = opendir("."); // 打开当前目录
    if (d) {
        while ((entry = readdir(d)) != NULL) {  // 遍历所有文件并输出WAV文件
            if (strlen(entry->d_name) > 4 && strcmp(entry->d_name + strlen(entry->d_name) - 4, ".wav") == 0) {
              printf("\t%d %s\n",i, entry->d_name);
              i++; 
            }
        }
        closedir(d);
    }
    return (0);
}

void play(){ // 播放音频计划用"ffplay -v 0 -nodisp -autoexit"
  printf("What to play? \n");
  listwav();
  // system("playerctl play");
}

void stop(){
  printf("Stopped\n");
  // system("playerctl stop");
}

int chose(){
  int chosen = 0;    
  
  printf("Choose what to do:\n 1.Play\n 2.Stop\n 3.Exit\n Chosen: "); scanf("%d", &chosen);
  while (chosen != 3){
    switch (chosen) {
      case 1: play(); break;
      case 2: stop(); break;
    }
    return 0;
  }
}

 /// -----
 int main(){
   chose();

 }

补充:我的项目文件夹未更新版本可查看对应仓库内容。

内容的提问来源于stack exchange,提问作者AutisticAllosaurus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 02:16:11