jasonaburton
jasonaburton

Reputation: 3103

Apple Mach-O Link Error with curses.h

I've been getting this error numerous times in my program. I've simplified things down a bit to illustrate the basics and am still getting errors. I was told that I needed to add this library file to my project for it to work (libncurses.dylib) and it did solve some problems, but not this one.

Here is my code:

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

And here's the error I am getting:

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

What's going on here?

Upvotes: 0

Views: 371

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409364

It's because you mix two different languages: C and C++.

In the screen.h header file, change the declaration to this:

#ifdef __cplusplus
extern "C" {
#endif

void screen_erase();

#ifdef __cplusplus
}
#endif

That tells the C++ compiler to not do name mangling on the screen_erase function name.

Upvotes: 1

Related Questions