Link libsndfile source code with gcc - c

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.

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:~$

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.

Related

Running Ruby in C compile and link issues

I've been trying all day to build and run a simple Ruby inside of C program.
This is a recurring topic here, and none of them are identical to my issue nor have any of the solutions helped me. I have the ruby-dev installed.
The pkg-config command gives this:
$ pkg-config --cflags --libs ruby-2.7
-I/usr/include/x86_64-linux-gnu/ruby-2.7.0 -I/usr/include/ruby-2.7.0 -lruby-2.7 -lm
The compile command gives this:
$ gcc -I/usr/include/x86_64-linux-gnu/ruby-2.7.0 -I/usr/include/ruby-2.7.0 -I. -lruby-2.7 -o hello *.c
/usr/bin/ld: /tmp/ccdKXtnU.o: in function 'main':
hello.c:(.text+0x9): undefined reference to 'ruby_setup'
collect2: error: ld returned 1 exit status
I have tried switching up the order of the includes. I have tried removing one then the other include. I have tried using a Makefile and running it thru make. I have tried breaking the program up into multiple files. I have tried symbolically linking the architecture relative config.h file into the main header file directory.
The only thing I can think of that I haven't tried is putting the name of the ruby object library that needs to be linked in on the command line, but I don't know the name, or location, of that file.
Here's the latest rendition of the program:
#include <stdio.h>
#include <ruby.h>
int main(void)
{
if (ruby_setup()){
puts("Hola!");
}
else{
printf("Hello World\n");
}
return(0);
}
One of the reasons that pkg-config separates cflags and libs is that they go in different places in the command-line (and sometimes different commands).
If you're going to compile and link in one command, it goes like this:
c99 -o hello $(pkg-config --cflags ruby-2.7) *.c $(pkg-config --libs ruby-2.7)
There's a certain logic to this arrangement. First, we tell the compiler where to look for header files (which it must see before it compiles your program), then where to find the program to compile, and finally where to find the libraries which are referred to by the program.

Cross-compiled library not found by toolchain

I'm new to developing for embedded systems, but I have installed arm-linux-gnueabi-gcc via the Linux Mint package manager and managed to build a few programs successfully.
I'm currently struggling with getting a program to compile using libusb. I've done the following:
Downloaded and unpacked the libusb 1.0.20 sources from https://sourceforge.net/projects/libusb/.
Compiled and installed them using the following commands:
~/Downloads/libusb-1.0.20 $ ./configure --host=arm-linux-gnueabi --prefix=/opt/ --disable-udev
~/Downloads/libusb-1.0.20 $ sudo make
~/Downloads/libusb-1.0.20 $ sudo make install
(The reason for sudo-ing the make commands was because I encountered permission problems related to removing old files.)
Copied a small sample file from somewhere on the internet:
#include <libusb-1.0/libusb.h>
#include <stdio.h>
int main()
{
int i=0;
libusb_context **c = NULL;
i = libusb_init(c);
printf("\nusing libusb.h\n");
return 0;
}
Tried to build it and run it with gcc:
~/Desktop/libtest $ gcc libtest1.c -o libtest1 -lusb-1.0
~/Desktop/libtest $ ./libtest1
using libusb.h
However, when I try to do the same with arm-linux-gnueabi-gcc, it can't find the library:
~/Desktop/libtest $ arm-linux-gnueabi-gcc libtest1.c -o libtest1 -lusb-1.0
/usr/lib/gcc-cross/arm-linux-gnueabi/4.7/../../../../arm-linux-gnueabi/bin/ld: cannot find -lusb-1.0
collect2: error: ld returned 1 exit status
Where did I go wrong? Is there something else I need to do in order to use the library? Did I fail at compiling the library for the arm compiler? I didn't include the compiler output here since it's quite long, but there are no obvious errors. This might be a very stupid question, but I'm completely clueless.

GCC Linux C - Compile object header not found

