1

I just cloned libsndfile and created program.c file with the following contents:

#include <stdio.h>
#include <sndfile.h>
main() {
    printf("hello world");
}

My goal is to get this file to compile using gcc (if indeed sndfile.h is the right header to include), however, I am not competent with c code nor the gcc compiler.

So far I've been referencing the gcc docs and the libsndfile FAQ and haven't been able to come up with a solution. I'm not sure if I even have a 'library' yet to feed the gcc -l option. Do I need to go through some sort of build process with the libsndfile source code first or can I 'link' to .c files?

So far, with program.c and the libsndfile clone directory in the same directory, I've tried the following:

gcc 'pkg-config --cflags libsndfile' program.c -o hello
gcc 'pkg-config --cflags ./libsndfile/sndfile.pc.in' program.c -o hello

I'm coding on my raspberry pi model B and did not install libsndfile, so, nothing is on my path... maybe I should rename sndfile.pc.in to sndfile.pc and somehow put it on my path? (not a linux guru either!)

If there are some resources you think I should study please comment! I'm perfectly willing to accept answers that simply push me enough to figure things out. Any help would be much appreciated.

lonious
  • 676
  • 9
  • 25
  • Is there a package `libsndfile-dev` or `libsndfile1-dev` for your OS? Using it might be easier than compiling it yourself, unless you need a specific version of it. This package should contain a pkg-config file `sndfile.pc` with the correct settings that you could use. – Karsten Koop May 28 '18 at 10:57
  • There's instructions on how to "setup a build environment for debian", which includes `sudo apt install autoconf autogen automake build-essential libasound2-dev \ libflac-dev libogg-dev libtool libvorbis-dev pkg-config python` that I should probably try – lonious May 28 '18 at 11:04
  • 1
    Seems like I need to learn quite a bit more about the GNU Build System – lonious May 28 '18 at 11:17

2 Answers2

1

First make sure you have gcc installed:

sudo apt-get install gcc

Then, install libsndfile1-dev:

sudo apt-get install libsndfile1-dev

I slightly fixed you code to avoid some warnings:

#include <stdio.h>
#include <sndfile.h>

int main(void) {
  printf("hello world\n");
  return 0;
}

Now you can compile:

gcc -o hello program.c

Execution:

bebs@rasp:~$ ./hello                                     
hello world                                                                              
bebs@rasp:~$      
alpereira7
  • 1,522
  • 3
  • 17
  • 30
  • getting the right packagenames is sometimes a pita especially if they all seem to have a very similar name and its not really clear which one you need. fixed a completly different issue I had – aldr Nov 18 '21 at 12:21
0

A small supplement of @alpereira7's answer. It would cause linking failure without its lib. gcc -lsndfile -o hello program.c should be a 100% solution.

Frank Yin
  • 1
  • 1