2014-04-21 2 views
0
#include <stdio.h> 
#include <stdlib.h> 

void reverse(char* lines[], int count) 
{ 
    for (int i = count-1; i >= 0; i--) 
    { 
      printf("%s", lines[i]); 
    } 
} 

.메인이 내 기능을 인식하지 못합니다.

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include "sortutil.h" 
#include "reverse.h" 


int getarray(char *lines[]); 
void printarray(char *lines[], int max); 

int main(int argc, char* argv[]) 
{ 
    char* arr[100]; 
    int numlines = getarray(arr); 
    printf("There are %d lines\n", numlines); 
    printarray(arr, numlines); 


    for (int i = 1; i < argc; i++) 
    { 
      if (strcmp(argv[i], "-s") == 0) 
      { 
        sortutil(arr); 
        printarray(arr, numlines); 
      } 
      if (strcmp(argv[i], "-r") == 0) 
      { 
        reverse(arr, numlines); 
        printarray(arr, numlines); 
      } 

    } 
} 

int getarray(char *lines[]) 
{ 
    int i = 0; 
    char *text = (char *)malloc(200); 
    while (fgets(text, 200, stdin) != NULL) 
    { 
     lines[i] = text; 
     i++; 
     text = (char *)malloc(200); 
    } 
    return i; 
} 

void printarray(char *lines[], int max) 
{ 
    for (int i = 0; i < max; i++) 
    { 
     printf("%s\n\n", lines[i]); 
    } 
} 

주 기능을 컴파일 할 때 'reverse'에 대한 정의되지 않은 참조가 있음을 알려줍니다. 나는 #include "reverse.h"을했기 때문에 역 기능을 보는 데 문제가 없어야합니다. 내가 뭔가를 놓치고 있습니까

+0

"reverse.h"는 역방향에 대해 무엇을 말합니까? 컴파일 또는 링크 중 오류가 발생합니까? –

+0

@Mike 링크가 분명합니다. "정의되지 않은 참조"는 연계 문제입니다. – littleadv

답변

2

구현이 누락되었습니다. 프로토 타입을 정의했지만 함수 본문 자체가 없습니다. 별도의 파일에 있으므로 링커에 알려야합니다. main.cc를 컴파일 할 때 다른 파일을 명령 행에 추가하십시오.

+0

링커 명령이 실패했음을 언급했는데 명령 줄에 다른 파일을 어떻게 추가합니까? makefile에 추가 할 수 있습니까? –

+0

@ user3427042 일반적으로 예. 그러나 makefile이 어떻게 보이고 어떤 링커를 사용하고 있는지 알지 못해서 그것을 고치는 방법을 정말로 알려주지 않습니다. – littleadv

+0

잘 명령 줄에 sortutil.h를 추가하지 않았고 여전히 작동했습니다. btw를 사용하면 clang을 사용합니다. –

관련 문제