I've been working on a module in C (under Linux) that requires another module (headers are in other directories).
My problem is that when I compile the code with my Makefile, the gcc compiler tells me that some headers aren't found.
gcc -c render.c
So I include the directories to find the header but here, gcc tries to find the "main" function which does not exist: it is a module...
gcc /opt/vc/include -c render.c
So I would like to know how is it possible to compile a module (output in module.o) that requires other modules?
Here are my files:
render.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "render.h"
int width,height;
int loop,counter;
int initRender(void(*setup)(void),void(*draw)(void),void(*end)(void))
{
init(&width, &height);
loop = -1;
counter = 0;
setup();
while(loop==-1)
{
Start(width, height);
draw();
End();
counter++;
}
end();
finish();
exit(0);
return 0;
}
render.h:
#include "VG/openvg.h"
#include "VG/vgu.h"
#include "fontinfo.h"
#include "shapes.h"
#ifndef RENDER_H_
#define RENDER_H_
extern int width,height;
extern int loop,counter;
int initRender(void(*setup)(void),void(*draw)(void),void(*end)(void));
#endif
Makefile:
INCLUDEFLAGS=-I/opt/vc/include -I/opt/vc/include/interface/vmcs_host/linux -I/opt/vc/include/interface/vcos/pthreads -IopenVG
LIBFLAGS=-L/opt/vc/lib -lGLESv2 -lEGL -lbcm_host -lpthread -ljpeg -LopenVG
NEEDED= openVG/libshapes.o openVG/oglinit.o
all: render
render.o: render.c
gcc -Wall -g $(INCLUDEFLAGS) -c render.c
You probably want
gcc -Wall -g -I/opt/vc/include -c render.c
this will produce a render.o object file.
Please take time to read the documentation about invoking GCC. In particular, check what every option -Wall, -g, -I and -c means. IMHO the first two are very important.
Later, you probably want to link all your object files into an executable, with some external libraries. Perhaps you want something like
gcc -g -Wall -L/opt/vc/lib render.o main.o -lvc -o myprogram
(you really want the -Wall and -g options; IMHO you need to be an expert to dare avoiding them; once you have debugged your program and want to benchmark it, add -O2 for optimizations)
But surely, you want other options.
Notice that order of arguments to gcc matters a lot
Of course, you should learn about GNU make and you need to use it. See this and that examples. You might use make --trace (with recent make) or remake to debug your Makefile (which is not good). You should also run once make -p to understand more the builtin rules of make.
Perhaps you want a library, then read the Program Library HowTo.

How do I add a directory to C header include path?

I am having trouble installing a dependency for a program that itself depends on pcre.h. I have this installed to /opt/local/include, but the C compiler does not see it and thus gives me:
error: pcre.h: No such file or directory
I have confirmed this by writing a hello world program that tries to include it:
#include <pcre.h>
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
return 0;
}
This also gives the error unless I specify the path as </opt/local/include/pcre.h>.
I would like the C compiler to find this by default but I do not know where this is configured. Tab completion hasn't revealed any HEADER_PATH environment variables and I cannot find anything like it that isn't specific to XCode. I am, however, using Mac OSX Snow Leopard on the off chance that makes a difference.
Use -I /opt/local/include on the command line or C_INCLUDE_PATH=/opt/local/include in the environment.
Use the pcre-config utility to get the right flags:
$ pcre-config --libs --cflags
-L/opt/local/lib -lpcre
-I/opt/local/include
If you're compiling via the command line,
$ gcc -Wall -g `pcre-config --libs --cflags` main.c

Undefined reference when using ncurses on linux

I'm trying to start developing a program using ncurses on Linux. I can't even get the Hello World example to compile. Here's the code:
#include <curses.h>
int main()
{
initscr();
printw("Hello, world.");
refresh();
getch();
endwin();
return 0;
}
When I attempt to compile, I get:
hello.c:(.text+0x12): undefined reference to `initscr'
For every one of those called functions.
I installed ncurses via apt-get, and also by downloading the sources and compiling, installing, etc.
I have tried #include both curses.h and ncurses.h.
What is going on?
Have you used the -lcurses option when linking?
Including the header files let the code compile (because the compiler knows what the function call looks like from the .h file), but the linker needs the library file to find the actual code to link into your program.
As Greg Hewgill said, you need to pass in -lcurses or -lncurses to link to the curses library.
gcc -o hello hello.c -lncurses
You also probably mean to use initscr() and getch(). Once I make those substitutions, the above compiles for me.
I was having a similar issue and found a solution which helped me, but was slightly different from the other answers posted here. I was trying to use the panels library with curses and my compile command was:
$ gcc -o hello hello.c -lncurses -lpanel
when I read the other answers, I was baffled because I was including the -lncurses flag, but it still was not compiling, and with similar errors to what you were getting:
$ gcc -o hello hello.c -lncurses -lpanel
/usr/lib/gcc/i686-linux-gnu/4.7/../../../../lib/libpanel.a(p_new.o): In function `new_panel':
p_new.c:(.text+0x18): undefined reference to `_nc_panelhook'
I finally found my answer in the tldp:
"To use panels library functions, you have to include panel.h and to link the program with panels library the flag -lpanel should be added along with -lncurses in that order."
So, it appears that order is important when using the compile flags! I tried switching the order:
gcc -o hello hello.c -lpanel -lncurses
This allowed it to compile successfully. I know you already have your answer, so I hope this helps someone.
For anyone having similar problems: -lx arguments, where x is your library, should always follow the source and object files.

Resources