User forums > Using Code::Blocks
Multiple Definitions (I promise I already searched the forums!)
EASports:
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
stahta01:
I would guess your Linux Compiler is not setup right. Not sure if it is a Code::Blocks related issue.
I suggest turning on Full Compiler Logging and then going to a Linux C Programming Site for help.
http://wiki.codeblocks.org/index.php?title=FAQ#Q:_How_do_I_troubleshoot_an_compiler_problem.3F
If you post the "Build Log" someone might be able to confirm if it is a setup issue.
Be it the setup in Code::Blocks, the Compiler, or some Linux setup issue.
Tim S.
EASports:
Thanks, I will post it in a general Linux C forum. BUT, just in case anyone feels like taking a look, here's the build log: (all of the warnings are from not including kbhit.h yet)
-------------- 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
/home/eric/Desktop/DELTA sim/file.c: In function ‘get_basename’:
/home/eric/Desktop/DELTA sim/file.c:28: warning: implicit declaration of function ‘getch’
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
/home/eric/Desktop/DELTA sim/main.c: In function ‘calc_wave’:
/home/eric/Desktop/DELTA sim/main.c:376: warning: implicit declaration of function ‘kbhit’
/home/eric/Desktop/DELTA sim/main.c:377: warning: implicit declaration of function ‘getch’
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
/home/eric/Desktop/DELTA sim/menu.c: In function ‘anykey’:
/home/eric/Desktop/DELTA sim/menu.c:54: warning: implicit declaration of function ‘getch’
gcc -Wall -std=c99 -g -c "/home/eric/Desktop/DELTA sim/test.c" -o obj/Debug/test.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/test.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
obj/Debug/test.o: In function `main':
/home/eric/Desktop/DELTA sim/test.c:15: multiple definition of `main'
obj/Debug/main.o:/home/eric/Desktop/DELTA sim/main.c:21: first defined here
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 2 seconds)
10 errors, 4 warnings
stahta01:
--- Quote from: EASports on July 14, 2009, 05:12:02 am ---/home/eric/Desktop/DELTA sim/main.c:376: warning: implicit declaration of function ‘kbhit’
/home/eric/Desktop/DELTA sim/main.c:377: warning: implicit declaration of function ‘getch’
--- End quote ---
Here's the real message you need to understand.
An "implicit declaration" is very bad.
Edit: It means the compiler guesses what it should mean using the default prototype of an function.
Which means it defaults to returning an int and to taking no parameters for most old,the ones I learned on 25 years ago, C compilers. No idea how GCC defaults.
You need to include the header that defines these functions in an explicit manner.
I would guess adding this line(s) to top of main.c is the correct solution/next step.
--- Code: ---#include "kbhit.h"
--- End code ---
or
--- Code: ---#define MAIN
#include "kbhit.h"
--- End code ---
Note: the define of MAIN is a guess on my part; based on other people code. Only one of the times the header "kbhit.h" is include tends to define MAIN.
Sometimes it is the main.c file and sometimes the kbhit.c file. In rare cases it could be both.
Tim S
EASports:
The implicit declarations are only there because I was trying to isolate the problem with the multiple definition errors. If I include "kbhit.h" then the warnings do go away, but the errors remain:
-------------- 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
Navigation
[0] Message Index
[#] Next page
Go to full version