Sneaky little linker errors...

This is a discussion on Sneaky little linker errors... within the C++ Programming forums, part of the General Programming Boards category; They always find their way into my programs! This time I'm getting errors when I try to include my header ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    17

    Angry Sneaky little linker errors...

    They always find their way into my programs!

    This time I'm getting errors when I try to include my header file in two different files of the same project. The only way it works is if instead of #including "consoletools.h", I #include "consoletools.cpp" in one of the two files. I'm also getting errors when i make a test program for the sole purpose of using the functions. The .h contains just the function definitions, the .cpp has the actual code for the functions.

    The consoletools.h file:
    Code:
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    #ifndef CONSOLETOOLS_H
    #define CONSOLETOOLS_H
    
    void setTitle(char title[]);
    
    void cls();
    
    void replaceChar(char newChar, int x, int y);
    
    void color(string text, string color);
    
    void toggleCursor();
    
    void pause();
    
    char getYorN();
    
    #endif
    The test file:
    Code:
    #include "consoletools.h"
    #include <iostream>
    using namespace std;
    
    int main()
    {
       string x; 
       while (true)
       {
          cin >> x;
          cin.ignore();  
          setTitle("Test program"); 
          color("Color\n",x);   
          pause();
          cls();
       } 
    }
    The linker errors:
    Code:
      [Linker error] undefined reference to `setTitle(char*)' 
      [Linker error] undefined reference to `color(std::string, std::string)' 
      [Linker error] undefined reference to `pause()' 
      [Linker error] undefined reference to `cls()' 
      ld returned 1 exit status
    I'm sure the answer is obvious... but I can't seem to point it out. I hate linker errors!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    You need to add consoletools.cpp to the project.
    That is, both need 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    Whaaatt... are you saying i need to add it to every project I use it in? Does that mean I can't use it on programs with only one source file?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    That's what it means.

    Another option if all those functions have small implementations is to place them inline in the header file. Then you can use the header by itself in a one source file project.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    That's strange, I've been able to #include .h files without having to worry about adding their .cpp to the project.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Did those headers have their function implemented in the header file, or a cpp file? Were those headers part of a library that was being linked to the project?

    Another option is to build a lib file and add that to the linker options. You won't have to include the source files in each project, but you will have to remember to link to the library.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    No, the header files consisted of nothing but the function headers (within a class, however). Their .cpps werent in the same folder, but their object files were.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If you have the object files to link to, then you don't actually need to add the source files. It works the same as the lib stuff I mentioned. You just have to figure out how to get the object files to link to your source file. How you do that depends on your IDE or compilation/link method.

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    You should've noticed some logic errors there:
    You have a header file, with prototypes.
    You have a source file, where you call those functions.
    You get an undefined reference error and you don't notice there's no function definitions in your files??
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker errors
    By jw232 in forum C++ Programming
    Replies: 3
    Last Post: 06-12-2009, 01:56 PM
  2. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 08:42 AM
  3. Linker errors when compiling
    By The Wazaa in forum C++ Programming
    Replies: 4
    Last Post: 10-07-2006, 01:55 PM
  4. Linker errors with Visual C++
    By codegirl in forum C++ Programming
    Replies: 4
    Last Post: 09-11-2003, 10:20 AM
  5. MSVis-Studio C++ libraries and linker errors
    By kellydj in forum Windows Programming
    Replies: 10
    Last Post: 03-12-2002, 02:03 PM

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