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 Code::Blocks 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
Code:
//kbhit function for linux
// taken from http://linux-sxs.org/programming/kbhit.html
//#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 getch()
{
char ch;
if(peek_character != -1)
{
ch = peek_character;
peek_character = -1;
return ch;
}
read(0,&ch,1);
return ch;
}
KBHIT.H
Code:
//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 is the build log with the errors:
-------------- Build: Debug in DELTA ---------------
WARNING: Can't read file's timestamp: /home/eric/Desktop/DELTA sim/DELTA/kbhit.c
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/H_derivs.c" -o obj/Debug/H_derivs.o
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/H_functions.c" -o obj/Debug/H_functions.o
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/RK4.c" -o obj/Debug/RK4.o
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/bisect.c" -o obj/Debug/bisect.o
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/cosmo.c" -o obj/Debug/cosmo.o
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/derivs.c" -o obj/Debug/derivs.o
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/file.c" -o obj/Debug/file.o
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/fisher.c" -o obj/Debug/fisher.o
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/fisher_functions.c" -o obj/Debug/fisher_functions.o
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/kbhit.c" -o obj/Debug/kbhit.o
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/main.c" -o obj/Debug/main.o
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/matrix.c" -o obj/Debug/matrix.o
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/menu.c" -o obj/Debug/menu.o
g++ -o bin/Debug/DELTA obj/Debug/kbhit.o obj/Debug/H_derivs.o obj/Debug/H_functions.o obj/Debug/RK4.o obj/Debug/bisect.o obj/Debug/cosmo.o obj/Debug/derivs.o obj/Debug/file.o obj/Debug/fisher.o obj/Debug/fisher_functions.o obj/Debug/kbhit.o obj/Debug/main.o obj/Debug/matrix.o obj/Debug/menu.o
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 `getch':
/home/eric/Desktop/DELTA sim/kbhit.c:53: multiple definition of `getch'
obj/Debug/kbhit.o:/home/eric/Desktop/DELTA sim/kbhit.c:53: first defined here
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 2 seconds)
8 errors, 0 warnings
Any insight would be great!
Thanks,
Eric