Hello everyone.
Really, I already searched these and other forums pretty well, and I haven't resolved the issue yet.
I am developing a suite of computational aides for research in physics, and I just recently switched into Linux. I'm trying out C::B as an IDE, but one issue is coming up.
I am trying to replicate getch() and kbhit() functions in Linux, which I found code for at a couple of other forums. When I try to compile these codes, however, I get multiple definition errors.
Here is the code:
KBHIT.C//kbhit function for linux
// taken from
http://linux-sxs.org/programming/kbhit.html//#ifndef KBHIT_H
//#define KBHIT_H
//#include "kbhit.h"
#include <termios.h>
#include <stdio.h>
#include <unistd.h> // for read()
static struct termios initial_settings, new_settings;
static int peek_character = -1;
void init_keyboard()
{
tcgetattr(0,&initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_lflag &= ~ISIG;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_settings);
}
void close_keyboard()
{
tcsetattr(0, TCSANOW, &initial_settings);
}
int kbhit()
{
unsigned char ch;
int nread;
if (peek_character != -1) return 1;
new_settings.c_cc[VMIN]=0;
tcsetattr(0, TCSANOW, &new_settings);
nread = read(0,&ch,1);
new_settings.c_cc[VMIN]=1;
tcsetattr(0, TCSANOW, &new_settings);
if(nread == 1)
{
peek_character = ch;
return 1;
}
return 0;
}
int readch()
{
char ch;
if(peek_character != -1)
{
ch = peek_character;
peek_character = -1;
return ch;
}
read(0,&ch,1);
return ch;
}
//getch function from
//http://cboard.cprogramming.com/faq-board/27714-faq-there-getch-conio-equivalent-linux-unix.html
int getch() {
struct termios oldt,
newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
//#endif
KBHIT.H//kbhit function for linux
//taken from
http://linux-sxs.org/programming/kbhit.html#ifndef KBHIT_H
#define KBHIT_H
#ifdef MAIN
#define EXTERN // the Main module defines objects
#else
#define EXTERN extern // others modules see objects as externs
#endif
EXTERN void init_keyboard();
EXTERN void close_keyboard();
EXTERN int kbhit();
EXTERN int readch();
EXTERN int getch();
#endif
and here are the errors I'm getting:
obj/Debug/kbhit.o||In function `init_keyboard':|
/home/eric/Desktop/DELTA sim/kbhit.c|17|multiple definition of `init_keyboard'|
obj/Debug/kbhit.o:/home/eric/Desktop/DELTA sim/kbhit.c|17|first defined here|
obj/Debug/kbhit.o||In function `close_keyboard':|
/home/eric/Desktop/DELTA sim/kbhit.c|29|multiple definition of `close_keyboard'|
obj/Debug/kbhit.o:/home/eric/Desktop/DELTA sim/kbhit.c|29|first defined here|
obj/Debug/kbhit.o||In function `kbhit':|
/home/eric/Desktop/DELTA sim/kbhit.c|34|multiple definition of `kbhit'|
obj/Debug/kbhit.o:/home/eric/Desktop/DELTA sim/kbhit.c|34|first defined here|
obj/Debug/kbhit.o||In function `readch':|
/home/eric/Desktop/DELTA sim/kbhit.c|53|multiple definition of `readch'|
obj/Debug/kbhit.o:/home/eric/Desktop/DELTA sim/kbhit.c|53|first defined here|
obj/Debug/kbhit.o||In function `getch':|
/home/eric/Desktop/DELTA sim/kbhit.c|71|multiple definition of `getch'|
obj/Debug/kbhit.o:/home/eric/Desktop/DELTA sim/kbhit.c|71|first defined here|
this is happening without any other files trying to #include kbhit.h. Even kbhit.c doesn't include it right now...
Any insight would be awesome.
Thanks!
Eric