Multiple Definition Problem

This is a discussion on Multiple Definition Problem within the Linux Programming forums, part of the Platform Specific Boards category; Hello everyone. Really, I already searched these and other forums pretty well, and I haven't resolved the issue yet. I ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    1

    Multiple Definition Problem

    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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    > 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
    You asked to link it twice, so it did - and complained.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by EASports View Post
    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.
    Nobody does this themselves, you masochist Just use ncurses.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple definition linker error
    By R.Stiltskin in forum C++ Programming
    Replies: 10
    Last Post: 03-27-2009, 05:05 AM
  2. Multiple Definition
    By Finfarfin in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2006, 03:18 PM
  3. Multiple Definition problems
    By shiver in forum C++ Programming
    Replies: 12
    Last Post: 08-30-2006, 02:33 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21