2013-03-05 3 views
1

정말 기본 프로그램을 C 라이브러리를 가져 오는, 그러나 나는 다음과 같은 오류 받고 있어요 :C에 창에

tcc: error: undefined symbol 'GetFloat'

어떤 문제가 있고 난 그것을 어떻게 해결합니까를? cs50.c와 cs50.h를 스크립트와 동일한 디렉토리에 넣었습니다.

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

int main(void) 
{ 
    printf("enter change owed: "); 
    float x = GetFloat(); 
    printf("you entered %f\n", x); 
} 

여기 cs50.h

/**************************************************************************** 
* CS50 Library 4 
* https://manual.cs50.net/Library 
* 
* Based on Eric Roberts' genlib.c and simpio.c. 
* 
* Copyright (c) 2011, 
* Glenn Holloway <[email protected]> 
* David J. Malan <[email protected]> 
* All rights reserved. 
* 
* BSD 3-Clause License 
* http://www.opensource.org/licenses/BSD-3-Clause 
* 
* Redistribution and use in source and binary forms, with or without 
* modification, are permitted provided that the following conditions are 
* met: 
* 
* * Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer. 
* * Redistributions in binary form must reproduce the above copyright 
* notice, this list of conditions and the following disclaimer in the 
* documentation and/or other materials provided with the distribution. 
* * Neither the name of CS50 nor the names of its contributors may be used 
* to endorse or promote products derived from this software without 
* specific prior written permission. 
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
***************************************************************************/ 

#ifndef _CS50_H 
#define _CS50_H 

#include <float.h> 
#include <limits.h> 
#include <stdbool.h> 
#include <stdlib.h> 


/* 
* Our own data type for string variables. 
*/ 

typedef char *string; 


/* 
* Reads a line of text from standard input and returns the equivalent 
* char; if text does not represent a char, user is prompted to retry. 
* Leading and trailing whitespace is ignored. If line can't be read, 
* returns CHAR_MAX. 
*/ 

char GetChar(void); 


/* 
* Reads a line of text from standard input and returns the equivalent 
* double as precisely as possible; if text does not represent a 
* double, user is prompted to retry. Leading and trailing whitespace 
* is ignored. For simplicity, overflow and underflow are not detected. 
* If line can't be read, returns DBL_MAX. 
*/ 

double GetDouble(void); 


/* 
* Reads a line of text from standard input and returns the equivalent 
* float as precisely as possible; if text does not represent a float, 
* user is prompted to retry. Leading and trailing whitespace is ignored. 
* For simplicity, overflow and underflow are not detected. If line can't 
* be read, returns FLT_MAX. 
*/ 

float GetFloat(void); 


/* 
* Reads a line of text from standard input and returns it as an 
* int in the range of [-2^31 + 1, 2^31 - 2], if possible; if text 
* does not represent such an int, user is prompted to retry. Leading 
* and trailing whitespace is ignored. For simplicity, overflow is not 
* detected. If line can't be read, returns INT_MAX. 
*/ 

int GetInt(void); 


/* 
* Reads a line of text from standard input and returns an equivalent 
* long long in the range [-2^63 + 1, 2^63 - 2], if possible; if text 
* does not represent such a long long, user is prompted to retry. 
* Leading and trailing whitespace is ignored. For simplicity, overflow 
* is not detected. If line can't be read, returns LLONG_MAX. 
*/ 

long long GetLongLong(void); 


/* 
* Reads a line of text from standard input and returns it as a 
* string (char *), sans trailing newline character. (Ergo, if 
* user inputs only "\n", returns "" not NULL.) Returns NULL 
* upon error or no input whatsoever (i.e., just EOF). Leading 
* and trailing whitespace is not ignored. Stores string on heap 
* (via malloc); memory must be freed by caller to avoid leak. 
*/ 

string GetString(void); 



#endif 

그리고 마지막으로, 여기

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

#include "cs50.h" 


/* 
* Reads a line of text from standard input and returns the equivalent 
* char; if text does not represent a char, user is prompted to retry. 
* Leading and trailing whitespace is ignored. If line can't be read, 
* returns CHAR_MAX. 
*/ 

char GetChar(void) 
{ 
    // try to get a char from user 
    while (true) 
    { 
     // get line of text, returning CHAR_MAX on failure 
     string line = GetString(); 
     if (line == NULL) 
      return CHAR_MAX; 

     // return a char if only a char (possibly with 
     // leading and/or trailing whitespace) was provided 
     char c1, c2; 
     if (sscanf(line, " %c %c", &c1, &c2) == 1) 
     { 
      free(line); 
      return c1; 
     } 
     else 
     { 
      free(line); 
      printf("Retry: "); 
     } 
    } 
} 


/* 
* Reads a line of text from standard input and returns the equivalent 
* double as precisely as possible; if text does not represent a 
* double, user is prompted to retry. Leading and trailing whitespace 
* is ignored. For simplicity, overflow and underflow are not detected. 
* If line can't be read, returns DBL_MAX. 
*/ 

double GetDouble(void) 
{ 
    // try to get a double from user 
    while (true) 
    { 
     // get line of text, returning DBL_MAX on failure 
     string line = GetString(); 
     if (line == NULL) 
      return DBL_MAX; 

     // return a double if only a double (possibly with 
     // leading and/or trailing whitespace) was provided 
     double d; char c; 
     if (sscanf(line, " %lf %c", &d, &c) == 1) 
     { 
      free(line); 
      return d; 
     } 
     else 
     { 
      free(line); 
      printf("Retry: "); 
     } 
    } 
} 


/* 
* Reads a line of text from standard input and returns the equivalent 
* float as precisely as possible; if text does not represent a float, 
* user is prompted to retry. Leading and trailing whitespace is ignored. 
* For simplicity, overflow and underflow are not detected. If line can't 
* be read, returns FLT_MAX. 
*/ 

