Author Topic: Multiple Definitions (I promise I already searched the forums!)  (Read 10472 times)

Offline EASports

  • Single posting newcomer
  • *
  • Posts: 5
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

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Multiple Definitions (I promise I already searched the forums!)
« Reply #1 on: July 14, 2009, 04:04:17 am »
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.
« Last Edit: July 14, 2009, 04:11:14 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline EASports

  • Single posting newcomer
  • *
  • Posts: 5
Re: Multiple Definitions (I promise I already searched the forums!)
« Reply #2 on: July 14, 2009, 05:12:02 am »
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
 

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Multiple Definitions (I promise I already searched the forums!)
« Reply #3 on: July 14, 2009, 05:41:29 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’

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"

or

Code
#define MAIN
#include "kbhit.h"

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



« Last Edit: July 14, 2009, 05:54:13 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline EASports

  • Single posting newcomer
  • *
  • Posts: 5
Re: Multiple Definitions (I promise I already searched the forums!)
« Reply #4 on: July 14, 2009, 05:51:16 am »
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
 

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Multiple Definitions (I promise I already searched the forums!)
« Reply #5 on: July 14, 2009, 05:56:45 am »
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:

Did you re-add it to both main.c and kbhit.c?
Which ones did you define MAIN in?

The error in this case implies MAIN should be defined in kbhit.c.

Tim S
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline EASports

  • Single posting newcomer
  • *
  • Posts: 5
Re: Multiple Definitions (I promise I already searched the forums!)
« Reply #6 on: July 14, 2009, 06:32:00 am »
Did you re-add it to both main.c and kbhit.c?

It is added in kbhit.c and delta.h, which is included in main.c


Which ones did you define MAIN in?

main() is defined in main.c


The error in this case implies MAIN should be defined in kbhit.c.

really? Which part of the error implies that? How could that have happened?

Thanks!

Offline EASports

  • Single posting newcomer
  • *
  • Posts: 5
Re: Multiple Definitions (I promise I already searched the forums!)
« Reply #7 on: July 14, 2009, 06:42:12 am »
I think I got it. There seemed to be some issue with having a function named kbhit() inside the file named kbhit.c

Anyways, now it's compiling. Thanks a lot for your help!

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Multiple Definitions (I promise I already searched the forums!)
« Reply #8 on: July 14, 2009, 06:42:41 am »
Code
#ifdef MAIN
#define EXTERN           // the Main module defines objects
#else
#define EXTERN extern    // others modules see objects as externs
#endif

The above code means normally define EXTERN as extern; but, in the Main module define it as nothing.
Sometimes the Main modules is the code holding the main() and sometimes it is the "c" file that goes with header.

I am guessing that since you are having issues with kbhit.c errors; that in this case MAIN needs to be defined there. But, it is just a guess!

Where are you defining MAIN?

Tim S
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Multiple Definitions (I promise I already searched the forums!)
« Reply #9 on: July 14, 2009, 06:44:32 am »
I think I got it. There seemed to be some issue with having a function named kbhit() inside the file named kbhit.c

Anyways, now it's compiling. Thanks a lot for your help!

Could be an Compiler or linker bug.

Tim S
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Multiple Definitions (I promise I already searched the forums!)
« Reply #10 on: July 14, 2009, 10:02:41 am »
This has indeed nothing todo with C::B, so the topic gets locked !!

Nevertheless a wild guess: you only give us the code from the kbhit.* files, they compile fine here, you did not give any other code, but you get exactly the behaviour you describe, if you include not only kbhit.h but also kbhit.c.