Linking problems

This is a discussion on Linking problems within the C++ Programming forums, part of the General Programming Boards category; I'm doing a little test project. A simple interface for handling keyboard input in Windows, however I was getting a ...

  1. #1
    myNegReal
    Join Date
    Jun 2005
    Posts
    100

    Linking problems

    I'm doing a little test project. A simple interface for handling keyboard input in Windows, however I was getting a little problems for it to link. I made a simple program as follows:
    Code:
    #include <iostream>
    #include "Input.h"
    
    int main() {
        Input::vkeys[1] = true;
        if (Input::vkeys[1])
           std::cout<<Input::vkeys[1];
    }
    vkeys is a static bool array inside class Input in Input.h.
    However, no matter what type of project it is, it says
    [Linker error] undefined reference to `Input::vkeys'
    Don't you usually get the when linking to libraries. There are no libraries as this is a header defined by me. What do I do to get it to link?
    If you need Input.h and .cpp(for function declarations), just say so.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You need to define vkeys in your Input.cpp file.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Is Input.cpp being compiled/linked with the rest of your project or have you just been compiling the source file with your main function in it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Input.cpp is in the project.
    You need to define vkeys in your Input.cpp file.
    I have vkeys defined in the header file, does it need to be defined in Input.cpp as well?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Yes... it is declared in the header file and defined in the source file. Something like this:

    bool Input::vkeys[];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ OpenGL linking problems
    By KoshiB in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2006, 04:25 PM
  2. Linking problems, class problems
    By Kheila in forum C++ Programming
    Replies: 12
    Last Post: 11-22-2005, 12:47 AM
  3. Linking problems trying to compile QT application
    By Maragato in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2005, 09:08 PM
  4. Grrr.... SDL Linking Problem
    By 7EVEN in forum Game Programming
    Replies: 5
    Last Post: 08-12-2005, 08:44 PM
  5. More linker problems
    By Ganoosh in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2005, 10:27 PM

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