float GetFloat(void) 
{ 
    // try to get a float from user 
    while (true) 
    { 
     // get line of text, returning FLT_MAX on failure 
     string line = GetString(); 
     if (line == NULL) 
      return FLT_MAX; 

     // return a float if only a float (possibly with 
     // leading and/or trailing whitespace) was provided 
     char c; float f; 
     if (sscanf(line, " %f %c", &f, &c) == 1) 
     { 
      free(line); 
      return f; 
     } 
     else 
     { 
      free(line); 
      printf("Retry: "); 
     } 
    } 
} 


/* 
* Reads a line of text from standard input and returns it as an 
* int in the range of [-2^31 + 1, 2^31 - 2], if possible; if text 
* does not represent such an int, user is prompted to retry. Leading 
* and trailing whitespace is ignored. For simplicity, overflow is not 
* detected. If line can't be read, returns INT_MAX. 
*/ 

int GetInt(void) 
{ 
    // try to get an int from user 
    while (true) 
    { 
     // get line of text, returning INT_MAX on failure 
     string line = GetString(); 
     if (line == NULL) 
      return INT_MAX; 

     // return an int if only an int (possibly with 
     // leading and/or trailing whitespace) was provided 
     int n; char c; 
     if (sscanf(line, " %d %c", &n, &c) == 1) 
     { 
      free(line); 
      return n; 
     } 
     else 
     { 
      free(line); 
      printf("Retry: "); 
     } 
    } 
} 


/* 
* Reads a line of text from standard input and returns an equivalent 
* long long in the range [-2^63 + 1, 2^63 - 2], if possible; if text 
* does not represent such a long long, user is prompted to retry. 
* Leading and trailing whitespace is ignored. For simplicity, overflow 
* is not detected. If line can't be read, returns LLONG_MAX. 
*/ 

long long GetLongLong(void) 
{ 
    // try to get a long long from user 
    while (true) 
    { 
     // get line of text, returning LLONG_MAX on failure 
     string line = GetString(); 
     if (line == NULL) 
      return LLONG_MAX; 

     // return a long long if only a long long (possibly with 
     // leading and/or trailing whitespace) was provided 
     long long n; char c; 
     if (sscanf(line, " %lld %c", &n, &c) == 1) 
     { 
      free(line); 
      return n; 
     } 
     else 
     { 
      free(line); 
      printf("Retry: "); 
     } 
    } 
} 


/* 
* Reads a line of text from standard input and returns it as a 
* string (char*), sans trailing newline character. (Ergo, if 
* user inputs only "\n", returns "" not NULL.) Returns NULL 
* upon error or no input whatsoever (i.e., just EOF). Leading 
* and trailing whitespace is not ignored. Stores string on heap 
* (via malloc); memory must be freed by caller to avoid leak. 
*/ 

string GetString(void) 
{ 
    // growable buffer for chars 
    string buffer = NULL; 

    // capacity of buffer 
    unsigned int capacity = 0; 

    // number of chars actually in buffer 
    unsigned int n = 0; 

    // character read or EOF 
    int c; 

    // iteratively get chars from standard input 
    while ((c = fgetc(stdin)) != '\n' && c != EOF) 
    { 
     // grow buffer if necessary 
     if (n + 1 > capacity) 
     { 
      // determine new capacity: start at 32 then double 
      if (capacity == 0) 
       capacity = 32; 
      else if (capacity <= (UINT_MAX/2)) 
       capacity *= 2; 
      else 
      { 
       free(buffer); 
       return NULL; 
      } 

      // extend buffer's capacity 
      string temp = realloc(buffer, capacity * sizeof(char)); 
      if (temp == NULL) 
      { 
       free(buffer); 
       return NULL; 
      } 
      buffer = temp; 
     } 

     // append current character to buffer 
     buffer[n++] = c; 
    } 

    // return NULL if user provided no input 
    if (n == 0 && c == EOF) 
     return NULL; 

    // minimize buffer 
    string minimal = malloc((n + 1) * sizeof(char)); 
    strncpy(minimal, buffer, n); 
    free(buffer); 

    // terminate string 
    minimal[n] = '\0'; 

    // return string 
    return minimal; 
} 
+3

어떻게 만드시겠습니까? –

+0

사용중인 링커에 따라 다릅니다. 가장 쉬운 방법은 메인을 cs50.c에 추가하는 것입니다. – QuentinUK

+0

포함 경로가 맞습니까? 헤더 파일 ('cs50.h')이 실제로 발견 되었습니까? –

답변

3

귀하의 코드는 괜찮 cs50.c입니다 : 다음은 내 주요 파일입니다. 나는 tcc에 익숙하지 않지만 오류는 #include "cs50.h"와 관련이없는 것처럼 보입니다. 내가 아는 바로는 귀하의 문제는 연결과 관련이 있습니다.

gcc -c cs50.c가 // 보통 CS50라는 이름의 객체를 만듭니다

는 먼저 cs50.c에서 중간 파일을 구축하고

다음

내가 GCC와 함께 프로그램을 컴파일 할 방법 cs50.h하는 TCC 말할 필요 .o 인

gcc cs50.o grdy.c -o grdy.exe 모두 함께 실행 (이 grdy.c에있을 주요 예상)이 도움이

희망을 구축하기 // 링크.

+0

이 스 니펫을 보내 주셔서 감사합니다. 프로그램을 컴파일하고 실행하기 위해 메모장 ++을 사용하고 있었는데, 이것이 문제였던 것처럼 보입니다. 나는 창문을 cmd를 사용하고 지금은 잘 작동하는 것 ... 이상한 – haosmark