Rave
Rave

Reputation: 843

Compiling a C program

i am trying to compile this code, but if i do using:

gcc prt.c portaudio.h -o prt

but i get this error:

main.c:47: undefined reference to `Pa_OpenDefaultStream'
main.c:62: undefined reference to `Pa_StartStream'
main.c:65: undefined reference to `Pa_Sleep'
main.c:66: undefined reference to `Pa_StopStream'
main.c:69: undefined reference to `Pa_CloseStream'
main.c:72: undefined reference to `Pa_Terminate'
main.c:78: undefined reference to `Pa_Terminate'

i don't know why, then i though it might be beacuse i don't have a rule (make file) so i made one:

main: main.o
    gcc main.o -o main

main.o: main.c portaudio.h
    gcc -c main.c

but when i try to run it through cygwin: using "Make" i get this message:

"make: *** No targets specified and no makefile found. Stop.

I don't understand the problem, please help me is something wrong with my makefile or is there something else wrong.

also this is the code: main.c

#include <stdio.h>
#include "portaudio.h"

#define SAMPLE_RATE (44100)


typedef struct
{
   float left_phase;
   float right_phase;
}
paTestData;

static int patestCallback( const void *inputBuffer, void *outputBuffer,
                       unsigned long framesPerBuffer,
                       const PaStreamCallbackTimeInfo* timeInfo,
                       PaStreamCallbackFlags statusFlags,
                       void *userData )
{
/* Cast data passed through stream to our structure. */
paTestData *data = (paTestData*)userData;
float *out = (float*)outputBuffer;
unsigned int i;
(void) inputBuffer; /* Prevent unused variable warning. */

for( i=0; i<framesPerBuffer; i++ )
{
    *out++ = data->left_phase;  /* left */
    *out++ = data->right_phase;  /* right */
    /* Generate simple sawtooth phaser that ranges between -1.0 and 1.0. */
    data->left_phase += 0.01f;
    /* When signal reaches top, drop back down. */
    if( data->left_phase >= 1.0f ) data->left_phase -= 2.0f;
    /* higher pitch so we can distinguish left and right. */
    data->right_phase += 0.03f;
    if( data->right_phase >= 1.0f ) data->right_phase -= 2.0f;
}
return 0;
}


static paTestData data;

int main (void) {
PaStream *stream;
PaError err;
err = Pa_OpenDefaultStream( &stream,
                            0,          /* no input channels */
                            2,          /* stereo output */
                            paFloat32,  /* 32 bit floating point output */
                            SAMPLE_RATE,
                            256,        /* frames per buffer, i.e. the number
                                               of sample frames that PortAudio will
                                               request from the callback. Many apps
                                               may want to use
                                               paFramesPerBufferUnspecified, which
                                               tells PortAudio to pick the best,
                                               possibly changing, buffer size.*/
                            patestCallback, /* this is your callback function */
                            &data ); /*This is a pointer that will be passed to
                                               your callback*/
                         err = Pa_StartStream( stream );
                         if( err != paNoError ) goto error;

                         Pa_Sleep(9*1000);
                        err = Pa_StopStream( stream );
                        if( err != paNoError ) goto error;

                        err = Pa_CloseStream( stream );
                        if( err != paNoError ) goto error;

                        err = Pa_Terminate( );
                        if( err != paNoError ) goto error;

                        printf("Test finished.\n");
                        return err;
                    error:
                        Pa_Terminate();

                        return err;




   }

and the header file portaudio.h: Portaudio.h if you want cleaner view of main.c: main.c

I am not so sure why these messages/errors/warning are coming, please help.

also this is my folder view: enter image description here

Upvotes: 1

Views: 1477

Answers (8)

zentara
zentara

Reputation: 11

I also need to add -lm, otherwise it throws an error.
So gcc -o test test.c -lm -lportaudio

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 754800

You seem to be using functions from a library for the 'Port Audio' facility, but your link line does not tell the C compiler how to find that library - so the functions show up as undefined references.

Your link line should look something like:

gcc -o main main.o -lpa

That should be macroized, but the gist is correct.

This assumes the library is in 'libpa.a' or thereabouts. If it is in 'libportaudio.so', then use -lportaudio instead of -lpa.

Using macros in the makefile:

PROGRAM = main
SOURCE  = main.c
OBJECT  = $(SOURCE:.c=.o)
LIBDIR  = /cygdrive/c/installdir/portaudio/lib
LIBRARY = $(LIBDIR)/portaudio_x86.lib

$(PROGRAM): $(OBJECT)
    $(CC) $(CFLAGS) -o $@ $(OBJECT) $(LDFLAGS) $(LIBRARY)


main.o: main.c portaudio.h

You should not need an explicit compilation command for main.o; make should be able to deduce that from its internal rules. Note that the character before $(CC) must be a TAB and not spaces.

Upvotes: 2

BlackJack
BlackJack

Reputation: 2876

You are probably not linking to those libraries libs (.so or .a) look at the documentation and see what libs you need to link your program with.

The other thing is that when you run "make -f Makefile" you need to have a tab in your makefile before the "gcc..." lines i.e. the command lines.

Upvotes: 1

Stu
Stu

Reputation: 380

It seems like you need to include the library (with a -l[library name] option. A search of portaudio compile commands shows libportaudio.a included in the gcc options.

Upvotes: 1

BjoernD
BjoernD

Reputation: 4780

Your initial problem ("Undefined reference to...") is a message from the linker saying it cannot find a definition of the functions mentioned. This means you need to add a linker argument saying that you want to add the library providing these functions (lib portaudio?) to your program. GCC's command line parameter to do so is "-l"

Upvotes: 1

SystemRaen
SystemRaen

Reputation: 855

Did portaudio come with a .lib or anything? The header file only contains the name of the functions, not the definitions. You'll need to link against the library to get the functionality for all of those functions

Upvotes: 1

aardvarkk
aardvarkk

Reputation: 15996

It would also appear that you need the PortAudio library to link to or those functions will not be defined. That is, unless they're defined in the header (I haven't used that library before...)

Upvotes: 1

Edwin Buck
Edwin Buck

Reputation: 70949

The make command only looks for a file called makefile or Makefile, to use make with a differently named makefile, you need to do make -f otherfile target.

Rename your file Make file to Makefile to have make look at its contents. Also, verify that you use one tab character (no spaces) in all of the commands under a target. You might have done that, but your cut-and-paste of the contents in this posting doesn't let us know if that is really how it is.

Upvotes: 1

Related Questions