简体   繁体   English

编译 C 程序

[英]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:但是当我尝试通过 cygwin 运行它时:使用“Make”我收到这条消息:

"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.我不明白这个问题,请帮助我是我的 makefile 有问题还是有其他问题。

also this is the code: main.c这也是代码: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和 header 文件 portaudio.h: Portaudio.h如果您想要更清晰的 main.c: main.c视图

I am not so sure why these messages/errors/warning are coming, please help.我不太确定为什么会出现这些消息/错误/警告,请帮忙。

also this is my folder view:这也是我的文件夹视图:在此处输入图像描述

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.您似乎正在为“端口音频”设施使用库中的函数,但您的链接行并没有告诉 C 编译器如何找到该库 - 因此这些函数显示为未定义的引用。

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.这假设库位于“libpa.a”或附近。 If it is in 'libportaudio.so', then use -lportaudio instead of -lpa .如果它在 'libportaudio.so' 中,则使用-lportaudio而不是-lpa

Using macros in the makefile:在 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 ;您不应该需要main.o的显式编译命令; make should be able to deduce that from its internal rules. make应该能够从其内部规则中推断出这一点。 Note that the character before $(CC) must be a TAB and not spaces.请注意, $(CC)之前的字符必须是TAB而不是空格。

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 . make命令只查找名为makefileMakefile的文件,要使用不同名称的 makefile 的 make,您需要执行make -f otherfile target

Rename your file Make file to Makefile to have make look at its contents.将您的文件Make文件重命名为Makefile以查看其内容。 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.你可能已经这样做了,但是你在这篇文章中的内容的剪切和粘贴并没有让我们知道它是否真的是这样。

It would also appear that you need the PortAudio library to link to or those functions will not be defined.您似乎还需要链接到 PortAudio 库,否则将不会定义这些函数。 That is, unless they're defined in the header (I haven't used that library before...)也就是说,除非它们在 header 中定义(我之前没有使用过那个库......)

Did portaudio come with a.lib or anything? portaudio 是否带有 a.lib 或任何东西? The header file only contains the name of the functions, not the definitions. header 文件仅包含函数的名称,而不包含定义。 You'll need to link against the library to get the functionality for all of those functions您需要链接库以获取所有这些功能的功能

Your initial problem ("Undefined reference to...") is a message from the linker saying it cannot find a definition of the functions mentioned.您最初的问题(“未定义对...的引用”)是来自linker的消息,说它找不到所提到的函数的定义。 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.这意味着您需要添加一个 linker 参数,表示您想要将提供这些功能的库(lib portaudio?)添加到您的程序中。 GCC's command line parameter to do so is "-l" GCC 的命令行参数是“-l”

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.似乎您需要包含库(带有-l[library name]选项。搜索 portaudio 编译命令显示libportaudio.a包含在 gcc 选项中。

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.您可能没有链接到那些库库(.so 或.a)查看文档并查看需要哪些库来链接您的程序。

The other thing is that when you run "make -f Makefile" you need to have a tab in your makefile before the "gcc..." lines ie the command lines.另一件事是,当您运行“make -f Makefile”时,您需要在 makefile 中的“gcc ...”行之前有一个选项卡,即命令行。

I also need to add -lm , otherwise it throws an error.我还需要添加-lm ,否则会引发错误。
So gcc -o test test.c -lm -lportaudio所以gcc -o test test.c -lm -lportaudio

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM