Airwavezx
Airwavezx

Reputation: 957

Compiling Unix code on Mac

We were given course work to create the game Mine Sweeper. We are still early in the semester so this homework shouldn't be too difficult. We were given header and source files that are to be used for the visual part of the program. Main problem is that I can't compile these files on my Mac. Here is what I get:

$ 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)

Also tried this:

$ 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 version:

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 version:

Software  OS X 10.9.5 (13F34)

And finally the code we were provided with:

//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");
}

There is another header and code for receiving the char from user but I'm assuming linking it is irrelevant. Any sort of help is welcome.. thanks in advance :)

PS. I also have a PC if it helps to switch to windows. PPS. I'm keeping VirtualBox as a last resort.

Upvotes: 0

Views: 89

Answers (1)

Jonathan Wakely
Jonathan Wakely

Reputation: 171273

You're trying to compile and link mineSweeper.c into a final executable on its own, but that file is not a complete program, it depends on a function defined in another file.

You either need to compile and link all the files in one step:

gcc mineSweep.c colourPrint.c

or compile each file separately and then link the objects:

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

I'm surprised your course didn't explain how to compile programs consisting of more than one file.

A simple makefile will ease the process:

mineSweeper: mineSweeper.o colorPrint.o
        $(CC) $^ $(LDLIBS) -o $@

Upvotes: 1

Related Questions