使用curses.h的Apple Mach-O Link错误

时间:2012-03-07 22:58:08

标签: c curses

我的程序中多次出现此错误。我简化了一些事情以说明基础知识,但仍然会出现错误。我被告知我需要将这个库文件添加到我的项目中才能工作(libncurses.dylib),它确实解决了一些问题,但不是这个问题。

这是我的代码:

// screen.h

#ifndef screen_h
#define screen_h

#define MAC  1
#define WIN  2
#define LNX  3

#ifdef PLATFORM 
#undef PLATFORM 
#endif

#define PLATFORM MAC

void screen_erase();

#endif

// screen.c

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

#include "screen.h"

#if PLATFORM == MAC

#include <curses.h> 

void screen_erase(){
    erase();
}

#endif

// main.cpp

#include <iostream>
#include <curses.h>
#include "screen.h"

using namespace std;

int main(){
    screen_erase();
}

这是我得到的错误:

Undefined symbols for architecture x86_64:
  "screen_erase()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation

这里发生了什么?

1 个答案:

答案 0 :(得分:1)

这是因为你混合了两种不同的语言:C和C ++。

screen.h头文件中,将声明更改为:

#ifdef __cplusplus
extern "C" {
#endif

void screen_erase();

#ifdef __cplusplus
}
#endif

告诉C ++编译器不要对screen_erase函数名name mangling进行操作。

相关问题