Thread: Linker error I can't fix

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    21

    Linker error I can't fix

    Hello all,
    I'm trying to compile code in a borland compiler (don't ask... it has to be done), and I'm getting this error (by linker):



    _brojac defined in module interr.cpp is duplicated in module interr.cpp
    timer() defined in module interr.cpp is duplicated in module interr.cpp


    (same goes for swapRoutine and swapTimer)
    Any idea what this is caused by ?

    here's the files (the relevant parts):


    Code:
    // interr.h
    
    #ifndef _interrupt_h_
    #define _interrupt_h_
    
    
    typedef void interrupt (*IntRoutine)();   // interrupt routine pointer type
    typedef unsigned int IntAdrPointer;  // interupt routine pointer in IVT
    
    
    
    
    IntRoutine swapRoutine(IntAdrPointer oldPointer, IntRoutine newRoutine);
    IntRoutine swapTimer();
    void interrupt timer();
    
    
    
    
    #endif
    
    
    // interr.cpp
    
    #ifndef _interrupt_cpp_
    #define _interrupt_cpp_
    
    
    #include"interr.h"
    #include"lounlo.h"
    #include<iostream.h>
    #include<dos.h>  // FP_SEG, FP_OFF, MK_FP
    
    
    volatile int brojac = 100;
    
    
    IntRoutine swapTimer() {
        return swapRoutine(0x1C,timer);
    }
    
    
    IntRoutine swapRoutine(IntAdrPointer oldPointer, IntRoutine newRoutine) {
    ...
    } // swapRoutine
    
    
    
    
    void interrupt timer() {
    ...
    }
    
    
    
    
    #endif

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > #ifndef _interrupt_cpp_
    > #define _interrupt_cpp_
    Lemme guess, you have other source files containing
    #include "interr.cpp"

    The answer is to STOP including source code, and include the header file instead.

    Then add interr.cpp to the list of source files to be compiled.
    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.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    21
    Quote Originally Posted by Salem View Post
    > #ifndef _interrupt_cpp_
    > #define _interrupt_cpp_
    Lemme guess, you have other source files containing
    #include "interr.cpp"

    The answer is to STOP including source code, and include the header file instead.

    Then add interr.cpp to the list of source files to be compiled.

    What? I never include .cpp files, what's the point of headers then? That's not it...
    The source files are all protected with #ifndef/#define as a precaution, its a really complicated project..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > What? I never include .cpp files, what's the point of headers then? That's not it...
    Perhaps you can begin by staring at #include<iostream.h> and wondering why it isn't #include<iostream.cpp>

    Header files are for declarations, not definitions. If your header file contains definitions (and you include it in many places), then that is what the linker is telling you - "timer() defined in module interr.cpp is duplicated in module interr.cpp"
    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.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    21
    Do you see definitions in that header file?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, all those things with = and braces are definitions.

    Your interr.h contains declarations, and these you can include as many times as you like.

    But you only need one actual implementation (definition) of say
    Code:
    IntRoutine swapTimer() {
        return swapRoutine(0x1C,timer);
    }
    which you get when you compile the actual source code.
    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.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    21
    Um... No, there are no definitions in the .h file. The code is two seperate files, interr.h and interr.cpp. Declarations of all three functions and two typedefs are in the interr.h, and the definitions and a global variable are are in interr.cpp

    interr.cpp is compiled, and then whenever I want a function defined in it in another .cpp, I include it's header, interr.h

    Those are two files up there, not one :P (i was lazy to write two code blocks with [])
    Last edited by total_newb; 05-11-2013 at 08:56 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > interr.cpp is compiled, and then whenever I want a function defined in it in another .cpp, I include it's header, interr.h
    I suggest you look again, because it seems to me that at least one other source file has #include "interr.cpp" instead.

    Otherwise, how would you get to the multiply declared symbols?

    Just search all the source files for "interr.cpp".
    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.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    How many times have you have added these source files to your project? They should only ever be added once to your project, and remove the inclusion guards from your .c/.cpp files they're not needed and could actually cause problems. Inclusion guards only come into play with the pre-processor, not the compiler or linker.

    Jim

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    21
    Quote Originally Posted by Salem View Post
    > interr.cpp is compiled, and then whenever I want a function defined in it in another .cpp, I include it's header, interr.h
    I suggest you look again, because it seems to me that at least one other source file has #include "interr.cpp" instead.

    Otherwise, how would you get to the multiply declared symbols?

    Just search all the source files for "interr.cpp".
    Im sorry, I don't get it. I never wrote
    #include"interr.cpp"
    in my code, only it's header is included twice (in inter.cpp and in main.cpp, so I could call it's functions from there). I've done the same with lounlo.h and that gives me no trouble.

    I've removed all preprocessors guards from cpp files. No change. All source files are added once to the project. Funny story, when I compile the source files one by one, it goes smooth. Once I attempt to build the exe (link them), I get these errors.

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    21
    Here's the remainder of the code - so you can see for yourself that I haven't included the interr.cpp twice:

    Code:
    // usermain.cpp
    // is called upon from  int main(argc,argv)
    
    #include<iostream.h>
    #include"interr.h"
    #include"lounlo.h"
    
    
    
    
    int userMain(int argc, char* argv[]) {
    	cout << "hello world!" << endl;
    	cout << "lets test interrupt routine swaping! :O " << endl;
    	IntRoutine old;
    	old = swapTimer();
    	
    	
    	return 0; // no errors
    }
    Code:
    // main.cpp
    
    int userMain(int argc, char* argv[]);
    int main(int argcount, char* argvec[]) {
    	return userMain(argcount,argvec);
    }

    That's all I've written, oh and

    Code:
    // lounlo.h
    #ifndef _lockunlock_
    #define _lockunlock_
    
    
    #define lock asm cli
    #define unlock asm sti
    
    
    #endif

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I call shenanigans. The code you have shown does not produce such a problem.
    You are perhaps leaving out some part you don't want us to see because you don't think it's the problem.
    What I'm saying is that there is not something we are missing here in regards to that code. There isn't something that will suddenly jump out at us.

    What borland compiler is this?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    21
    It's BC31 C++
    It's ancient and broken and I suspect it is the reason of my grief. And here's the best part - I have to use this particular one.

    I can't solve the problem because I don't know what the problem is. The only parts of code left out are the two functions (some asm code that surely works, and a simple for(..)) that shouldn't even cause this problem with linker.
    Last edited by total_newb; 05-12-2013 at 03:46 AM.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > The source files are all protected with #ifndef/#define as a precaution, its a really complicated project..
    So far, you've only posted a handful of lines of code scattered over at most 5 files.

    Is that all there is?
    I'd kind of imagined that you've just found (or been given) a large amount of code which you didn't write, and now you're asking us to fix it for you.

    Another possibility is that you have old object files from previous compiles.
    Try something like
    build->clean
    build->build all
    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.

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    21
    Why would I hide anything if I wanted help? I'm starting to write a 'pretend' OS kernel (a basic OS working under an actual OS on simulated 8086 architecture), and I began by changing the interrupt routine generated by clock every 55ms, which I will later use to control how much time each thread is given using the CPU of a 8086 CPU I'm writing this for.. ANYWAY, no, that's all the code there is.


    I have actually tried deleting all old .obj and the old .exe file before I posted here, then compiling, no good.

    Here's a screenshot of the compiler (the error and the project files) - now there really ins't anything I'm "hiding"

    http://i.imgur.com/haaCKCa.jpg
    Linker error I can't fix-haackca-jpg
    Last edited by total_newb; 05-12-2013 at 05:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linker error
    By royroy in forum C Programming
    Replies: 4
    Last Post: 12-24-2012, 12:24 PM
  2. linker error ???
    By jadaces in forum C Programming
    Replies: 4
    Last Post: 07-15-2010, 04:46 PM
  3. Linker error
    By Basca in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2006, 02:27 PM
  4. Linker Error !!!!
    By CodeJerk in forum C++ Programming
    Replies: 9
    Last Post: 11-15-2002, 07:34 AM
  5. Linker Error
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2002, 04:32 PM