Thread: Linking

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Question Linking

    I wrote a .cpp full of functions and made a .h for it. How can I use header in other files without compiling the .cpp?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    you can't
    Kurt

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Your answer was like a hammer!
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    OK. I made a .lib
    But there is a problem, linker generates an error and says you defined some vars in both files (.lib and the caller .cpp). And it is true, because there are some vars, const, etc defined in the header and the header was included in both .cpp and .lib.
    What is the solution?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You DON'T define variables in the header, never ever. If you must have a reference toa variable there, declare it as extern:

    X.h:
    Code:
    extern int Y;
    X.cpp:
    Code:
    int Y;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    You mean make it extern, define it in both .lib and .cpp and recompile all?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Add "int Y" in one of the modules
    Add "extern int Y" in all of the modules
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    The problem is that I defined an array in .h and I don't want to redefine it anywhere else.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by siavoshkc
    The problem is that I defined an array in .h and I don't want to redefine it anywhere else.
    There is only one solution then. Just use that header in only one compilation unit.

    Kurt.

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    OK, I start another thread for solving this.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems linking with g++
    By Just in forum Linux Programming
    Replies: 11
    Last Post: 07-24-2006, 01:35 AM
  2. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 10:51 PM
  3. Replies: 8
    Last Post: 04-27-2006, 10:39 AM
  4. Grrr.... SDL Linking Problem
    By 7EVEN in forum Game Programming
    Replies: 5
    Last Post: 08-12-2005, 08:44 PM
  5. Linking error with DirectDrawCreateEx
    By jjj93421 in forum Game Programming
    Replies: 6
    Last Post: 04-06-2004, 03:57 PM