使用math.h进行sin函数和获取错误不知道为什么

时间:2014-02-19 05:27:44

标签: c gcc

../NoteConverter.c: In function ‘main’:
../NoteConverter.c:154:9: warning: variable ‘position’ set but not used [-Wunused-but-set-variable]
Finished building: ../NoteConverter.c

Building target: NoteConverter
Invoking: GCC C Linker
gcc  -o "NoteConverter"  ./NoteConverter.o   
../NoteConverter.c:21: error: undefined reference to 'sin'
collect2: error: ld returned 1 exit status
make: *** [NoteConverter] Error 1

以下是代码

/**
 * Frequency octave finder and play note
 */

 #include <stdio.h>
 #include <string.h>
 #include <math.h>

 #define DURATION 5
 #define SAMPLERATE 48000
 #define AMPLITUDE 1

 //A*sin(2*pi*n*f/R)

double yCord(int n, double freq)
{
    double y= sin(2*M_PI*freq*n/48000);
    return y;
}


int xCord(int n)
{
    double x = n/SAMPLERATE;
    return x;
}

// finds the freq from switch
int noteBaseFeq(char *note) {
    int baseNote = 0;
    int baseFreq = 0;

    const char *notes[] = { "C ", "C#", "Db", "D ", "D#", "Eb", "E ", 
        "F ", "F#", "Gb", "G ", "G#", "Ab", "A ", "A#", "Bb", "B " };

    int i;
    for( i = 0; i < 17; i++) {
        if (strcmp(note,notes[i]) == 0) {
            baseNote = i;
            break;
        }
    }

    switch (baseNote+1) {
        case 1: // c
            baseFreq = 26163;
            break;
        case 2: // c#
        case 3: // Db
            baseFreq = 27718;
            break;
        case 4: // D
            baseFreq = 29366;
            break;
        case 5: //D#
        case 6: //Eb
            baseFreq = 31113;
            break;
        case 7: //E
            baseFreq = 32963;
            break;
        case 8: //F
            baseFreq = 34923;
            break;
        case 9: //F#
        case 10: //Gb
            baseFreq = 36999;
            break;
        case 11: //G
            baseFreq = 39200;
            break;
        case 12: //G#
        case 13: //Ab
            baseFreq = 41530;
            break;
        case 14: //A
            baseFreq = 44000;
            break;
        case 15: //A#
        case 16: //Bb
            baseFreq = 46616;
            break;
        case 17: //B
            baseFreq = 49388;
            break;
    }
    return baseFreq;
}

// finds the octive based on A 4
int octaveModifier(int oct)
{
    if (oct == 4) {
        return oct;
    } else if (oct < 4) {
        oct = 4 - oct;
    } else if (oct > 4) {
        oct = oct - 4;
    }

    return oct;
}

// allows for concatenation
void append(char* s, char c)
{
    int len=strlen(s);
    s[len]=c;
    s[len+1]='\0';
}

int main() 
{
    char note;
    char modifier;
    char inputNote[256] = "";
    long long intFreq = 0;
    double freq = 0.0;
    int oct = 0;
    int modOct = 0;

    printf("Please enter an note sharp\\flat(\"b\" for flat) and octave:  \n");
    scanf("%c%c%d", &note, &modifier, &modOct);// gets input from user
    append(inputNote, note);
    append(inputNote, modifier);

    oct = octaveModifier(modOct);
    intFreq = noteBaseFeq(inputNote);

    int i;
    if(modOct > 4) {
        for(i = 0; i < oct; i++) {
            intFreq = intFreq * 2;
            freq = intFreq / 100.0;
        }
    } else if(modOct < 4) {
        for( i = 0; i < oct; i++) {
            intFreq = intFreq / 2;
            freq = intFreq / 100.0;
        }
    } else {
        freq = intFreq / 100.0;
    }

    double time = 0;
    double position = 0;
    for (i = 0; time < DURATION; i++) {
        time = xCord(i);
        position = yCord(i, freq);
    }

    return 0;
}

1 个答案:

答案 0 :(得分:6)

您可能需要与-lm关联才能连接数学库。有些平台要求;其他人没有。

修复set-but-unused变量很容易;删除它的声明和设置它的行(或使用结果 - 打印它,也许?)。