在Mac上编译Unix代码

时间:2014-11-29 17:19:38

标签: c macos gcc

我们获得了创建游戏Mine Sweeper的课程作品。我们还在学期初,所以这个功课不应该太难。 我们获得了用于程序可视部分的头文件和源文件。 主要问题是我无法在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");
}

还有另一个标题和代码用于从用户接收char,但我假设链接它是无关紧要的。 欢迎任何形式的帮助..提前感谢:)

PS。如果它有助于切换到Windows,我也有一台PC。 PPS。我将VirtualBox作为最后的手段。

1 个答案:

答案 0 :(得分:1)

您正在尝试编译并将mineSweeper.c链接到最终的可执行文件中,但该文件不是一个完整的程序,它取决于另一个文件中定义的函数。

您需要一步编译并链接所有文件:

gcc mineSweep.c colourPrint.c

或单独编译每个文件,然后链接对象:

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

我很惊讶你的课程没有解释如何编译由多个文件组成的程序。

一个简单的makefile将简化这个过程:

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