2014-11-29 4 views
0

Нам была дана учебная работа по созданию игры Mine Sweeper. Мы еще в начале семестра, поэтому эта домашняя работа не должна быть слишком сложной. Нам были предоставлены заголовки и исходные файлы, которые будут использоваться для визуальной части программы. Основная проблема заключается в том, что я не могу скомпилировать эти файлы на своем Mac. Вот что я получаю:Компиляция кода Unix на Mac

$ gcc mineSweeper.c -I. 
Undefined symbols for architecture x86_64: 
    "_colorPrint", referenced from: 
     _main in mineSweeper-4b9486.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

попытался Также это:

$ gcc mineSweeper.c -I. -arch i386 
Undefined symbols for architecture i386: 
    "_colorPrint", referenced from: 
     _main in mineSweeper-0938b1.o 
ld: symbol(s) not found for architecture i386 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

версия GCC:

gcc --version 
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn) 
Target: x86_64-apple-darwin13.4.0 
Thread model: posix 

OSX версии:

Software OS X 10.9.5 (13F34) 

И, наконец, код, который мы были при условии:

//colorPrint.h 

//defines possible colors for the foreground color. 
typedef enum 
{ 
    FG_Def = 0, 
    FG_Black = 30, 
    FG_Red, 
    FG_Green, 
    FG_Yellow, 
    FG_Blue, 
    FG_Magenta, 
    FG_Cyan, 
    FG_White 
}fgColor; 

//defines possible colors for the background color. 
//BG_Def prints with the default background color of the terminal. 
typedef enum 
{ 
    BG_Def = 0, 
    BG_Black = 40, 
    BG_Red, 
    BG_Green, 
    BG_Yellow, 
    BG_Blue, 
    BG_Magenta, 
    BG_Cyan, 
    BG_White 
}bgColor; 

//defines possible additional attributes for the color printing. 
//normally, you would use ATT_Def. 
typedef enum 
{ 
    ATT_Def = 0, 
    ATT_Bright = 1, 
    ATT_Underline = 4, 
    ATT_Reverse = 7, 
    ATT_Hidden = 8, 
    ATT_Scratch = 9 
}attribute; 

//clears the screen completely. 
void clearScreen(); 

//prints a format string with its arguments (like printf!), 
//in the specified foreground color, background color, and attribute. 
void colorPrint(fgColor fg, bgColor bg, attribute att, char* format,...); 


//colorPrint.c 

#include <stdio.h> 
#include <colorPrint.h> 
#include <stdarg.h> 

void clearScreen() 
{ 
    printf("\e[1;1H\e[2J"); 
} 

void colorPrint(fgColor fg, bgColor bg, attribute att, char* format,...) 
{ 
    va_list args; 
    if(bg != BG_Def) 
     printf("\e[%d;%d;%dm",att,fg,bg); 
    else 
     printf("\e[%d;%dm",att,fg); 

    va_start (args, format); 
    vprintf(format, args); 
    va_end (args); 
    printf("\e[0m"); 
} 

Существует еще один заголовок и код для получения символа от пользователя, но я предполагаю, что ссылка не имеет значения. Любая помощь приветствуется .. заранее спасибо :)

PS. У меня также есть компьютер, если он помогает переключиться на окна. PPS. Я поддерживаю VirtualBox как последнее средство.

+0

Где главный? – igon

+0

@igon Есть ли главное? Все компилируется до тех пор, пока я не буду использовать функцию colorPrint. – Airwavezx

+0

Функция 'colorPrint' компилируется правильно. Возможно, вы неправильно вызываете colorPrint. – igon

ответ

1

Вы пытаетесь скомпилировать и связать в свой собственный исполняемый файл самостоятельно, но этот файл не является полной программой, это зависит от функции, определенной в другом файле.

Вам либо нужно собрать и связать все файлы в одном шаге:

gcc mineSweep.c colourPrint.c 

или компилировать каждый файл по отдельности, а затем связать объекты:

gcc -c mineSweeper.c 
gcc -c colorPrint.c 
gcc mineSweeper.o colorPrint.o 

Я удивлен, что ваш курс Didn Не объясняйте, как скомпилировать программы, состоящие из нескольких файлов.

Простой Makefile облегчит процесс:

mineSweeper: mineSweeper.o colorPrint.o 
     $(CC) $^ $(LDLIBS) -o [email protected] 
+0

Ничего себе. Спасибо, сэр. Я также удивлен, что исправление было настолько простым. Ошибка, которую я получил, заставила меня искать решения далеко от точки. – Airwavezx

Смежные вопросы